@modern-js/main-doc 3.9.3 → 3.10.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.
Files changed (197) 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/dist-path.mdx +0 -1
  37. package/docs/en/configure/app/output/inline-scripts.mdx +1 -2
  38. package/docs/en/configure/app/output/inline-styles.mdx +1 -2
  39. package/docs/en/configure/app/output/split-route-chunks.mdx +1 -1
  40. package/docs/en/configure/app/output/ssg.mdx +1 -1
  41. package/docs/en/configure/app/output/ssgByEntries.mdx +1 -0
  42. package/docs/en/configure/app/performance/chunk-split.mdx +1 -4
  43. package/docs/en/configure/app/runtime/plugins.mdx +6 -4
  44. package/docs/en/configure/app/server/rsc.mdx +1 -1
  45. package/docs/en/configure/app/server/ssr.mdx +7 -8
  46. package/docs/en/configure/app/server/tsconfig-path.mdx +2 -2
  47. package/docs/en/configure/app/tools/less.mdx +5 -6
  48. package/docs/en/configure/app/tools/sass.mdx +5 -6
  49. package/docs/en/configure/app/tools/svgr.mdx +7 -7
  50. package/docs/en/configure/app/usage.mdx +1 -5
  51. package/docs/en/guides/advanced-features/bff/cross-project.mdx +5 -4
  52. package/docs/en/guides/advanced-features/compatibility.mdx +0 -1
  53. package/docs/en/guides/advanced-features/international/advanced.mdx +1 -1
  54. package/docs/en/guides/advanced-features/international/api.mdx +34 -28
  55. package/docs/en/guides/advanced-features/international/best-practices.mdx +15 -15
  56. package/docs/en/guides/advanced-features/international/configuration.mdx +37 -37
  57. package/docs/en/guides/advanced-features/international/locale-detection.mdx +37 -28
  58. package/docs/en/guides/advanced-features/international/resource-loading.mdx +19 -17
  59. package/docs/en/guides/advanced-features/international/routing.mdx +8 -6
  60. package/docs/en/guides/advanced-features/international.mdx +8 -8
  61. package/docs/en/guides/advanced-features/server-monitor/logger.mdx +5 -5
  62. package/docs/en/guides/advanced-features/server-monitor/metrics.mdx +8 -8
  63. package/docs/en/guides/advanced-features/server-monitor/monitors.mdx +1 -3
  64. package/docs/en/guides/advanced-features/source-build.mdx +1 -1
  65. package/docs/en/guides/advanced-features/web-server.mdx +3 -3
  66. package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -3
  67. package/docs/en/guides/basic-features/data/data-write.mdx +24 -28
  68. package/docs/en/guides/basic-features/debug/mock.mdx +2 -2
  69. package/docs/en/guides/basic-features/debug/using-storybook.mdx +5 -6
  70. package/docs/en/guides/basic-features/html.mdx +2 -3
  71. package/docs/en/guides/basic-features/output-files.mdx +1 -1
  72. package/docs/en/guides/basic-features/render/overview.mdx +12 -12
  73. package/docs/en/guides/basic-features/render/rsc.mdx +16 -9
  74. package/docs/en/guides/basic-features/render/ssr-cache.mdx +5 -5
  75. package/docs/en/guides/basic-features/render/ssr.mdx +8 -12
  76. package/docs/en/guides/basic-features/render/streaming-ssr.mdx +2 -1
  77. package/docs/en/guides/basic-features/routes/config-routes.mdx +60 -59
  78. package/docs/en/guides/basic-features/testing/playwright.mdx +14 -8
  79. package/docs/en/guides/basic-features/testing/rstest.mdx +6 -4
  80. package/docs/en/guides/get-started/ai-coding-agents.mdx +6 -6
  81. package/docs/en/guides/get-started/introduction.mdx +0 -1
  82. package/docs/en/guides/topic-detail/module-federation/deploy.mdx +2 -2
  83. package/docs/en/guides/topic-detail/module-federation/introduce.mdx +0 -1
  84. package/docs/en/guides/upgrade/config.mdx +12 -0
  85. package/docs/en/guides/upgrade/other.mdx +4 -4
  86. package/docs/en/guides/upgrade/tailwindcss.mdx +0 -1
  87. package/docs/en/guides/upgrade/web-server.md +34 -17
  88. package/docs/en/index.md +1 -0
  89. package/docs/en/plugin/cli-plugins/api.mdx +23 -23
  90. package/docs/en/plugin/cli-plugins/life-cycle.mdx +2 -0
  91. package/docs/en/plugin/introduction.mdx +6 -6
  92. package/docs/en/plugin/official/cli-plugins/plugin-bff.mdx +1 -1
  93. package/docs/en/plugin/official/cli-plugins/plugin-ssg.mdx +1 -1
  94. package/docs/en/plugin/plugin-system.mdx +19 -26
  95. package/docs/en/plugin/runtime-plugins/life-cycle.mdx +2 -0
  96. package/docs/en/plugin/server-plugins/api.mdx +11 -14
  97. package/docs/en/plugin/server-plugins/life-cycle.mdx +2 -0
  98. package/docs/zh/apis/app/hooks/api/lambda.mdx +1 -1
  99. package/docs/zh/apis/app/hooks/config/html.mdx +1 -0
  100. package/docs/zh/apis/app/hooks/config/mock.mdx +1 -0
  101. package/docs/zh/apis/app/hooks/config/public.mdx +1 -0
  102. package/docs/zh/apis/app/hooks/config/upload.mdx +2 -3
  103. package/docs/zh/apis/app/hooks/modern-config.mdx +1 -0
  104. package/docs/zh/apis/app/hooks/shared.mdx +1 -0
  105. package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +1 -0
  106. package/docs/zh/apis/app/hooks/src/routes.mdx +1 -0
  107. package/docs/zh/apis/app/hooks/src/server.mdx +2 -1
  108. package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +1 -1
  109. package/docs/zh/apis/app/runtime/core/create-root.mdx +4 -1
  110. package/docs/zh/apis/app/runtime/core/render.mdx +6 -3
  111. package/docs/zh/apis/app/runtime/core/runtime-context.mdx +17 -17
  112. package/docs/zh/apis/app/runtime/router/router.mdx +10 -6
  113. package/docs/zh/apis/app/runtime/ssr/no-ssr.mdx +1 -0
  114. package/docs/zh/apis/app/runtime/utility/head.mdx +1 -0
  115. package/docs/zh/apis/app/runtime/utility/loadable.mdx +1 -0
  116. package/docs/zh/community/blog/v3-release-note.mdx +38 -29
  117. package/docs/zh/community/contributing-guide.mdx +16 -34
  118. package/docs/zh/components/default-browserslist.mdx +0 -1
  119. package/docs/zh/components/deploy-command.mdx +1 -1
  120. package/docs/zh/components/deploy.mdx +1 -1
  121. package/docs/zh/components/entry-scan-logic.mdx +1 -1
  122. package/docs/zh/components/init-app.mdx +9 -3
  123. package/docs/zh/components/international/custom-instance-code.mdx +0 -1
  124. package/docs/zh/components/international/install-command.mdx +0 -1
  125. package/docs/zh/components/micro-runtime-config.mdx +12 -9
  126. package/docs/zh/components/nodeVersion.mdx +2 -2
  127. package/docs/zh/components/rsc-deploy-tip.mdx +0 -1
  128. package/docs/zh/components/ssr-monitor.mdx +1 -1
  129. package/docs/zh/configure/app/bff/cross-project.mdx +2 -2
  130. package/docs/zh/configure/app/bff/prefix.mdx +1 -1
  131. package/docs/zh/configure/app/dev/lazy-compilation.mdx +17 -1
  132. package/docs/zh/configure/app/html/crossorigin.mdx +0 -1
  133. package/docs/zh/configure/app/html/favicon.mdx +0 -1
  134. package/docs/zh/configure/app/output/convert-to-rem.mdx +0 -1
  135. package/docs/zh/configure/app/output/css-modules.mdx +1 -5
  136. package/docs/zh/configure/app/output/inline-scripts.mdx +1 -2
  137. package/docs/zh/configure/app/output/inline-styles.mdx +1 -2
  138. package/docs/zh/configure/app/output/source-map.mdx +0 -2
  139. package/docs/zh/configure/app/output/split-route-chunks.mdx +1 -1
  140. package/docs/zh/configure/app/output/ssg.mdx +1 -0
  141. package/docs/zh/configure/app/output/ssgByEntries.mdx +1 -1
  142. package/docs/zh/configure/app/performance/chunk-split.mdx +1 -4
  143. package/docs/zh/configure/app/resolve/dedupe.mdx +0 -1
  144. package/docs/zh/configure/app/runtime/plugins.mdx +6 -4
  145. package/docs/zh/configure/app/server/rsc.mdx +1 -1
  146. package/docs/zh/configure/app/server/ssr.mdx +6 -7
  147. package/docs/zh/configure/app/server/tsconfig-path.mdx +2 -2
  148. package/docs/zh/configure/app/tools/less.mdx +6 -7
  149. package/docs/zh/configure/app/tools/sass.mdx +6 -7
  150. package/docs/zh/configure/app/tools/svgr.mdx +11 -11
  151. package/docs/zh/configure/app/usage.mdx +1 -5
  152. package/docs/zh/guides/advanced-features/bff.mdx +1 -0
  153. package/docs/zh/guides/advanced-features/international/advanced.mdx +1 -1
  154. package/docs/zh/guides/advanced-features/international/api.mdx +28 -27
  155. package/docs/zh/guides/advanced-features/international/best-practices.mdx +15 -15
  156. package/docs/zh/guides/advanced-features/international/configuration.mdx +38 -36
  157. package/docs/zh/guides/advanced-features/international/locale-detection.mdx +37 -28
  158. package/docs/zh/guides/advanced-features/international/resource-loading.mdx +20 -16
  159. package/docs/zh/guides/advanced-features/international/routing.mdx +6 -4
  160. package/docs/zh/guides/advanced-features/international.mdx +9 -9
  161. package/docs/zh/guides/advanced-features/server-monitor/logger.mdx +4 -6
  162. package/docs/zh/guides/advanced-features/server-monitor/metrics.mdx +9 -10
  163. package/docs/zh/guides/advanced-features/source-build.mdx +1 -1
  164. package/docs/zh/guides/advanced-features/web-server.mdx +3 -3
  165. package/docs/zh/guides/basic-features/data/data-fetch.mdx +1 -3
  166. package/docs/zh/guides/basic-features/data/data-write.mdx +25 -31
  167. package/docs/zh/guides/basic-features/debug/mock.mdx +2 -2
  168. package/docs/zh/guides/basic-features/debug/using-storybook.mdx +5 -5
  169. package/docs/zh/guides/basic-features/env-vars.mdx +0 -1
  170. package/docs/zh/guides/basic-features/html.mdx +2 -3
  171. package/docs/zh/guides/basic-features/render/overview.mdx +12 -13
  172. package/docs/zh/guides/basic-features/render/rsc.mdx +16 -9
  173. package/docs/zh/guides/basic-features/render/ssr.mdx +8 -17
  174. package/docs/zh/guides/basic-features/routes/config-routes.mdx +61 -59
  175. package/docs/zh/guides/basic-features/testing/playwright.mdx +15 -10
  176. package/docs/zh/guides/basic-features/testing/rstest.mdx +6 -4
  177. package/docs/zh/guides/get-started/ai-coding-agents.mdx +7 -7
  178. package/docs/zh/guides/topic-detail/module-federation/deploy.mdx +2 -2
  179. package/docs/zh/guides/upgrade/config.mdx +12 -0
  180. package/docs/zh/guides/upgrade/other.md +4 -11
  181. package/docs/zh/guides/upgrade/overview.mdx +1 -4
  182. package/docs/zh/guides/upgrade/tailwindcss.mdx +0 -1
  183. package/docs/zh/guides/upgrade/web-server.md +34 -19
  184. package/docs/zh/index.md +1 -0
  185. package/docs/zh/plugin/cli-plugins/api.mdx +24 -24
  186. package/docs/zh/plugin/cli-plugins/life-cycle.mdx +2 -0
  187. package/docs/zh/plugin/introduction.mdx +6 -6
  188. package/docs/zh/plugin/official/cli-plugins/plugin-bff.mdx +1 -1
  189. package/docs/zh/plugin/official/cli-plugins/plugin-ssg.mdx +1 -1
  190. package/docs/zh/plugin/plugin-system.mdx +19 -26
  191. package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +2 -0
  192. package/docs/zh/plugin/server-plugins/api.mdx +11 -14
  193. package/docs/zh/plugin/server-plugins/life-cycle.mdx +2 -0
  194. package/package.json +11 -13
  195. package/{rspress.config.ts → rstack.config.mts} +7 -10
  196. package/src/components/Footer/styles.module.scss +1 -1
  197. package/src/components/SolutionCards/index.module.scss +3 -1
