@modern-js/main-doc 3.9.2 → 3.10.0

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.
Files changed (203) hide show
  1. package/docs/en/apis/app/hooks/api/lambda.mdx +2 -1
  2. package/docs/en/apis/app/hooks/config/html.mdx +1 -0
  3. package/docs/en/apis/app/hooks/config/mock.mdx +1 -0
  4. package/docs/en/apis/app/hooks/config/public.mdx +1 -0
  5. package/docs/en/apis/app/hooks/config/upload.mdx +2 -3
  6. package/docs/en/apis/app/hooks/modern-config.mdx +1 -0
  7. package/docs/en/apis/app/hooks/shared.mdx +1 -0
  8. package/docs/en/apis/app/hooks/src/modern.runtime.mdx +1 -0
  9. package/docs/en/apis/app/hooks/src/routes.mdx +1 -0
  10. package/docs/en/apis/app/hooks/src/server.mdx +2 -1
  11. package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +1 -0
  12. package/docs/en/apis/app/runtime/core/create-root.mdx +4 -1
  13. package/docs/en/apis/app/runtime/core/render.mdx +6 -2
  14. package/docs/en/apis/app/runtime/core/runtime-context.mdx +17 -17
  15. package/docs/en/apis/app/runtime/router/router.mdx +9 -6
  16. package/docs/en/apis/app/runtime/ssr/no-ssr.mdx +1 -0
  17. package/docs/en/apis/app/runtime/utility/head.mdx +1 -0
  18. package/docs/en/apis/app/runtime/utility/loadable.mdx +1 -0
  19. package/docs/en/community/blog/v3-release-note.mdx +36 -27
  20. package/docs/en/community/contributing-guide.mdx +15 -33
  21. package/docs/en/components/default-browserslist.mdx +0 -1
  22. package/docs/en/components/deploy-command.mdx +1 -1
  23. package/docs/en/components/deploy.mdx +1 -1
  24. package/docs/en/components/entry-scan-logic.mdx +1 -1
  25. package/docs/en/components/init-app.mdx +9 -3
  26. package/docs/en/components/international/custom-instance-code.mdx +0 -1
  27. package/docs/en/components/micro-runtime-config.mdx +12 -9
  28. package/docs/en/components/nodeVersion.mdx +2 -2
  29. package/docs/en/components/rsc-deploy-tip.mdx +0 -1
  30. package/docs/en/components/ssr-monitor.mdx +1 -1
  31. package/docs/en/configure/app/bff/cross-project.mdx +2 -2
  32. package/docs/en/configure/app/bff/prefix.mdx +1 -1
  33. package/docs/en/configure/app/dev/lazy-compilation.mdx +17 -1
  34. package/docs/en/configure/app/dev/start-url.mdx +0 -1
  35. package/docs/en/configure/app/output/css-modules.mdx +1 -5
  36. package/docs/en/configure/app/output/disable-svgr.mdx +4 -0
  37. package/docs/en/configure/app/output/dist-path.mdx +0 -1
  38. package/docs/en/configure/app/output/inline-scripts.mdx +1 -2
  39. package/docs/en/configure/app/output/inline-styles.mdx +1 -2
  40. package/docs/en/configure/app/output/split-route-chunks.mdx +1 -1
  41. package/docs/en/configure/app/output/ssg.mdx +1 -1
  42. package/docs/en/configure/app/output/ssgByEntries.mdx +1 -0
  43. package/docs/en/configure/app/output/svg-default-export.mdx +4 -0
  44. package/docs/en/configure/app/performance/chunk-split.mdx +1 -4
  45. package/docs/en/configure/app/runtime/plugins.mdx +6 -4
  46. package/docs/en/configure/app/server/rsc.mdx +1 -1
  47. package/docs/en/configure/app/server/ssr.mdx +7 -8
  48. package/docs/en/configure/app/server/tsconfig-path.mdx +2 -2
  49. package/docs/en/configure/app/tools/less.mdx +94 -19
  50. package/docs/en/configure/app/tools/sass.mdx +87 -18
  51. package/docs/en/configure/app/tools/svgr.mdx +93 -0
  52. package/docs/en/configure/app/usage.mdx +1 -5
  53. package/docs/en/guides/advanced-features/bff/cross-project.mdx +5 -4
  54. package/docs/en/guides/advanced-features/compatibility.mdx +0 -1
  55. package/docs/en/guides/advanced-features/international/advanced.mdx +1 -1
  56. package/docs/en/guides/advanced-features/international/api.mdx +34 -28
  57. package/docs/en/guides/advanced-features/international/best-practices.mdx +15 -15
  58. package/docs/en/guides/advanced-features/international/configuration.mdx +37 -37
  59. package/docs/en/guides/advanced-features/international/locale-detection.mdx +37 -28
  60. package/docs/en/guides/advanced-features/international/resource-loading.mdx +19 -17
  61. package/docs/en/guides/advanced-features/international/routing.mdx +8 -6
  62. package/docs/en/guides/advanced-features/international.mdx +8 -8
  63. package/docs/en/guides/advanced-features/server-monitor/logger.mdx +5 -5
  64. package/docs/en/guides/advanced-features/server-monitor/metrics.mdx +8 -8
  65. package/docs/en/guides/advanced-features/server-monitor/monitors.mdx +1 -3
  66. package/docs/en/guides/advanced-features/source-build.mdx +1 -1
  67. package/docs/en/guides/advanced-features/web-server.mdx +3 -3
  68. package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -3
  69. package/docs/en/guides/basic-features/data/data-write.mdx +24 -28
  70. package/docs/en/guides/basic-features/debug/mock.mdx +2 -2
  71. package/docs/en/guides/basic-features/debug/using-storybook.mdx +5 -6
  72. package/docs/en/guides/basic-features/html.mdx +2 -3
  73. package/docs/en/guides/basic-features/output-files.mdx +1 -1
  74. package/docs/en/guides/basic-features/render/overview.mdx +12 -12
  75. package/docs/en/guides/basic-features/render/rsc.mdx +16 -9
  76. package/docs/en/guides/basic-features/render/ssr-cache.mdx +5 -5
  77. package/docs/en/guides/basic-features/render/ssr.mdx +8 -12
  78. package/docs/en/guides/basic-features/render/streaming-ssr.mdx +2 -1
  79. package/docs/en/guides/basic-features/routes/config-routes.mdx +60 -59
  80. package/docs/en/guides/basic-features/static-assets/svg-assets.mdx +29 -17
  81. package/docs/en/guides/basic-features/testing/playwright.mdx +14 -8
  82. package/docs/en/guides/basic-features/testing/rstest.mdx +6 -4
  83. package/docs/en/guides/get-started/ai-coding-agents.mdx +6 -6
  84. package/docs/en/guides/get-started/introduction.mdx +0 -1
  85. package/docs/en/guides/topic-detail/module-federation/deploy.mdx +2 -2
  86. package/docs/en/guides/topic-detail/module-federation/introduce.mdx +0 -1
  87. package/docs/en/guides/upgrade/config.mdx +12 -0
  88. package/docs/en/guides/upgrade/other.mdx +4 -4
  89. package/docs/en/guides/upgrade/tailwindcss.mdx +0 -1
  90. package/docs/en/guides/upgrade/web-server.md +34 -17
  91. package/docs/en/index.md +1 -0
  92. package/docs/en/plugin/cli-plugins/api.mdx +23 -23
  93. package/docs/en/plugin/cli-plugins/life-cycle.mdx +2 -0
  94. package/docs/en/plugin/introduction.mdx +6 -6
  95. package/docs/en/plugin/official/cli-plugins/plugin-bff.mdx +1 -1
  96. package/docs/en/plugin/official/cli-plugins/plugin-ssg.mdx +1 -1
  97. package/docs/en/plugin/plugin-system.mdx +19 -26
  98. package/docs/en/plugin/runtime-plugins/life-cycle.mdx +2 -0
  99. package/docs/en/plugin/server-plugins/api.mdx +11 -14
  100. package/docs/en/plugin/server-plugins/life-cycle.mdx +2 -0
  101. package/docs/zh/apis/app/hooks/api/lambda.mdx +1 -1
  102. package/docs/zh/apis/app/hooks/config/html.mdx +1 -0
  103. package/docs/zh/apis/app/hooks/config/mock.mdx +1 -0
  104. package/docs/zh/apis/app/hooks/config/public.mdx +1 -0
  105. package/docs/zh/apis/app/hooks/config/upload.mdx +2 -3
  106. package/docs/zh/apis/app/hooks/modern-config.mdx +1 -0
  107. package/docs/zh/apis/app/hooks/shared.mdx +1 -0
  108. package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +1 -0
  109. package/docs/zh/apis/app/hooks/src/routes.mdx +1 -0
  110. package/docs/zh/apis/app/hooks/src/server.mdx +2 -1
  111. package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +1 -1
  112. package/docs/zh/apis/app/runtime/core/create-root.mdx +4 -1
  113. package/docs/zh/apis/app/runtime/core/render.mdx +6 -3
  114. package/docs/zh/apis/app/runtime/core/runtime-context.mdx +17 -17
  115. package/docs/zh/apis/app/runtime/router/router.mdx +10 -6
  116. package/docs/zh/apis/app/runtime/ssr/no-ssr.mdx +1 -0
  117. package/docs/zh/apis/app/runtime/utility/head.mdx +1 -0
  118. package/docs/zh/apis/app/runtime/utility/loadable.mdx +1 -0
  119. package/docs/zh/community/blog/v3-release-note.mdx +38 -29
  120. package/docs/zh/community/contributing-guide.mdx +16 -34
  121. package/docs/zh/components/default-browserslist.mdx +0 -1
  122. package/docs/zh/components/deploy-command.mdx +1 -1
  123. package/docs/zh/components/deploy.mdx +1 -1
  124. package/docs/zh/components/entry-scan-logic.mdx +1 -1
  125. package/docs/zh/components/init-app.mdx +9 -3
  126. package/docs/zh/components/international/custom-instance-code.mdx +0 -1
  127. package/docs/zh/components/international/install-command.mdx +0 -1
  128. package/docs/zh/components/micro-runtime-config.mdx +12 -9
  129. package/docs/zh/components/nodeVersion.mdx +2 -2
  130. package/docs/zh/components/rsc-deploy-tip.mdx +0 -1
  131. package/docs/zh/components/ssr-monitor.mdx +1 -1
  132. package/docs/zh/configure/app/bff/cross-project.mdx +2 -2
  133. package/docs/zh/configure/app/bff/prefix.mdx +1 -1
  134. package/docs/zh/configure/app/dev/lazy-compilation.mdx +17 -1
  135. package/docs/zh/configure/app/html/crossorigin.mdx +0 -1
  136. package/docs/zh/configure/app/html/favicon.mdx +0 -1
  137. package/docs/zh/configure/app/output/convert-to-rem.mdx +0 -1
  138. package/docs/zh/configure/app/output/css-modules.mdx +1 -5
  139. package/docs/zh/configure/app/output/disable-svgr.mdx +4 -0
  140. package/docs/zh/configure/app/output/inline-scripts.mdx +1 -2
  141. package/docs/zh/configure/app/output/inline-styles.mdx +1 -2
  142. package/docs/zh/configure/app/output/source-map.mdx +0 -2
  143. package/docs/zh/configure/app/output/split-route-chunks.mdx +1 -1
  144. package/docs/zh/configure/app/output/ssg.mdx +1 -0
  145. package/docs/zh/configure/app/output/ssgByEntries.mdx +1 -1
  146. package/docs/zh/configure/app/output/svg-default-export.mdx +4 -0
  147. package/docs/zh/configure/app/performance/chunk-split.mdx +1 -4
  148. package/docs/zh/configure/app/resolve/dedupe.mdx +0 -1
  149. package/docs/zh/configure/app/runtime/plugins.mdx +6 -4
  150. package/docs/zh/configure/app/server/rsc.mdx +1 -1
  151. package/docs/zh/configure/app/server/ssr.mdx +6 -7
  152. package/docs/zh/configure/app/server/tsconfig-path.mdx +2 -2
  153. package/docs/zh/configure/app/tools/less.mdx +93 -19
  154. package/docs/zh/configure/app/tools/sass.mdx +87 -16
  155. package/docs/zh/configure/app/tools/svgr.mdx +93 -0
  156. package/docs/zh/configure/app/usage.mdx +1 -5
  157. package/docs/zh/guides/advanced-features/bff.mdx +1 -0
  158. package/docs/zh/guides/advanced-features/international/advanced.mdx +1 -1
  159. package/docs/zh/guides/advanced-features/international/api.mdx +28 -27
  160. package/docs/zh/guides/advanced-features/international/best-practices.mdx +15 -15
  161. package/docs/zh/guides/advanced-features/international/configuration.mdx +38 -36
  162. package/docs/zh/guides/advanced-features/international/locale-detection.mdx +37 -28
  163. package/docs/zh/guides/advanced-features/international/resource-loading.mdx +20 -16
  164. package/docs/zh/guides/advanced-features/international/routing.mdx +6 -4
  165. package/docs/zh/guides/advanced-features/international.mdx +9 -9
  166. package/docs/zh/guides/advanced-features/server-monitor/logger.mdx +4 -6
  167. package/docs/zh/guides/advanced-features/server-monitor/metrics.mdx +9 -10
  168. package/docs/zh/guides/advanced-features/source-build.mdx +1 -1
  169. package/docs/zh/guides/advanced-features/web-server.mdx +3 -3
  170. package/docs/zh/guides/basic-features/data/data-fetch.mdx +1 -3
  171. package/docs/zh/guides/basic-features/data/data-write.mdx +25 -31
  172. package/docs/zh/guides/basic-features/debug/mock.mdx +2 -2
  173. package/docs/zh/guides/basic-features/debug/using-storybook.mdx +5 -5
  174. package/docs/zh/guides/basic-features/env-vars.mdx +0 -1
  175. package/docs/zh/guides/basic-features/html.mdx +2 -3
  176. package/docs/zh/guides/basic-features/render/overview.mdx +12 -13
  177. package/docs/zh/guides/basic-features/render/rsc.mdx +16 -9
  178. package/docs/zh/guides/basic-features/render/ssr.mdx +8 -17
  179. package/docs/zh/guides/basic-features/routes/config-routes.mdx +61 -59
  180. package/docs/zh/guides/basic-features/static-assets/svg-assets.mdx +29 -17
  181. package/docs/zh/guides/basic-features/testing/playwright.mdx +15 -10
  182. package/docs/zh/guides/basic-features/testing/rstest.mdx +6 -4
  183. package/docs/zh/guides/get-started/ai-coding-agents.mdx +7 -7
  184. package/docs/zh/guides/topic-detail/module-federation/deploy.mdx +2 -2
  185. package/docs/zh/guides/upgrade/config.mdx +12 -0
  186. package/docs/zh/guides/upgrade/other.md +4 -11
  187. package/docs/zh/guides/upgrade/overview.mdx +1 -4
  188. package/docs/zh/guides/upgrade/tailwindcss.mdx +0 -1
  189. package/docs/zh/guides/upgrade/web-server.md +34 -19
  190. package/docs/zh/index.md +1 -0
  191. package/docs/zh/plugin/cli-plugins/api.mdx +24 -24
  192. package/docs/zh/plugin/cli-plugins/life-cycle.mdx +2 -0
  193. package/docs/zh/plugin/introduction.mdx +6 -6
  194. package/docs/zh/plugin/official/cli-plugins/plugin-bff.mdx +1 -1
  195. package/docs/zh/plugin/official/cli-plugins/plugin-ssg.mdx +1 -1
  196. package/docs/zh/plugin/plugin-system.mdx +19 -26
  197. package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +2 -0
  198. package/docs/zh/plugin/server-plugins/api.mdx +11 -14
  199. package/docs/zh/plugin/server-plugins/life-cycle.mdx +2 -0
  200. package/package.json +11 -13
  201. package/{rspress.config.ts → rstack.config.mts} +7 -10
  202. package/src/components/Footer/styles.module.scss +1 -1
  203. package/src/components/SolutionCards/index.module.scss +3 -1
