@modern-js/main-doc 2.55.0 → 2.56.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ ---
2
+ sidebar_label: liveReload
3
+ ---
4
+
5
+ # dev.liveReload
6
+
7
+ - **Type:** `boolean`
8
+ - **Default:** `true`
9
+
10
+ Whether to reload the page when source files are changed.
11
+
12
+ By default, Modern.js uses HMR as the preferred method to update modules. If HMR is disabled or cannot be used in certain scenarios, it will automatically fallback to liveReload.
13
+
14
+ Please refer to [Hot Module Replacement](https://rsbuild.dev/guide/advanced/hmr) for more information.
15
+
16
+ ## Disabling liveReload
17
+
18
+ If you need to disable liveReload, you can set both `dev.hmr` and `dev.liveReload` to `false`. Then, no Web Socket requests will be made to the dev server on the page, and the page will not automatically refresh when file change.
19
+
20
+ ```js
21
+ export default {
22
+ dev: {
23
+ hmr: false,
24
+ liveReload: false,
25
+ },
26
+ };
27
+ ```
@@ -0,0 +1,69 @@
1
+ ---
2
+ sidebar_label: setupMiddlewares
3
+ ---
4
+
5
+ # dev.setupMiddlewares
6
+
7
+ - **Type:**
8
+
9
+ ```ts
10
+ type ServerAPIs = {
11
+ sockWrite: (
12
+ type: string,
13
+ data?: string | boolean | Record<string, any>,
14
+ ) => void;
15
+ };
16
+
17
+ type SetupMiddlewares = Array<
18
+ (
19
+ middlewares: {
20
+ unshift: (...handlers: RequestHandler[]) => void;
21
+ push: (...handlers: RequestHandler[]) => void;
22
+ },
23
+ server: ServerAPIs,
24
+ ) => void
25
+ >;
26
+ ```
27
+
28
+ - **Default:** `undefined`
29
+
30
+ Provides the ability to execute a custom function and apply custom middlewares.
31
+
32
+ The order among several different types of middleware is: `unshift` => internal middlewares => `push`.
33
+
34
+ ```js
35
+ export default {
36
+ dev: {
37
+ setupMiddlewares: [
38
+ (middlewares, server) => {
39
+ middlewares.unshift((req, res, next) => {
40
+ next();
41
+ });
42
+
43
+ middlewares.push((req, res, next) => {
44
+ next();
45
+ });
46
+ },
47
+ ],
48
+ },
49
+ };
50
+ ```
51
+
52
+ It is possible to use some server api to meet special scenario requirements:
53
+
54
+ - sockWrite. Allow send some message to HMR client, and then the HMR client will take different actions depending on the message type. If you send a "content changed" message, the page will reload.
55
+
56
+ ```js
57
+ export default {
58
+ dev: {
59
+ setupMiddlewares: [
60
+ (middlewares, server) => {
61
+ // add custom watch & trigger page reload when change
62
+ watcher.on('change', (changed) => {
63
+ server.sockWrite('content-changed');
64
+ });
65
+ },
66
+ ],
67
+ },
68
+ };
69
+ ```
@@ -0,0 +1,59 @@
1
+ ---
2
+ sidebar_label: watchFiles
3
+ ---
4
+
5
+ # dev.watchFiles
6
+
7
+ - **Type:**
8
+
9
+ ```ts
10
+ type WatchFiles = {
11
+ paths: string | string[];
12
+ // watch options for chokidar
13
+ options?: WatchOptions;
14
+ };
15
+ ```
16
+
17
+ - **Default:** `undefined`
18
+
19
+ Watch files and directories for changes. When a file changes, the page will be reloaded.
20
+
21
+ If both `dev.hmr` and `dev.liveReload` are set to false, `watchFiles` will be ignored.
22
+
23
+ :::tip
24
+ When files in WatchFiles change, reloading and recompilation of the configuration file will not be triggered.
25
+ :::
26
+
27
+ ### Example
28
+
29
+ You can configure a list of globs/directories/files to watch for file changes.
30
+
31
+ ```js
32
+ export default {
33
+ dev: {
34
+ watchFiles: {
35
+ // watch a single file
36
+ paths: 'public/demo.txt',
37
+ // use a glob pattern
38
+ paths: 'src/**/*.txt',
39
+ // watch multiple file paths
40
+ paths: ['src/**/*.txt', 'public/**/*'],
41
+ },
42
+ },
43
+ };
44
+ ```
45
+
46
+ You can also specify [chokidar](https://github.com/paulmillr/chokidar#api) watcher options by passing an object with `paths` and `options` properties.
47
+
48
+ ```js
49
+ export default {
50
+ dev: {
51
+ watchFiles: {
52
+ paths: 'src/**/*.txt',
53
+ options: {
54
+ usePolling: false,
55
+ },
56
+ },
57
+ },
58
+ };
59
+ ```
@@ -0,0 +1,38 @@
1
+ ---
2
+ sidebar_label: writeToDisk
3
+ ---
4
+
5
+ # dev.writeToDisk
6
+
7
+ - **Type:** `boolean | ((filename: string) => boolean)`
8
+ - **Default:** `(file) => !file.includes('.hot-update.')`
9
+
10
+ Used to control whether the build artifacts of the development environment are written to the disk.
11
+
12
+ ## Writing to Memory
13
+
14
+ You can choose to save the build artifacts in the memory of the dev server, thereby reducing the overhead caused by file operations.
15
+
16
+ Simply set the `dev.writeToDisk` option to `false`:
17
+
18
+ ```ts
19
+ export default {
20
+ dev: {
21
+ writeToDisk: false,
22
+ },
23
+ };
24
+ ```
25
+
26
+ ## Matching Specific Files
27
+
28
+ You can also set `dev.writeToDisk` to a function to match only certain files. When the function returns `false`, the file will not be written; when it returns `true`, the file will be written to disk.
29
+
30
+ For example, Modern.js by default write files to disk while excluding hot-update temporary files:
31
+
32
+ ```ts
33
+ export default {
34
+ dev: {
35
+ writeToDisk: (file) => !file.includes('.hot-update.'),
36
+ },
37
+ };
38
+ ```
@@ -247,7 +247,7 @@ In a multi-entry (MPA) scenario, the value of `routeId` needs to include the nam
247
247
 
248
248
  If you want to get the data returned by the `loader` in `entry1/routes/layout.tsx`, the value of `routeId` is `entry1_layout`.
249
249
 
250
- ### (WIP)Loading UI
250
+ ### Loading UI (Experimental)
251
251
 
252
252
  :::info
253
253
  This feature is currently experimental and the API may change in the future.
@@ -290,7 +290,7 @@ When accessing the route, you will get the following UI layout:
290
290
  </RootLayout>
291
291
  ```
292
292
 
293
- ### (WIP)Loading
293
+ ### Loading (Experimental)
294
294
 
295
295
  In each directory under `routes/`, developers can create a `loading.tsx` file that exports a `<Loading>` component by default.
296
296
 
@@ -0,0 +1,27 @@
1
+ ---
2
+ sidebar_label: liveReload
3
+ ---
4
+
5
+ # dev.liveReload
6
+
7
+ - **类型:** `boolean`
8
+ - **默认值:** `true`
9
+
10
+ 是否在源文件变更时自动刷新页面。
11
+
12
+ 默认情况下,Modern.js 会优先使用 HMR 来更新模块。当 HMR 功能被禁用,或者某些场景 HMR 无法生效时,会自动降级到 liveReload。
13
+
14
+ 请查看 [模块热更新](https://rsbuild.dev/zh/guide/advanced/hmr) 来了解更多内容。
15
+
16
+ ## 禁用 liveReload
17
+
18
+ 如果你需要禁用 liveReload,可以将 `dev.hmr` 和 `dev.liveReload` 同时设置为 `false`,此时页面上不会发起 Web Socket 请求到 dev server,也不会在文件变更时自动刷新页面。
19
+
20
+ ```js
21
+ export default {
22
+ dev: {
23
+ hmr: false,
24
+ liveReload: false,
25
+ },
26
+ };
27
+ ```
@@ -0,0 +1,69 @@
1
+ ---
2
+ sidebar_label: setupMiddlewares
3
+ ---
4
+
5
+ # dev.setupMiddlewares
6
+
7
+ - **类型:**
8
+
9
+ ```ts
10
+ type ServerAPIs = {
11
+ sockWrite: (
12
+ type: string,
13
+ data?: string | boolean | Record<string, any>,
14
+ ) => void;
15
+ };
16
+
17
+ type SetupMiddlewares = Array<
18
+ (
19
+ middlewares: {
20
+ unshift: (...handlers: RequestHandler[]) => void;
21
+ push: (...handlers: RequestHandler[]) => void;
22
+ },
23
+ server: ServerAPIs,
24
+ ) => void
25
+ >;
26
+ ```
27
+
28
+ - **默认值:** `undefined`
29
+
30
+ 提供执行自定义函数和应用自定义中间件的能力。
31
+
32
+ 中间件的执行顺序是: `unshift` => 内置中间件 => `push`。
33
+
34
+ ```js
35
+ export default {
36
+ dev: {
37
+ setupMiddlewares: [
38
+ (middlewares, server) => {
39
+ middlewares.unshift((req, res, next) => {
40
+ next();
41
+ });
42
+
43
+ middlewares.push((req, res, next) => {
44
+ next();
45
+ });
46
+ },
47
+ ],
48
+ },
49
+ };
50
+ ```
51
+
52
+ 一些特殊场景需求可能需要使用服务器 API:
53
+
54
+ - sockWrite。允许向 HMR 客户端传递一些消息,HMR 客户端将根据接收到的消息类型进行不同的处理。如果你发送一个 "content-changed " 的消息,页面将会重新加载。
55
+
56
+ ```js
57
+ export default {
58
+ dev: {
59
+ setupMiddlewares: [
60
+ (middlewares, server) => {
61
+ // 添加自定义 watcher 并在文件更新时触发页面刷新
62
+ watcher.on('change', (changed) => {
63
+ server.sockWrite('content-changed');
64
+ });
65
+ },
66
+ ],
67
+ },
68
+ };
69
+ ```
@@ -0,0 +1,59 @@
1
+ ---
2
+ sidebar_label: watchFiles
3
+ ---
4
+
5
+ # dev.watchFiles
6
+
7
+ - **类型:**
8
+
9
+ ```ts
10
+ type WatchFiles = {
11
+ paths: string | string[];
12
+ // chokidar 选项
13
+ options?: WatchOptions;
14
+ };
15
+ ```
16
+
17
+ - **默认值:** `undefined`
18
+
19
+ 监视指定文件和目录的变化。当文件发生变化时,页面将重新加载。
20
+
21
+ 如果 `dev.hmr` 和 `dev.liveReload` 都设置为 false,则 `watchFiles` 将被忽略。
22
+
23
+ :::tip
24
+ WatchFiles 中文件发生变化时,不会触发配置文件的重新加载及重新编译。
25
+ :::
26
+
27
+ ### 示例
28
+
29
+ 你可以配置一个 glob 模式 / 目录 / 文件的列表,用于监视文件变化。
30
+
31
+ ```js
32
+ export default {
33
+ dev: {
34
+ watchFiles: {
35
+ // 监视单个文件
36
+ paths: 'public/demo.txt',
37
+ // 使用 glob 模式
38
+ paths: 'src/**/*.txt',
39
+ // 监视多个文件路径
40
+ paths: ['src/**/*.txt', 'public/**/*'],
41
+ },
42
+ },
43
+ };
44
+ ```
45
+
46
+ 你也可以通过传入一个包含 `paths` 和 `options` 属性的对象,来指定 [chokidar](https://github.com/paulmillr/chokidar#api) 选项。
47
+
48
+ ```js
49
+ export default {
50
+ dev: {
51
+ watchFiles: {
52
+ paths: 'src/**/*.txt',
53
+ options: {
54
+ usePolling: false,
55
+ },
56
+ },
57
+ },
58
+ };
59
+ ```
@@ -0,0 +1,38 @@
1
+ ---
2
+ sidebar_label: writeToDisk
3
+ ---
4
+
5
+ # dev.writeToDisk
6
+
7
+ - **类型:** `boolean | ((filename: string) => boolean)`
8
+ - **默认值:** `(file: string) => !file.includes('.hot-update.')`
9
+
10
+ 用于控制是否将开发环境的构建产物写入到磁盘上。
11
+
12
+ ## 写入内存
13
+
14
+ 你可以选择将构建产物构建产物保存在 dev server 的内存中,从而减少文件操作产生的开销。
15
+
16
+ 只需要将 `dev.writeToDisk` 配置项设置为 `false` 即可:
17
+
18
+ ```ts
19
+ export default {
20
+ dev: {
21
+ writeToDisk: false,
22
+ },
23
+ };
24
+ ```
25
+
26
+ ## 匹配部分文件
27
+
28
+ 你也可以将 `dev.writeToDisk` 设置为函数来匹配一部分文件,函数返回 `false` 时不会写入文件,返回值 `true` 时会将文件写入磁盘。
29
+
30
+ 例如,Modern.js 会默认将文件写入磁盘,并排除热更新临时文件:
31
+
32
+ ```ts
33
+ export default {
34
+ dev: {
35
+ writeToDisk: (file) => !file.includes('.hot-update.'),
36
+ },
37
+ };
38
+ ```
@@ -247,7 +247,7 @@ export function UserLayout() {
247
247
  如果想获取 `entry1/routes/layout.tsx` 中 `loader` 返回的数据,`routeId` 的值就是 `entry1_layout`。
248
248
 
249
249
 
250
- ### (WIP)Loading UI
250
+ ### Loading UI (Experimental)
251
251
 
252
252
  :::info
253
253
  此功能目前是实验性质,后续 API 可能有调整。
@@ -296,7 +296,7 @@ Modern.js 会生成 `/login` 和 `/sign` 两条路由,`__auth/layout.tsx` 组
296
296
  </RootLayout>
297
297
  ```
298
298
 
299
- ### (WIP)Loading
299
+ ### Loading (Experimental)
300
300
 
301
301
  `routes/` 下每一层目录中,开发者可以创建 `loading.tsx` 文件,默认导出一个 `<Loading>` 组件。
302
302
 
package/package.json CHANGED
@@ -15,17 +15,17 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.55.0",
18
+ "version": "2.56.1",
19
19
  "publishConfig": {
20
20
  "registry": "https://registry.npmjs.org/",
21
21
  "access": "public",
22
22
  "provenance": true
23
23
  },
24
24
  "dependencies": {
25
- "@modern-js/sandpack-react": "2.55.0"
25
+ "@modern-js/sandpack-react": "2.56.1"
26
26
  },
27
27
  "peerDependencies": {
28
- "@modern-js/builder-doc": "^2.55.0"
28
+ "@modern-js/builder-doc": "^2.56.1"
29
29
  },
30
30
  "devDependencies": {
31
31
  "classnames": "^2",
@@ -35,12 +35,12 @@
35
35
  "ts-node": "^10.9.1",
36
36
  "typescript": "^5",
37
37
  "fs-extra": "^10",
38
- "rspress": "1.18.2",
39
- "@rspress/shared": "1.18.2",
38
+ "rspress": "1.26.1",
39
+ "@rspress/shared": "1.26.1",
40
40
  "@types/node": "^16",
41
41
  "@types/fs-extra": "9.0.13",
42
- "@modern-js/doc-plugin-auto-sidebar": "2.55.0",
43
- "@modern-js/builder-doc": "2.55.0"
42
+ "@modern-js/builder-doc": "2.56.1",
43
+ "@modern-js/doc-plugin-auto-sidebar": "2.56.1"
44
44
  },
45
45
  "scripts": {
46
46
  "dev": "rspress dev",