@@ -6,7 +6,6 @@ sidebar_position: 3
6
6
 
7
7
  默认情况下,Modern.js 推荐使用 [约定式路由](./routes.mdx) 作为路由定义的方式。同时,Modern.js 也提供了一个配置式路由的能力,其可以和约定式路由一起使用,或两者分别单独使用。
8
8
 
9
-
10
9
  ## 何时使用配置式路由
11
10
 
12
11
  配置式路由在以下场景中特别有用:
@@ -22,27 +21,26 @@ sidebar_position: 3
22
21
  在 `src` 或每个入口目录下,你可以定义一个 `modern.routes.ts` 文件,通过该文件,你可以对路由进行配置:
23
22
 
24
23
  ```ts
25
- import { defineRoutes } from '@modern-js/runtime/config-routes'
24
+ import { defineRoutes } from '@modern-js/runtime/config-routes';
26
25
 
27
26
  export default defineRoutes(({ route, layout, page, $ }, fileRoutes) => {
28
- return [
29
- route("home.tsx", "/"),
30
- ]
31
- })
27
+ return [route('home.tsx', '/')];
28
+ });
32
29
  ```
33
30
 
34
31
  ### 函数签名说明
35
32
 
36
33
  `defineRoutes` 接受一个回调函数,该回调函数有两个参数:
34
+
37
35
  1. `routeFunctions`:包含 `route`、`layout`、`page`、`$` 函数的对象
38
36
  2. `fileRoutes`:约定式路由生成的路由配置数组
39
37
 
40
38
  路由函数的基本签名:
39
+
41
40
  - 第一个参数:相对于 `modern.routes.ts` 的文件路径
42
41
  - 第二个参数:路由路径(可选,必须是字符串)
43
42
  - 第三个参数:子路由数组(可选)
44
43
 
45
-
46
44
  ## 路由函数
47
45
 
48
46
  Modern.js 提供了四个主要的路由配置函数:
@@ -50,6 +48,7 @@ Modern.js 提供了四个主要的路由配置函数:
50
48
  :::tip 路径说明
51
49
 
52
50
  所有路由函数的第一个参数(路径)都是**相对路径**,会与父级路径拼接生成最终的路由路径:
51
+
53
52
  - **根路径**:`"/"` 或 "" 表示当前层级的根路径
54
53
  - **相对路径**:子路由的路径会相对于父级路径进行拼接
55
54
  - **动态路径**:使用 `:param` 语法表示动态参数
@@ -57,7 +56,6 @@ Modern.js 提供了四个主要的路由配置函数:
57
56
 
58
57
  :::
59
58
 
60
-
61
59
  :::info
62
60
 
63
61
  - `route`、`layout`、`page`、`$` 等函数的第一个参数(组件文件路径)必须指向当前项目中的真实文件,暂不支持 `node_modules` 及 Monorepo 下其他仓库中的文件
@@ -73,27 +71,28 @@ Modern.js 提供了四个主要的路由配置函数:
73
71
  export default defineRoutes(({ route }, fileRoutes) => {
74
72
  return [
75
73
  // 无子路由时,自动生成页面路由
76
- route("home.tsx", "/"),
77
- route("about.tsx", "about"),
74
+ route('home.tsx', '/'),
75
+ route('about.tsx', 'about'),
78
76
 
79
77
  // 有子路由时,自动生成布局路由
80
78
  // dashboard/layout.tsx 需要包含 <Outlet> 来渲染子路由
81
- route("dashboard/layout.tsx", "dashboard", [
82
- route("dashboard/overview.tsx", "overview"), // 生成路径:/dashboard/overview
83
- route("dashboard/settings.tsx", "settings"), // 生成路径:/dashboard/settings
79
+ route('dashboard/layout.tsx', 'dashboard', [
80
+ route('dashboard/overview.tsx', 'overview'), // 生成路径:/dashboard/overview
81
+ route('dashboard/settings.tsx', 'settings'), // 生成路径:/dashboard/settings
84
82
  ]),
85
83
 
86
84
  // 动态路由
87
- route("products/detail.tsx", "products/:id"),
85
+ route('products/detail.tsx', 'products/:id'),
88
86
 
89
87
  // 多路径指向同一组件
90
- route("user/profile.tsx", "user"),
91
- route("user/profile.tsx", "profile"),
92
- ]
93
- })
88
+ route('user/profile.tsx', 'user'),
89
+ route('user/profile.tsx', 'profile'),
90
+ ];
91
+ });
94
92
  ```
95
93
 
96
94
  **使用场景**:
95
+
97
96
  - 快速配置路由,无需明确指定是页面还是布局
98
97
  - 简化路由配置的复杂度
99
98
 
@@ -105,15 +104,16 @@ export default defineRoutes(({ route }, fileRoutes) => {
105
104
  export default defineRoutes(({ layout, page }, fileRoutes) => {
106
105
  return [
107
106
  // 生成布局路由,路径为 "/auth",必须包含子路由
108
- layout("auth/layout.tsx", "auth", [
109
- page("auth/login/page.tsx", "login"), // 生成路径:/auth/login
110
- page("auth/register/page.tsx", "register"), // 生成路径:/auth/register
107
+ layout('auth/layout.tsx', 'auth', [
108
+ page('auth/login/page.tsx', 'login'), // 生成路径:/auth/login
109
+ page('auth/register/page.tsx', 'register'), // 生成路径:/auth/register
111
110
  ]),
112
- ]
113
- })
111
+ ];
112
+ });
114
113
  ```