@@ -40,9 +40,9 @@ title: tsconfigPath
40
40
  "types": ["node"],
41
41
  "esModuleInterop": true,
42
42
  "experimentalDecorators": true,
43
- "emitDecoratorMetadata": true
43
+ "emitDecoratorMetadata": true,
44
44
  },
45
- "include": ["api", "shared", "server", "config", "modern.config.ts"]
45
+ "include": ["api", "shared", "server", "config", "modern.config.ts"],
46
46
  }
47
47
  ```
48
48
 
@@ -9,49 +9,78 @@ title: less
9
9
 
10
10
  ```js
11
11
  const defaultOptions = {
12
- lessOptions: {
13
- javascriptEnabled: true,
12
+ lessLoaderOptions: {
13
+ lessOptions: {
14
+ javascriptEnabled: true,
15
+ },
16
+ // 默认在开发环境下启用 CSS 的 Source Map
17
+ sourceMap: isDev,
14
18
  },
15
- // 默认在开发环境下启用 CSS 的 Source Map
16
- sourceMap: isDev,
17
19
  };
18
20
  ```
19
21
 
20
- 你可以通过 `tools.less` 修改 [less-loader](https://github.com/webpack-contrib/less-loader) 的配置。
22
+ `tools.less` 用于修改 [@rsbuild/plugin-less](https://rsbuild.rs/plugins/list/plugin-less) 的选项,取值为该插件的完整选项:
23
+
24
+ | 选项 | 说明 |
25
+ | ------------------- | ----------------------------------------------------------------------------------------- |
26
+ | `lessLoaderOptions` | 传给 [less-loader](https://github.com/webpack-contrib/less-loader) 的选项,支持对象或函数 |
27
+ | `include` | 指定哪些文件交给 less-loader 处理,默认 `/\.less$/` |
28
+ | `exclude` | 指定哪些文件不交给 less-loader 处理 |
29
+ | `parallel` | 是否使用 worker 线程并行编译 Less,默认 `false` |
30
+
31
+ ### 修改 less-loader 选项
21
32
 
22
- ### Object 类型
33
+ `lessLoaderOptions` 为 `Object` 类型时,会与默认配置通过 Object.assign 进行浅层合并,值得注意的是,`lessOptions` 会通过 deepMerge 进行深层合并。
34
+
35
+ ```js
36
+ export default {
37
+ tools: {
38
+ less: {
39
+ lessLoaderOptions: {
40
+ lessOptions: {
41
+ javascriptEnabled: false,
42
+ },
43
+ },
44
+ },
45
+ },
46
+ };
47
+ ```
23
48
 
24
- 当 `tools.less` 的值为 `Object` 类型时,会与默认配置通过 Object.assign 进行浅层合并,值得注意的是,`lessOptions` 会通过 deepMerge 进行深层合并。
49
+ `lessLoaderOptions` 为 `Function` 类型时,默认配置作为第一个参数传入,可以直接修改配置对象,也可以返回一个值作为最终结果,第二个参数提供了一些可以直接调用的工具函数:
25
50
 
26
51
  ```js
27
52
  export default {
28
53
  tools: {
29
54
  less: {
30
- lessOptions: {
31
- javascriptEnabled: false,
55
+ lessLoaderOptions(config) {
56
+ // 修改 lessOptions 配置
57
+ config.lessOptions = {
58
+ javascriptEnabled: false,
59
+ };
32
60
  },
33
61
  },
34
62
  },
35
63
  };
36
64
  ```
37
65
 
38
- ### Function 类型
66
+ ### 并行编译
39
67
 
40
- 当 `tools.less` 为 Function 类型时,默认配置作为第一个参数传入,可以直接修改配置对象,也可以返回一个值作为最终结果,第二个参数提供了一些可以直接调用的工具函数:
68
+ Less 编译是纯 JavaScript 计算,默认只占用 Node.js 主线程。开启 `parallel` 后,Less 模块会分发到 worker 线程池中编译,项目里 Less 文件较多时可以缩短构建时间。
41
69
 
42
70
  ```js
43
71
  export default {
44
72
  tools: {
45
- less(config) {
46
- // 修改 lessOptions 配置
47
- config.lessOptions = {
48
- javascriptEnabled: false,
49
- };
73
+ less: {
74
+ parallel: true,
50
75
  },
51
76
  },
52
77
  };
53
78
  ```
54
79
 
80
+ :::tip
81
+ 传给 worker 线程的选项需要满足[结构化克隆算法](https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)的要求,因此开启 `parallel` 后,`lessLoaderOptions` 中不能包含函数,例如 `additionalData` 函数或自定义的 `implementation`。
82
+ :::
83
+
55
84
  ### 修改 Less 版本
56
85
 
57
86
  在某些场景下,如果你需要使用特定的 Less 版本,而不是使用 Modern.js 内置的 Less v4,可以在项目中安装需要使用的 Less 版本,并通过 `less-loader` 的 `implementation` 选项设置。
@@ -60,7 +89,9 @@ export default {
60
89
  export default {
61
90
  tools: {
62
91
  less: {
63
- implementation: require('less'),
92
+ lessLoaderOptions: {
93
+ implementation: require('less'),
94
+ },
64
95
  },
65
96
  },
66
97
  };
@@ -77,10 +108,53 @@ export default {
77
108
  ```js
78
109
  export default {
79
110
  tools: {
80
- less(config, { addExcludes }) {
81
- addExcludes(/node_modules/);
111
+ less: {
112
+ lessLoaderOptions(config, { addExcludes }) {
113
+ addExcludes(/node_modules/);
114
+ },
82
115
  },
83
116
  },
84
117
  };
85
118
  ```
86
119
 
120
+ 推荐直接使用插件的 `exclude` 选项,效果相同:
121
+
122
+ ```js
123
+ export default {
124
+ tools: {
125
+ less: {
126
+ exclude: /node_modules/,
127
+ },
128
+ },
129
+ };
130
+ ```
131
+
132
+ ### 兼容旧写法
133
+
134
+ :::warning 不要混写两种层级
135
+ 同一个对象里只要出现插件顶层键(`lessLoaderOptions`、`include`、`exclude` 等),整个对象就会按插件选项解析,其中的 loader 选项不会生效。例如 `tools.less: { parallel: true, lessOptions: { ... } }` 里的 `lessOptions` 会被忽略,必须移到 `lessLoaderOptions` 下;开发环境下会打印一条提示。
136
+ :::
137
+
138
+ 在之前的版本中,`tools.less` 的取值直接是 less-loader 的选项,例如 `tools.less: { lessOptions: {} }` 或 `tools.less(config, { addExcludes }) {}`。这种写法目前仍然生效,Modern.js 会自动把它包装为 `lessLoaderOptions`,产出的配置与之前完全一致;在开发环境下会打印一条迁移提示,并将在下一个大版本中移除。
139
+
140
+ ```js
141
+ // 旧写法
142
+ export default {
143
+ tools: {
144
+ less: {
145
+ lessOptions: { javascriptEnabled: false },
146
+ },
147
+ },
148
+ };
149
+
150
+ // 新写法
151
+ export default {
152
+ tools: {
153
+ less: {
154
+ lessLoaderOptions: {
155
+ lessOptions: { javascriptEnabled: false },
156
+ },
157
+ },
158
+ },
159
+ };
160
+ ```
@@ -9,39 +9,64 @@ title: sass
9
9
 
10
10
  ```js
11
11
  const defaultOptions = {
12
- // 默认在开发环境下启用 CSS 的 Source Map
13
- sourceMap: isDev,
12
+ sassLoaderOptions: {
13
+ // 默认在开发环境下启用 CSS 的 Source Map
14
+ sourceMap: isDev,
15
+ },
14
16
  };
15
17
  ```
16
18
 
17
- 你可以通过 `tools.sass` 修改 [sass-loader](https://github.com/webpack-contrib/sass-loader) 的配置。
19
+ `tools.sass` 用于修改 [@rsbuild/plugin-sass](https://rsbuild.rs/plugins/list/plugin-sass) 的选项,取值为该插件的完整选项:
20
+
21
+ | 选项 | 说明 |
22
+ | ------------------- | ----------------------------------------------------------------------------------------- |
23
+ | `sassLoaderOptions` | 传给 [sass-loader](https://github.com/webpack-contrib/sass-loader) 的选项,支持对象或函数 |
24
+ | `include` | 指定哪些文件交给 sass-loader 处理,默认 `/\.s(?:a\|c)ss$/` |
25
+ | `exclude` | 指定哪些文件不交给 sass-loader 处理 |
26
+ | `rewriteUrls` | 是否使用 resolve-url-loader 重写 Sass 文件中的相对 URL,默认 `true` |
18
27
 
19
- ### Object 类型
28
+ ### 修改 sass-loader 选项
20
29
 
21
- 当 `tools.sass` 的值为 `Object` 类型时,会与默认配置通过 Object.assign 进行浅层合并,值得注意的是,`sassOptions` 会通过 deepMerge 进行深层合并。
30
+ `sassLoaderOptions` 为 `Object` 类型时,会与默认配置通过 Object.assign 进行浅层合并,值得注意的是,`sassOptions` 会通过 deepMerge 进行深层合并。
22
31
 
23
32
  ```js
24
33
  export default {
25
34
  tools: {
26
35
  sass: {
27
- sourceMap: true,
36
+ sassLoaderOptions: {
37
+ sourceMap: true,
38
+ },
28
39
  },
29
40
  },
30
41
  };
31
42
  ```
32
43
 
33
- ### Function 类型
44
+ `sassLoaderOptions` 为 `Function` 类型时,默认配置作为第一个参数传入,可以直接修改配置对象,也可以返回一个值作为最终结果,第二个参数提供了一些可以直接调用的工具函数:
45
+
46
+ ```js
47
+ export default {
48
+ tools: {
49
+ sass: {
50
+ sassLoaderOptions(config) {
51
+ // 修改 additionalData 配置
52
+ config.additionalData = async (content, loaderContext) => {
53
+ // ...
54
+ };
55
+ },
56
+ },
57
+ },
58
+ };
59
+ ```
34
60
 
35
- 当 `tools.sass` 为 Function 类型时,默认配置作为第一个参数传入,可以直接修改配置对象,也可以返回一个值作为最终结果,第二个参数提供了一些可以直接调用的工具函数:
61
+ ### 关闭 URL 重写
62
+
63
+ 默认情况下,Sass 文件里的相对 URL 会经过 resolve-url-loader 重写为相对于源文件的路径。如果项目不依赖这一行为,可以关闭它来减少一个 loader:
36
64
 
37
65
  ```js
38
66
  export default {
39
67
  tools: {
40
- sass(config) {
41
- // 修改 sourceMap 配置
42
- config.additionalData = async (content, loaderContext) => {
43
- // ...
44
- };
68
+ sass: {
69
+ rewriteUrls: false,
45
70
  },
46
71
  },
47
72
  };
@@ -55,7 +80,9 @@ export default {
55
80
  export default {
56
81
  tools: {
57
82
  sass: {
58
- implementation: require('sass'),
83
+ sassLoaderOptions: {
84
+ implementation: require('sass'),
85
+ },
59
86
  },
60
87
  },
61
88
  };
@@ -72,8 +99,52 @@ export default {
72
99
  ```js
73
100
  export default {
74
101
  tools: {
75
- sass(config, { addExcludes }) {
76
- addExcludes(/node_modules/);
102
+ sass: {
103
+ sassLoaderOptions(config, { addExcludes }) {
104
+ addExcludes(/node_modules/);
105
+ },
106
+ },
107
+ },
108
+ };
109
+ ```
110
+
111
+ 推荐直接使用插件的 `exclude` 选项,效果相同:
112
+
113
+ ```js
114
+ export default {
115
+ tools: {
116
+ sass: {
117
+ exclude: /node_modules/,
118
+ },
119
+ },
120
+ };
121
+ ```
122
+
123
+ ### 兼容旧写法
124
+
125
+ :::warning 不要混写两种层级
126
+ 同一个对象里只要出现插件顶层键(`sassLoaderOptions`、`include`、`exclude` 等),整个对象就会按插件选项解析,其中的 loader 选项不会生效。例如 `tools.sass: { rewriteUrls: false, sassOptions: { ... } }` 里的 `sassOptions` 会被忽略,必须移到 `sassLoaderOptions` 下;开发环境下会打印一条提示。
127
+ :::
128
+
129
+ 在之前的版本中,`tools.sass` 的取值直接是 sass-loader 的选项,例如 `tools.sass: { sassOptions: {} }` 或 `tools.sass(config, { addExcludes }) {}`。这种写法目前仍然生效,Modern.js 会自动把它包装为 `sassLoaderOptions`,产出的配置与之前完全一致;在开发环境下会打印一条迁移提示,并将在下一个大版本中移除。
130
+
131
+ ```js
132
+ // 旧写法
133
+ export default {
134
+ tools: {
135
+ sass: {
136
+ sourceMap: true,
137
+ },
138
+ },
139
+ };
140
+
141
+ // 新写法
142
+ export default {
143
+ tools: {
144
+ sass: {
145
+ sassLoaderOptions: {
146
+ sourceMap: true,
147
+ },
77
148
  },
78
149
  },
79
150
  };
@@ -0,0 +1,93 @@
1
+ ---
2
+ title: svgr
3
+ ---
4
+
5
+ # tools.svgr
6
+
7
+ - **类型:** `Object | Function | false`
8
+ - **默认值:**
9
+
10
+ ```js
11
+ const defaultOptions = {
12
+ mixedImport: true,
13
+ svgrOptions: {
14
+ exportType: 'named',
15
+ },
16
+ };
17
+ ```
18
+
19
+ `tools.svgr` 用于修改 [@rsbuild/plugin-svgr](https://rsbuild.rs/plugins/list/plugin-svgr) 的选项,取值为该插件的完整选项。设置为 `false` 时不再注册 SVGR 插件,所有 `.svg` 文件按静态资源处理。
20
+
21
+ | 选项 | 说明 |
22
+ | ----------------- | ------------------------------------------------------------------------------------------------- |
23
+ | `svgrOptions` | 传给 [SVGR](https://react-svgr.com/docs/options/) 的参数,例如 `exportType`、`icon`、`svgoConfig` |
24
+ | `parallel` | 是否使用 worker 线程并行转换 SVG,默认 `false` |
25
+ | `exclude` | 指定哪些 SVG 文件不交给 SVGR 处理 |
26
+ | `excludeImporter` | 指定从哪些文件导入 SVG 时不交给 SVGR 处理 |
27
+ | `query` | 指定触发 SVGR 转换的 query,默认 `/react/` |
28
+ | `mixedImport` | 是否允许在同一个模块中同时导入 URL 与 React 组件,默认 `true` |
29
+
30
+ ### 合并规则
31
+
32
+ `tools.svgr` 为 `Object` 类型时,顶层字段与默认配置通过 Object.assign 进行浅层合并,`svgrOptions` 会再合并一层,因此只设置 `svgrOptions.icon` 不会丢掉默认的 `exportType`。
33
+
34
+ `tools.svgr` 为 `Function` 类型时,默认配置作为第一个参数传入,可以直接修改配置对象,也可以返回一个值作为最终结果。也支持传入由对象和函数组成的数组,按顺序依次应用。
35
+
36
+ ### 并行转换
37
+
38
+ SVGR 转换是纯 JavaScript 计算,默认只占用 Node.js 主线程。项目里 SVG 文件较多时,开启 `parallel` 可以把转换分发到 worker 线程池:
39
+
40
+ ```js
41
+ export default {
42
+ tools: {
43
+ svgr: {
44
+ parallel: true,
45
+ },
46
+ },
47
+ };
48
+ ```
49
+
50
+ :::tip
51
+ 传给 worker 线程的选项需要满足[结构化克隆算法](https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)的要求,因此开启 `parallel` 后,`svgrOptions` 中不能包含函数。
52
+ :::
53
+
54
+ ### 修改默认导出
55
+
56
+ `svgrOptions.exportType` 决定 SVG 文件默认导出的内容:`'named'` 表示默认导出是文件 URL,React 组件通过 `ReactComponent` 具名导出;`'default'` 表示默认导出就是 React 组件。
57
+
58
+ ```js
59
+ export default {
60
+ tools: {
61
+ svgr: {
62
+ svgrOptions: {
63
+ exportType: 'default',
64
+ },
65
+ },
66
+ },
67
+ };
68
+ ```
69
+
70
+ ### 关闭 SVGR
71
+
72
+ 如果你确定项目内的所有 SVG 资源都没有当成 React 组件使用,可以关闭 SVGR 以提升构建性能:
73
+
74
+ ```js
75
+ export default {
76
+ tools: {
77
+ svgr: false,
78
+ },
79
+ };
80
+ ```
81
+
82
+ ### 与旧配置的关系
83
+
84
+ `tools.svgr` 取代了下面两个已废弃的配置项,二者目前仍然生效,并将在下一个大版本中移除:
85
+
86
+ | 旧配置 | 新配置 |
87
+ | -------------------------------------- | -------------------------------------------------------- |
88
+ | `output.svgDefaultExport: 'component'` | `tools.svgr: { svgrOptions: { exportType: 'default' } }` |
89
+ | `output.svgDefaultExport: 'url'` | `tools.svgr: { svgrOptions: { exportType: 'named' } }` |
90
+ | `output.disableSvgr: true` | `tools.svgr: false` |
91
+ | `output.disableSvgr: false` | 默认行为,直接删除即可 |
92
+
93
+ 同时设置时以 `tools.svgr` 为准:显式设置了 `tools.svgr` 后,`output.disableSvgr` 不再生效;`tools.svgr.svgrOptions.exportType` 会覆盖 `output.svgDefaultExport` 派生的值。
@@ -265,11 +265,7 @@ import { defineConfig } from '@modern-js/app-tools';
265
265
  import { xxPlugin } from '@modern-js/plugin-xx';
266
266
 
267
267
  export default defineConfig({
268
- plugins: [
269
- xxPlugin({
270
- /** 在此处添加参数 */
271
- }),
272
- ],
268
+ plugins: [xxPlugin({/** 在此处添加参数 */})],
273
269
  });
274
270
  ```
275
271
 
@@ -1,6 +1,7 @@
1
1
  ---
2
2
  title: BFF
3
3
  ---
4
+
4
5
  # BFF
5
6
 
6
7
  BFF(Backends for Frontends)是一种架构模式,主要用于解决前后端协作中的数据聚合问题。在 BFF 架构下,前端应用程序不直接与后端服务通信,而是通过一个专门为前端定制的BFF中间层与后端服务交互。
@@ -68,7 +68,7 @@ i18nPlugin({
68
68
  localeDetectionByEntry: {
69
69
  admin: {
70
70
  localePathRedirect: false, // admin 入口不使用路径前缀
71
- languages: ['en'], // admin 只支持英文
71
+ languages: ['en'], // admin 只支持英文
72
72
  },
73
73
  mobile: {
74
74
  languages: ['zh', 'en', 'ja'], // mobile 支持更多语言
@@ -10,14 +10,14 @@ title: API 参考
10
10
 
11
11
  ### 返回值
12
12
 
13
- | 字段 | 类型 | 说明 |
14
- | --- | --- | --- |
15
- | `language` | `string` | 当前语言代码 |
16
- | `changeLanguage` | `(lang: string) => Promise<void>` | 切换语言 |
17
- | `supportedLanguages` | `string[]` | 支持的语言列表(来自 `localeDetection.languages`) |
18
- | `isLanguageSupported` | `(lang: string) => boolean` | 检查语言是否在支持列表中 |
19
- | `isResourcesReady` | `boolean` | 当前语言的翻译资源是否已加载完成 |
20
- | `i18nInstance` | `I18nInstance` | i18next 实例(用于高级场景) |
13
+ | 字段 | 类型 | 说明 |
14
+ | --------------------- | --------------------------------- | -------------------------------------------------- |
15
+ | `language` | `string` | 当前语言代码 |
16
+ | `changeLanguage` | `(lang: string) => Promise<void>` | 切换语言 |
17
+ | `supportedLanguages` | `string[]` | 支持的语言列表(来自 `localeDetection.languages`) |
18
+ | `isLanguageSupported` | `(lang: string) => boolean` | 检查语言是否在支持列表中 |
19
+ | `isResourcesReady` | `boolean` | 当前语言的翻译资源是否已加载完成 |
20
+ | `i18nInstance` | `I18nInstance` | i18next 实例(用于高级场景) |
21
21
 
22
22
  ### 基本用法
23
23
 
@@ -85,13 +85,13 @@ function MyComponent() {
85
85
 
86
86
  ### Props
87
87
 
88
- | Prop | 类型 | 必填 | 说明 |
89
- | --- | --- | --- | --- |
90
- | `to` | `string` | 是 | 目标路径,不需要包含语言前缀 |
91
- | `children` | `React.ReactNode` | 是 | 链接内容 |
92
- | `replace` | `boolean` | 否 | 使用 `history.replace` 而非 `push` |
93
- | `state` | `any` | 否 | 传递给目标路由的状态 |
94
- | 其他 Link props | — | 否 | 继承自 `@modern-js/runtime/router` 的 `Link` 组件 |
88
+ | Prop | 类型 | 必填 | 说明 |
89
+ | --------------- | ----------------- | ---- | ------------------------------------------------- |
90
+ | `to` | `string` | 是 | 目标路径,不需要包含语言前缀 |
91
+ | `children` | `React.ReactNode` | 是 | 链接内容 |
92
+ | `replace` | `boolean` | 否 | 使用 `history.replace` 而非 `push` |
93
+ | `state` | `any` | 否 | 传递给目标路由的状态 |
94
+ | 其他 Link props | — | 否 | 继承自 `@modern-js/runtime/router` 的 `Link` 组件 |
95
95
 
96
96
  ### 用法
97
97
 
@@ -132,6 +132,7 @@ const myPlugin = (): RuntimePlugin => ({
132
132
  ```
133
133
 
134
134
  **注意事项:**
135
+
135
136
  - 确保 i18n 插件在使用 `context.changeLanguage` 的插件之前注册
136
137
  - `changeLanguage` 在服务端不会更新 URL 路径,需要配合路由插件或手动处理
137
138
 
@@ -165,11 +166,11 @@ interface I18nInstance {
165
166
  type I18nSdkLoader = (options: I18nSdkLoadOptions) => Promise<Resources>;
166
167
 
167
168
  interface I18nSdkLoadOptions {
168
- lng?: string; // 单个语言代码
169
- ns?: string; // 单个命名空间
170
- lngs?: string[]; // 多个语言代码
171
- nss?: string[]; // 多个命名空间
172
- all?: boolean; // 加载所有资源
169
+ lng?: string; // 单个语言代码
170
+ ns?: string; // 单个命名空间
171
+ lngs?: string[]; // 多个语言代码
172
+ nss?: string[]; // 多个命名空间
173
+ all?: boolean; // 加载所有资源
173
174
  }
174
175
 
175
176
  type Resources = {
@@ -184,14 +185,14 @@ type Resources = {
184
185
  ```ts
185
186
  interface LanguageDetectorOptions {
186
187
  order?: string[];
187
- lookupQuerystring?: string; // 默认 'lng'
188
- lookupCookie?: string; // 默认 'i18next'
189
- lookupLocalStorage?: string; // 默认 'i18nextLng'
188
+ lookupQuerystring?: string; // 默认 'lng'
189
+ lookupCookie?: string; // 默认 'i18next'
190
+ lookupLocalStorage?: string; // 默认 'i18nextLng'
190
191
  lookupSession?: string;
191
- lookupHeader?: string; // 默认 'accept-language'
192
+ lookupHeader?: string; // 默认 'accept-language'
192
193
  lookupFromPathIndex?: number; // 默认 0
193
194
  caches?: false | string[];
194
- cookieMinutes?: number; // 默认 525600(1年)
195
+ cookieMinutes?: number; // 默认 525600(1年)
195
196
  cookieExpirationDate?: Date;
196
197
  cookieDomain?: string;
197
198
  }
@@ -211,8 +212,8 @@ import { useTranslation } from 'react-i18next';
211
212
  import { useModernI18n } from '@modern-js/plugin-i18n/runtime';
212
213
 
213
214
  function App() {
214
- const { t } = useTranslation(); // 翻译文本
215
- const { language, changeLanguage } = useModernI18n(); // 语言切换
215
+ const { t } = useTranslation(); // 翻译文本
216
+ const { language, changeLanguage } = useModernI18n(); // 语言切换
216
217
 
217
218
  return (
218
219
  <div>
@@ -63,8 +63,8 @@ function ButtonGroup() {
63
63
  const { t } = useTranslation('common', { keyPrefix: 'button' });
64
64
  return (
65
65
  <>
66
- <button>{t('submit')}</button> {/* → common:button.submit */}
67
- <button>{t('cancel')}</button> {/* → common:button.cancel */}
66
+ <button>{t('submit')}</button> {/* → common:button.submit */}
67
+ <button>{t('cancel')}</button> {/* → common:button.cancel */}
68
68
  </>
69
69
  );
70
70
  }
@@ -94,9 +94,9 @@ function ButtonGroup() {
94
94
  ```
95
95
 
96
96
  ```tsx
97
- t('page.title')
98
- t('form.username.label')
99
- t('form.submit', { defaultValue: '保存' }) // defaultValue 防止键缺失时显示 key 字符串
97
+ t('page.title');
98
+ t('form.username.label');
99
+ t('form.submit', { defaultValue: '保存' }); // defaultValue 防止键缺失时显示 key 字符串
100
100
  ```
101
101
 
102
102
  ## 复数
@@ -121,9 +121,9 @@ i18next 根据 `count` 参数自动选择对应的复数形式。**注意:i18n
121
121
  ```
122
122
 
123
123
  ```tsx
124
- t('item', { count: 0 }) // → "没有条目" / "No items"
125
- t('item', { count: 1 }) // → "没有条目" / "1 item"(中文通常只有一种形式)
126
- t('item', { count: 5 }) // → "5 个条目" / "5 items"
124
+ t('item', { count: 0 }); // → "没有条目" / "No items"
125
+ t('item', { count: 1 }); // → "没有条目" / "1 item"(中文通常只有一种形式)
126
+ t('item', { count: 5 }); // → "5 个条目" / "5 items"
127
127
  ```
128
128
 
129
129
  不同语言的复数规则不同(英语有单复数,俄语有 one/few/many 等多种形式),i18next 会根据语言代码自动匹配 CLDR 规则。
@@ -152,8 +152,8 @@ t('item', { count: 5 }) // → "5 个条目" / "5 items"
152
152
  ```
153
153
 
154
154
  ```tsx
155
- t('modal.confirm.title')
156
- t('modal.confirm.actions.ok')
155
+ t('modal.confirm.title');
156
+ t('modal.confirm.actions.ok');
157
157
  ```
158
158
 
159
159
  嵌套层级不建议超过 4 层,过深的结构会让键名变得冗长难维护。
@@ -199,8 +199,8 @@ export default defineRuntimeConfig({
199
199
  ```
200
200
 
201
201
  ```tsx
202
- t('price', { value: 99.5 }) // → "当前价格:¥99.50"(zh)/ "$99.50"(en)
203
- t('expiry', { date: new Date() }) // → "到期日:2025年5月12日"(zh)
202
+ t('price', { value: 99.5 }); // → "当前价格:¥99.50"(zh)/ "$99.50"(en)
203
+ t('expiry', { date: new Date() }); // → "到期日:2025年5月12日"(zh)
204
204
  ```
205
205
 
206
206
  ## 错误处理
@@ -237,7 +237,7 @@ if (!i18n.isInitialized) return null;
237
237
  通过 `defaultValue` 提供兜底文案,防止界面显示 key 字符串:
238
238
 
239
239
  ```tsx
240
- t('missing.key', { defaultValue: '默认文案' })
240
+ t('missing.key', { defaultValue: '默认文案' });
241
241
  ```
242
242
 
243
243
  开发环境可以开启 `saveMissing`,将缺失的键输出到控制台方便排查:
@@ -281,8 +281,8 @@ declare module 'react-i18next' {
281
281
 
282
282
  ```tsx
283
283
  const { t } = useTranslation();
284
- t('welcome'); // ✅ TypeScript 自动补全
285
- t('nonExistent'); // ❌ TypeScript 报错
284
+ t('welcome'); // ✅ TypeScript 自动补全
285
+ t('nonExistent'); // ❌ TypeScript 报错
286
286
  ```
287
287
 
288
288
  :::tip 大型项目