115
114
 
116
115
  **使用场景**:
116
+
117
117
  - 需要明确指定某个组件为布局组件
118
118
  - 为多个页面提供共同的布局结构
119
119
  - 需要在多个路由间共享导航、侧边栏等UI组件
@@ -125,15 +125,16 @@ export default defineRoutes(({ layout, page }, fileRoutes) => {
125
125
  ```ts
126
126
  export default defineRoutes(({ layout, page }, fileRoutes) => {
127
127
  return [
128
- layout("dashboard/layout.tsx", "dashboard", [
129
- page("dashboard/overview.tsx", "overview"), // 生成路径:/dashboard/overview
130
- page("dashboard/settings.tsx", "settings"), // 生成路径:/dashboard/settings
128
+ layout('dashboard/layout.tsx', 'dashboard', [
129
+ page('dashboard/overview.tsx', 'overview'), // 生成路径:/dashboard/overview
130
+ page('dashboard/settings.tsx', 'settings'), // 生成路径:/dashboard/settings
131
131
  ]),
132
- ]
133
- })
132
+ ];
133
+ });
134
134
  ```
135
135
 
136
136
  **使用场景**:
137
+
137
138
  - 需要明确指定某个组件为页面组件
138
139
  - 确保组件不包含 `<Outlet>` 子组件渲染
139
140
  - 提供更清晰的语义表达
@@ -145,16 +146,17 @@ export default defineRoutes(({ layout, page }, fileRoutes) => {
145
146
  ```ts
146
147
  export default defineRoutes(({ layout, page, $ }, fileRoutes) => {
147
148
  return [
148
- layout("blog/layout.tsx", "blog", [
149
- page("blog/page.tsx", ""), // 生成路径:/blog
150
- page("blog/[id]/page.tsx", ":id"), // 生成路径:/blog/:id
151
- $("blog/$.tsx", "*"), // 通配路由,匹配 /blog 下的所有未匹配路径
149
+ layout('blog/layout.tsx', 'blog', [
150
+ page('blog/page.tsx', ''), // 生成路径:/blog
151
+ page('blog/[id]/page.tsx', ':id'), // 生成路径:/blog/:id
152
+ $('blog/$.tsx', '*'), // 通配路由,匹配 /blog 下的所有未匹配路径
152
153
  ]),
153
- ]
154
- })
154
+ ];
155
+ });
155
156
  ```
156
157
 
157
158
  **使用场景**:
159
+
158
160
  - 自定义 404 页面
159
161
  - 处理特定路径下的所有未匹配请求
160
162
 
@@ -170,11 +172,11 @@ export default defineRoutes(({ layout, page, $ }, fileRoutes) => {
170
172
  export default defineRoutes(({ page }, fileRoutes) => {
171
173
  return [
172
174
  // 使用 page 函数明确指定页面路由
173
- page("home.tsx", "/"),
174
- page("about.tsx", "about"),
175
- page("contact.tsx", "contact"),
176
- ]
177
- })
175
+ page('home.tsx', '/'),
176
+ page('about.tsx', 'about'),
177
+ page('contact.tsx', 'contact'),
178
+ ];
179
+ });
178
180
  ```
179
181
 
180
182
  ### 无路径路由
@@ -186,15 +188,16 @@ export default defineRoutes(({ layout, page }, fileRoutes) => {
186
188
  return [
187
189
  // 使用 layout 函数明确指定布局路由
188
190
  // auth/layout.tsx 需要包含 <Outlet> 来渲染子路由
189
- layout("auth/layout.tsx", [
190
- page("login/page.tsx", "login"),
191
- page("register/page.tsx", "register"),
191
+ layout('auth/layout.tsx', [
192
+ page('login/page.tsx', 'login'),
193
+ page('register/page.tsx', 'register'),
192
194
  ]),
193
- ]
194
- })
195
+ ];
196
+ });
195
197
  ```
196
198
 
197
199
  上述配置会生成:
200
+
198
201
  - `/login` → `auth/layout.tsx` + `login/page.tsx`
199
202
  - `/register` → `auth/layout.tsx` + `register/page.tsx`
200
203
 
@@ -203,11 +206,11 @@ export default defineRoutes(({ layout, page }, fileRoutes) => {
203
206
  ```ts
204
207
  export default defineRoutes(({ page }, fileRoutes) => {
205
208
  return [
206
- page("user.tsx", "user"),
207
- page("user.tsx", "profile"),
208
- page("user.tsx", "account"),
209
- ]
210
- })
209
+ page('user.tsx', 'user'),
210
+ page('user.tsx', 'profile'),
211
+ page('user.tsx', 'account'),
212
+ ];
213
+ });
211
214
  ```
212
215
 
213
216
  ### 动态路由
@@ -218,12 +221,12 @@ export default defineRoutes(({ page }, fileRoutes) => {
218
221
  export default defineRoutes(({ page }, fileRoutes) => {
219
222
  return [
220
223
  // 必需参数
221
- page("blog/detail.tsx", "blog/:id"),
224
+ page('blog/detail.tsx', 'blog/:id'),
222
225
 
223
226
  // 可选参数
224
- page("blog/index.tsx", "blog/:slug?"),
225
- ]
226
- })
227
+ page('blog/index.tsx', 'blog/:slug?'),
228
+ ];
229
+ });
227
230
  ```
228
231
 
229
232
  ## 路由相关文件自动查找
@@ -234,10 +237,10 @@ export default defineRoutes(({ page }, fileRoutes) => {
234
237
  // modern.routes.ts
235
238
  export default defineRoutes(({ route }, fileRoutes) => {
236
239
  return [
237
- route("pages/profile.tsx", "profile"),
238
- route("pages/user/detail.tsx", "user/:id"),
239
- ]
240
- })
240
+ route('pages/profile.tsx', 'profile'),
241
+ route('pages/user/detail.tsx', 'user/:id'),
242
+ ];
243
+ });
241
244
  ```
242
245
 
243
246
  Modern.js 会自动查找并加载:
@@ -259,7 +262,6 @@ Modern.js 会自动查找并加载:
259
262
 
260
263
  :::
261
264
 
262
-
263
265
  ## 路由合并
264
266
 
265
267
  配置式路由可以与约定式路由一起使用,你可以通过修改 `fileRoutes` 参数来实现路由的合并:
@@ -274,7 +276,6 @@ Modern.js 会自动查找并加载:
274
276
 
275
277
  :::
276
278
 
277
-
278
279
  ### 合并示例
279
280
 
280
281
  以下示例展示了配置式路由与约定式路由的合并方式:
@@ -312,6 +313,7 @@ export default defineRoutes(({ page }, fileRoutes) => {
312
313
  return fileRoutes;
313
314
  });
314
315
  ```
316
+
315
317
  ## 调试路由
316
318
 
317
319
  由于路由合并后的最终结构可能不够直观,Modern.js 提供了调试命令来帮助你查看完整路由信息:
@@ -6,8 +6,13 @@ Playwright 是一个测试框架,它允许你使用单一的 API,自动的
6
6
 
7
7
  import { PackageManagerTabs } from '@theme';
8
8
 
9
- <PackageManagerTabs command={{ npm: "npm init playwright", yarn: "yarn create playwright", pnpm: "pnpm create playwright" }} />
10
-
9
+ <PackageManagerTabs
10
+ command={{
11
+ npm: 'npm init playwright',
12
+ yarn: 'yarn create playwright',
13
+ pnpm: 'pnpm create playwright',
14
+ }}
15
+ />
11
16
 
12
17
  上述命令会自动安装 Playwright 依赖,并通过一系列提示帮助你在项目中安装和配置,包括添加一个 `playwright.config.ts` 文件。
13
18
 
@@ -75,18 +80,18 @@ export default Index;
75
80
  接下来,添加测试用例,来保证你页面的链接能够正常工作。
76
81
 
77
82
  ```ts title="tests/example.spec.ts"
78
- import { test, expect } from '@playwright/test'
79
-
83
+ import { test, expect } from '@playwright/test';
84
+
80
85
  test('should navigate to the about page', async ({ page }) => {
81
86
  // Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
82
- await page.goto('http://localhost:8080/')
87
+ await page.goto('http://localhost:8080/');
83
88
  // Find an element with the text 'About' and click on it
84
- await page.click('text=About')
89
+ await page.click('text=About');
85
90
  // The new URL should be "/about" (baseURL is used there)
86
- await expect(page).toHaveURL('http://localhost:8080/about')
91
+ await expect(page).toHaveURL('http://localhost:8080/about');
87
92
  // The new page should contain an h1 with "About"
88
- await expect(page.locator('h1')).toContainText('About')
89
- })
93
+ await expect(page.locator('h1')).toContainText('About');
94
+ });
90
95
  ```
91
96
 
92
97
  ## 运行测试用例
@@ -109,4 +114,4 @@ Running 3 tests using 3 workers
109
114
  To open last HTML report run:
110
115
 
111
116
  npx playwright show-report
112
- ```
117
+ ```
@@ -84,9 +84,9 @@ test('Page', async () => {
84
84
  </Router>,
85
85
  );
86
86
 
87
- await expect.element(
88
- page.getByRole('heading', { level: 1, name: 'Home' }),
89
- ).toBeVisible();
87
+ await expect
88
+ .element(page.getByRole('heading', { level: 1, name: 'Home' }))
89
+ .toBeVisible();
90
90
  });
91
91
  ```
92
92
 
@@ -181,12 +181,14 @@ First install and use the migrate-to-rstest skill. If it is not installed, insta
181
181
  npx skills add rstackjs/agent-skills --skill migrate-to-rstest
182
182
 
183
183
  Follow the migrate-to-rstest skill instructions and the official Rstest migration guides:
184
+
184
185
  - Jest: https://rstest.rs/guide/migration/jest
185
186
  - Vitest: https://rstest.rs/guide/migration/vitest
186
187
 
187
188
  Modern.js-specific requirement:
189
+
188
190
  - Install and use @modern-js/adapter-rstest together with @rstest/core.
189
191
  - Create or update rstest.config.ts to import withModernConfig from @modern-js/adapter-rstest.
190
192
  - Reuse the Modern.js app configuration with extends: withModernConfig().
191
193
  - Do not replace this with a plain Rstest config unless the project explicitly does not need Modern.js config integration.`}
192
- />
194
+ />
@@ -15,10 +15,10 @@ AI Agent 由此获取与当前框架版本严格匹配的 API、配置与约定
15
15
 
16
16
  ## AGENTS.md
17
17
 
18
- `AGENTS.md` 是给 AI 编码助手的项目指引,告诉它在动手前先阅读随包文档、并遵循 Modern.js 的约定;同时会生成 `CLAUDE.md`,通过 `@AGENTS.md` 让 Claude Code 复用同一份指引。
18
+ `AGENTS.md` 是给 AI 编码助手的项目指引,告诉它在动手前先阅读随包文档、并遵循 Modern.js 的约定。
19
19
 
20
- - **新建项目**:`npx @modern-js/create` 默认生成这两个文件,加 `--no-agents-md` 可跳过。
21
- - **已有项目**:在项目根运行 `npx @modern-js/create --agents-md-only` 生成或更新,可随时重复运行,你添加的自定义内容会被保留。
20
+ - **新建项目**:`npx @modern-js/create` 默认生成 `AGENTS.md`,加 `--no-agents-md` 可跳过。
21
+ - **已有项目**:在项目根运行 `npx @modern-js/create --agents-md-only` 生成或更新 `AGENTS.md`,可随时重复运行,你添加的自定义内容会被保留。
22
22
 
23
23
  Modern.js 只维护 `AGENTS.md` 中 `<!-- BEGIN:modernjs-agent-rules -->` 标记内的内容,标记之外你写的内容不会被改动。
24
24
 
@@ -35,10 +35,10 @@ llms.txt 是遵循 [llms.txt 规范](https://llmstxt.org/) 的在线文档索引
35
35
 
36
36
  Skills 是按需触发的 AI 辅助能力,遵循 [Agent Skills 开放标准](https://github.com/vercel-labs/skills)。用户向 Skills:
37
37
 
38
- | Skill | 标识 | 说明 |
39
- |---|---|---|
40
- | 升级到 v3 | `modernjs-migrate-to-v3` | v2 应用迁移到 v3:安全改写 + 复杂项人工清单 + 迁移报告 |
41
- | 启用特性 | `modernjs-feature-enable` | 为 v3 应用启用 BFF / SSG / styled-components,并脚手架化 Tailwind CSS / 自定义 Web Server |
38
+ | Skill | 标识 | 说明 |
39
+ | --------- | ------------------------- | ----------------------------------------------------------------------------------------- |
40
+ | 升级到 v3 | `modernjs-migrate-to-v3` | v2 应用迁移到 v3:安全改写 + 复杂项人工清单 + 迁移报告 |
41
+ | 启用特性 | `modernjs-feature-enable` | 为 v3 应用启用 BFF / SSG / styled-components,并脚手架化 Tailwind CSS / 自定义 Web Server |
42
42
 
43
43
  > Skills 默认不强装、不隐式安装,由你显式安装。RSC、微前端等属于配置或架构决策,不是 `modernjs-feature-enable` 的一键启用项。
44
44
 
@@ -63,9 +63,9 @@ export default defineConfig({
63
63
  "name": "remote",
64
64
  "publicPath": "https://my-remote-module/"
65
65
  },
66
- "shared": [ /* xxx */ ],
66
+ "shared": [/* xxx */],
67
67
  "remotes": [],
68
- "exposes": [ /* xxx */ ]
68
+ "exposes": [/* xxx */]
69
69
  }
70
70
  ```
71
71
 
@@ -22,6 +22,18 @@ server: {
22
22
  }
23
23
  ```
24
24
 
25
+ ### dev.lazyCompilation
26
+
27
+ **变更内容**:开发环境下的默认值变化。纯 CSR 与流式 SSR 应用默认开启按需编译,默认值为 `{ imports: true, entries: false }`;使用 string SSR、RSC 或 SSG 的应用仍默认关闭。该变更只影响开发环境,不影响生产构建产物。
28
+
29
+ **迁移示例**:如需保持 2.0 的行为,可以显式关闭:
30
+
31
+ ```typescript
32
+ dev: {
33
+ lazyCompilation: false;
34
+ }
35
+ ```
36
+
25
37
  ## html
26
38
 
27
39
  ### html.appIcon
@@ -22,10 +22,10 @@ Modern.js 3.0 将项目默认入口名称改为 `index`,默认构建出的 HTM
22
22
 
23
23
  Modern.js 3.0 对部分运行时进行了调整,需要更新相关的导入路径。路径映射对照如下:
24
24
 
25
- | 旧版路径 | 新版路径 | 说明 |
26
- |---------|---------|------|
27
- | `@modern-js/runtime/bff` | `@modern-js/plugin-bff/runtime` | BFF 运行时路径 |
28
- | `@modern-js/runtime/server` | `@modern-js/server-runtime` | 服务端运行时路径 |
25
+ | 旧版路径 | 新版路径 | 说明 |
26
+ | --------------------------- | ------------------------------- | ---------------- |
27
+ | `@modern-js/runtime/bff` | `@modern-js/plugin-bff/runtime` | BFF 运行时路径 |
28
+ | `@modern-js/runtime/server` | `@modern-js/server-runtime` | 服务端运行时路径 |
29
29
 
30
30
  ## useRuntimeContext 已废弃
31
31
 
@@ -81,7 +81,6 @@ Modern.js 3.0 将 `server.ssr.mode` 的默认值从 `'string'` 改为 `'stream'`
81
81
 
82
82
  对于 React 18 及以上项目,把 `ssr.mode` 的值由 `'stream'` 改为 `'string'`,不对 Data Loader 中的代码进行修改或使用 Suspense 的话,从渲染结果上没有任何影响。如果你的项目依赖了 React17,请把 `ssr.mode` 的值手动设置为 `'string'`。
83
83
 
84
-
85
84
  ## 使用 React Router v7
86
85
 
87
86
  Modern.js 3.0 默认使用 React Router v7 作为路由库。React Router v7 相比 v6 只有少量的 [不兼容变更](https://reactrouter.com/upgrading/v6)。
@@ -182,9 +181,3 @@ antd v5 使用了 CSS-in-JS 方案,已原生支持按需加载,无需配置
182
181
  ## Eslint 规则集
183
182
 
184
183
  Modern.js 之前提供了 ESLint 的完整规则集,涵盖了 @modern-js(针对 Node.js 项目的 Lint 规则)和 @modern-js-app(针对前端项目的 Lint 规则)。在 [v2.60.0](https://github.com/web-infra-dev/modern.js/releases/tag/v2.60.0) 版本中,我们正式移除了这些规则集。我们鼓励开发者根据自身需求选择合适的代码规范工具,直接使用 ESLint 并结合社区推荐的规则,或使用 Biome 以提升代码格式化的性能。
185
-
186
-
187
-
188
-
189
-
190
-
@@ -14,15 +14,13 @@ Modern.js 3.0 带来了多项重大改进和变化,主要包括:
14
14
  - **SSG 能力完善**:提供完整的[静态站点生成(SSG)](/guides/basic-features/render/ssg)支持。
15
15
  - **路由配置增强**:支持[配置式路由](/guides/basic-features/routes/config-routes),可单独使用或与[约定式路由](/guides/basic-features/routes/routes)结合,提供更灵活的路由定义方式。
16
16
 
17
-
18
17
  ## 升级前置检查
19
18
 
20
19
  在开始升级之前,请确认:
21
20
 
22
21
  1. 当前项目使用的是 Modern.js 2.0
23
22
  2. 确定使用的 react 版本是 17 及以上
24
- 2. 确定使用的 node.js 版本是 18.20.8 及以上,推荐使用 node.js 22 以上版本
25
-
23
+ 3. 确定使用的 node.js 版本是 18.20.8 及以上,推荐使用 node.js 22 以上版本
26
24
 
27
25
  ## 获取帮助
28
26
 
@@ -33,4 +31,3 @@ Modern.js 3.0 带来了多项重大改进和变化,主要包括:
33
31
  - 加入 [Modern.js Discord 社区](https://discord.gg/modernjs)与其他开发者交流
34
32
 
35
33
  我们建议在提交 issue 时提供尽可能详细的信息,包括错误日志、配置文件和复现步骤,以便更快地解决问题
36
-
@@ -89,4 +89,3 @@ export default {
89
89
  @tailwind components;
90
90
  @tailwind utilities;
91
91
  ```
92
-
@@ -7,7 +7,6 @@
7
7
 
8
8
  这两种写法在旧版中互斥,迁移时请根据项目实际使用的能力选择对应路径。
9
9
 
10
-
11
10
  ## unstableMiddleware
12
11
 
13
12
  ### 核心差异
@@ -22,7 +21,10 @@
22
21
 
23
22
  ```typescript
24
23
  // 旧版 - server/index.ts
25
- export const unstableMiddleware: UnstableMiddleware[] = [middleware1, middleware2];
24
+ export const unstableMiddleware: UnstableMiddleware[] = [
25
+ middleware1,
26
+ middleware2,
27
+ ];
26
28
 
27
29
  // 新版 - server/modern.server.ts
28
30
  import { defineServerConfig } from '@modern-js/server-runtime';
@@ -39,13 +41,22 @@ export default defineServerConfig({
39
41
 
40
42
  ```typescript
41
43
  // 旧版
42
- import type { UnstableMiddleware, UnstableMiddlewareContext } from '@modern-js/server-runtime';
43
- const middleware: UnstableMiddleware = async (c: UnstableMiddlewareContext, next) => {
44
+ import type {
45
+ UnstableMiddleware,
46
+ UnstableMiddlewareContext,
47
+ } from '@modern-js/server-runtime';
48
+ const middleware: UnstableMiddleware = async (
49
+ c: UnstableMiddlewareContext,
50
+ next,
51
+ ) => {
44
52
  return c.response.raw('response'); // 不调用 next 也会继续渲染
45
53
  };
46
54
 
47
55
  // 新版
48
- import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
56
+ import {
57
+ defineServerConfig,
58
+ type MiddlewareHandler,
59
+ } from '@modern-js/server-runtime';
49
60
  const middleware: MiddlewareHandler = async (c, next) => {
50
61
  await next(); // 必须调用
51
62
  return c.text('response');
@@ -54,18 +65,17 @@ const middleware: MiddlewareHandler = async (c, next) => {
54
65
 
55
66
  ### Context API 对照
56
67
 
57
- | 旧版 API | 新版 API | 说明 |
58
- | -------------------- | ---------------------- | ------------- |
59
- | `c.request.cookie` | `getCookie(c, 'key')` | Cookie 读取 |
60
- | `c.req.cookie()` | `getCookie(c, 'key')` | Hono v4 已废弃 |
61
- | `c.request.pathname` | `c.req.path` | 请求路径 |
62
- | `c.request.host` | `c.req.header('Host')` | 请求主机 |
63
- | `c.request.query` | `c.req.query()` | 查询参数 |
64
- | `c.request.headers` | `c.req.header()` | 请求头 |
65
- | `c.response.status` | `c.status()` | 响应状态码 |
66
- | `c.response.set` | `c.res.headers.set` | 设置响应头 |
67
- | `c.response.raw` | `c.text` / `c.json` | 响应体 |
68
-
68
+ | 旧版 API | 新版 API | 说明 |
69
+ | -------------------- | ---------------------- | -------------- |
70
+ | `c.request.cookie` | `getCookie(c, 'key')` | Cookie 读取 |
71
+ | `c.req.cookie()` | `getCookie(c, 'key')` | Hono v4 已废弃 |
72
+ | `c.request.pathname` | `c.req.path` | 请求路径 |
73
+ | `c.request.host` | `c.req.header('Host')` | 请求主机 |
74
+ | `c.request.query` | `c.req.query()` | 查询参数 |
75
+ | `c.request.headers` | `c.req.header()` | 请求头 |
76
+ | `c.response.status` | `c.status()` | 响应状态码 |
77
+ | `c.response.set` | `c.res.headers.set` | 设置响应头 |
78
+ | `c.response.raw` | `c.text` / `c.json` | 响应体 |
69
79
 
70
80
  ## afterRender Hook
71
81
 
@@ -90,7 +100,10 @@ export const afterRender = (ctx, next) => {
90
100
  };
91
101
 
92
102
  // 新版 - server/modern.server.ts
93
- import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
103
+ import {
104
+ defineServerConfig,
105
+ type MiddlewareHandler,
106
+ } from '@modern-js/server-runtime';
94
107
 
95
108
  const renderMiddleware: MiddlewareHandler = async (c, next) => {
96
109
  await next(); // 先等待页面渲染
@@ -106,6 +119,8 @@ const renderMiddleware: MiddlewareHandler = async (c, next) => {
106
119
  };
107
120
 
108
121
  export default defineServerConfig({
109
- renderMiddlewares: [{ name: 'custom-content-injection', handler: renderMiddleware }],
122
+ renderMiddlewares: [
123
+ { name: 'custom-content-injection', handler: renderMiddleware },
124
+ ],
110
125
  });
111
126
  ```
package/docs/zh/index.md CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
2
  pageType: home
3
3
  ---
4
+
4
5
  # Modern.js - 基于 React 的渐进式 Web 开发框架
@@ -39,29 +39,29 @@ export default myCliPlugin;
39
39
 
40
40
  - **返回值:** `AppContext` 对象,包含以下字段:
41
41
 
42
- | 字段名 | 类型 | 描述 | 何时可用 |
43
- | ---------------------- | ------------------- | -------------------------------------------------- | --------------------------- |
44
- | `command` | `string` | 当前执行的命令 (e.g., `dev`, `build`, `deploy`) | - |
45
- | `port` | `number` | 开发服务器端口号 | `onPrepare` 之后 |
46
- | `configFile` | `string` | 配置文件的绝对路径 | - |
47
- | `isProd` | `boolean` | 是否为生产模式 | - |
48
- | `appDirectory` | `string` | 项目根目录的绝对路径 | - |
49
- | `srcDirectory` | `string` | 项目源码目录的绝对路径 | - |
50
- | `distDirectory` | `string` | 项目产物输出目录的绝对路径 | `onPrepare`(及之后) |
51
- | `sharedDirectory` | `string` | 公共模块目录的绝对路径 | - |
52
- | `nodeModulesDirectory` | `string` | `node_modules` 目录的绝对路径 | - |
53
- | `ip` | `string` | 当前机器的 IPv4 地址 | - |
54
- | `packageName` | `string` | 项目 `package.json` 中的 `name` 字段 | - |
55
- | `plugins` | `object[]` | 当前已注册的插件列表 | - |
56
- | `entrypoints` | `object[]` | 页面入口信息 | - |
57
- | `serverRoutes` | `object[]` | 服务端路由信息 | - |
58
- | `bundlerType` | `rspack` | 当前使用的打包工具类型 | `onPrepare` 之后 |
59
- | `metaName` | `string` | 框架内部名称 | - |
60
- | `apiDirectory` | `string` | API 模块目录的绝对路径 (BFF 使用) | - |
61
- | `lambdaDirectory` | `string` | Lambda 模块目录的绝对路径 (BFF 使用) | - |
62
- | `runtimeConfigFile` | `string` | 运行时配置文件的名称 | - |
63
- | `checkedEntries` | `string[]` | 指定的入口信息 | - |
64
- | `apiOnly` | `boolean` | 是否为 `apiOnly` 模式 | - |
42
+ | 字段名 | 类型 | 描述 | 何时可用 |
43
+ | ---------------------- | ---------- | -------------------------------------------------- | --------------------- |
44
+ | `command` | `string` | 当前执行的命令 (e.g., `dev`, `build`, `deploy`) | - |
45
+ | `port` | `number` | 开发服务器端口号 | `onPrepare` 之后 |
46
+ | `configFile` | `string` | 配置文件的绝对路径 | - |
47
+ | `isProd` | `boolean` | 是否为生产模式 | - |
48
+ | `appDirectory` | `string` | 项目根目录的绝对路径 | - |
49
+ | `srcDirectory` | `string` | 项目源码目录的绝对路径 | - |
50
+ | `distDirectory` | `string` | 项目产物输出目录的绝对路径 | `onPrepare`(及之后) |
51
+ | `sharedDirectory` | `string` | 公共模块目录的绝对路径 | - |
52
+ | `nodeModulesDirectory` | `string` | `node_modules` 目录的绝对路径 | - |
53
+ | `ip` | `string` | 当前机器的 IPv4 地址 | - |
54
+ | `packageName` | `string` | 项目 `package.json` 中的 `name` 字段 | - |
55
+ | `plugins` | `object[]` | 当前已注册的插件列表 | - |
56
+ | `entrypoints` | `object[]` | 页面入口信息 | - |
57
+ | `serverRoutes` | `object[]` | 服务端路由信息 | - |
58
+ | `bundlerType` | `rspack` | 当前使用的打包工具类型 | `onPrepare` 之后 |
59
+ | `metaName` | `string` | 框架内部名称 | - |
60
+ | `apiDirectory` | `string` | API 模块目录的绝对路径 (BFF 使用) | - |
61
+ | `lambdaDirectory` | `string` | Lambda 模块目录的绝对路径 (BFF 使用) | - |
62
+ | `runtimeConfigFile` | `string` | 运行时配置文件的名称 | - |
63
+ | `checkedEntries` | `string[]` | 指定的入口信息 | - |
64
+ | `apiOnly` | `boolean` | 是否为 `apiOnly` 模式 | - |
65
65
 
66
66
  - **示例:**
67
67
 
@@ -236,7 +236,7 @@ api.modifyRspackConfig((config, utils) => {
236
236
  config.builtins.minify = {
237
237
  enable: true,
238
238
  implementation: utils.rspack.SwcJsMinimizerRspackPlugin,
239
- }
239
+ };
240
240
  });
241
241
  ```
242
242