@modern-js/main-doc 3.0.0-alpha.0 → 3.0.0-alpha.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 (142) hide show
  1. package/docs/en/apis/app/commands.mdx +6 -30
  2. package/docs/en/components/bff-upload.mdx +3 -5
  3. package/docs/en/components/bundler.mdx +1 -1
  4. package/docs/en/components/enable-bff.mdx +6 -2
  5. package/docs/en/components/enable-ssg.mdx +1 -0
  6. package/docs/en/components/esbuild.mdx +2 -2
  7. package/docs/en/components/extend-bff-function.mdx +2 -4
  8. package/docs/en/components/hono.mdx +119 -0
  9. package/docs/en/components/international/custom-instance-code.mdx +16 -0
  10. package/docs/en/components/international/init-options-desc.mdx +1 -0
  11. package/docs/en/components/international/install-command.mdx +15 -1
  12. package/docs/en/components/international/instance-code.mdx +26 -0
  13. package/docs/en/configure/app/builder-plugins.mdx +1 -2
  14. package/docs/en/configure/app/dev/server.mdx +108 -0
  15. package/docs/en/configure/app/experiments/source-build.mdx +0 -1
  16. package/docs/en/configure/app/output/assets-retry.mdx +1 -1
  17. package/docs/en/configure/app/output/disable-inline-runtime-chunk.mdx +2 -2
  18. package/docs/en/configure/app/output/filename.mdx +2 -4
  19. package/docs/en/configure/app/output/temp-dir.mdx +3 -3
  20. package/docs/en/configure/app/performance/build-cache.mdx +1 -1
  21. package/docs/en/configure/app/performance/profile.mdx +1 -1
  22. package/docs/en/configure/app/plugins.mdx +1 -3
  23. package/docs/en/configure/app/runtime/router.mdx +0 -4
  24. package/docs/en/configure/app/security/sri.mdx +0 -1
  25. package/docs/en/configure/app/source/alias.mdx +1 -1
  26. package/docs/en/configure/app/source/enable-async-entry.mdx +1 -1
  27. package/docs/en/configure/app/source/include.mdx +2 -14
  28. package/docs/en/configure/app/tools/dev-server.mdx +8 -8
  29. package/docs/en/configure/app/usage.mdx +0 -12
  30. package/docs/en/guides/_meta.json +5 -0
  31. package/docs/en/guides/advanced-features/bff/_meta.json +9 -1
  32. package/docs/en/guides/advanced-features/bff/cross-project.mdx +1 -1
  33. package/docs/en/guides/advanced-features/bff/frameworks.mdx +2 -15
  34. package/docs/en/guides/advanced-features/bff/function.mdx +4 -4
  35. package/docs/en/guides/advanced-features/bff/operators.mdx +628 -0
  36. package/docs/en/guides/advanced-features/bff/sdk.mdx +17 -9
  37. package/docs/en/guides/advanced-features/bff/upload.mdx +3 -1
  38. package/docs/en/guides/advanced-features/international/configuration.mdx +7 -16
  39. package/docs/en/guides/advanced-features/international/quick-start.mdx +4 -32
  40. package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
  41. package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +18 -4
  42. package/docs/en/guides/advanced-features/rspack-start.mdx +1 -1
  43. package/docs/en/guides/advanced-features/server-monitor/monitors.mdx +62 -5
  44. package/docs/en/guides/basic-features/data/data-cache.mdx +60 -76
  45. package/docs/en/guides/basic-features/data/data-fetch.mdx +15 -14
  46. package/docs/en/guides/basic-features/debug/proxy.mdx +6 -9
  47. package/docs/en/guides/basic-features/render/rsc.mdx +24 -19
  48. package/docs/en/guides/basic-features/render/ssg.mdx +4 -9
  49. package/docs/en/guides/basic-features/render/ssr-cache.mdx +0 -4
  50. package/docs/en/guides/basic-features/static-assets/svg-assets.mdx +0 -4
  51. package/docs/en/guides/get-started/tech-stack.mdx +1 -1
  52. package/docs/en/guides/upgrade/_meta.json +1 -0
  53. package/docs/en/guides/upgrade/config.mdx +936 -0
  54. package/docs/en/guides/upgrade/entry.mdx +463 -0
  55. package/docs/en/guides/upgrade/other.mdx +83 -0
  56. package/docs/en/guides/upgrade/overview.mdx +33 -0
  57. package/docs/en/guides/upgrade/tailwindcss.mdx +130 -0
  58. package/docs/en/guides/upgrade/web-server.mdx +91 -0
  59. package/docs/en/plugin/_meta.json +5 -0
  60. package/docs/en/plugin/cli-plugins/_meta.json +1 -1
  61. package/docs/en/plugin/cli-plugins/api.mdx +13 -63
  62. package/docs/en/plugin/cli-plugins/life-cycle.mdx +0 -4
  63. package/docs/en/plugin/introduction.mdx +8 -20
  64. package/docs/en/plugin/plugin-system.mdx +3 -3
  65. package/docs/en/plugin/runtime-plugins/_meta.json +1 -1
  66. package/docs/en/plugin/runtime-plugins/api.mdx +1 -1
  67. package/docs/en/plugin/server-plugins/_meta.json +1 -0
  68. package/docs/en/plugin/server-plugins/api.mdx +210 -1
  69. package/docs/en/plugin/server-plugins/life-cycle.mdx +41 -1
  70. package/docs/zh/apis/app/commands.mdx +6 -30
  71. package/docs/zh/components/bff-operator-code.mdx +5 -0
  72. package/docs/zh/components/bff-upload.mdx +0 -2
  73. package/docs/zh/components/bundler.mdx +1 -1
  74. package/docs/zh/components/enable-bff.mdx +6 -2
  75. package/docs/zh/components/enable-ssg.mdx +3 -1
  76. package/docs/zh/components/esbuild.mdx +2 -2
  77. package/docs/zh/components/extend-bff-function.mdx +2 -4
  78. package/docs/zh/components/hono.mdx +119 -0
  79. package/docs/zh/components/international/custom-instance-code.mdx +16 -0
  80. package/docs/zh/components/international/init-options-desc.mdx +1 -0
  81. package/docs/zh/components/international/install-command.mdx +15 -0
  82. package/docs/zh/components/international/instance-code.mdx +26 -0
  83. package/docs/zh/configure/app/builder-plugins.mdx +1 -2
  84. package/docs/zh/configure/app/dev/server.mdx +109 -2
  85. package/docs/zh/configure/app/experiments/source-build.mdx +0 -1
  86. package/docs/zh/configure/app/output/assets-retry.mdx +1 -1
  87. package/docs/zh/configure/app/output/disable-inline-runtime-chunk.mdx +2 -2
  88. package/docs/zh/configure/app/output/filename.mdx +2 -4
  89. package/docs/zh/configure/app/output/temp-dir.mdx +3 -3
  90. package/docs/zh/configure/app/performance/build-cache.mdx +1 -1
  91. package/docs/zh/configure/app/performance/profile.mdx +1 -1
  92. package/docs/zh/configure/app/plugins.mdx +1 -2
  93. package/docs/zh/configure/app/runtime/router.mdx +0 -4
  94. package/docs/zh/configure/app/security/sri.mdx +0 -1
  95. package/docs/zh/configure/app/source/alias.mdx +1 -1
  96. package/docs/zh/configure/app/source/enable-async-entry.mdx +1 -1
  97. package/docs/zh/configure/app/source/include.mdx +2 -16
  98. package/docs/zh/configure/app/tools/dev-server.mdx +5 -5
  99. package/docs/zh/configure/app/usage.mdx +0 -12
  100. package/docs/zh/guides/advanced-features/bff/_meta.json +9 -1
  101. package/docs/zh/guides/advanced-features/bff/frameworks.mdx +2 -16
  102. package/docs/zh/guides/advanced-features/bff/operators.mdx +628 -0
  103. package/docs/zh/guides/advanced-features/bff/sdk.mdx +19 -12
  104. package/docs/zh/guides/advanced-features/bff/upload.mdx +3 -1
  105. package/docs/zh/guides/advanced-features/international/configuration.mdx +7 -16
  106. package/docs/zh/guides/advanced-features/international/quick-start.mdx +2 -25
  107. package/docs/zh/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
  108. package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +18 -4
  109. package/docs/zh/guides/advanced-features/server-monitor/monitors.mdx +60 -5
  110. package/docs/zh/guides/basic-features/data/data-cache.mdx +47 -54
  111. package/docs/zh/guides/basic-features/data/data-fetch.mdx +9 -12
  112. package/docs/zh/guides/basic-features/debug/proxy.mdx +4 -7
  113. package/docs/zh/guides/basic-features/render/rsc.mdx +23 -37
  114. package/docs/zh/guides/basic-features/render/ssr-cache.mdx +0 -4
  115. package/docs/zh/guides/basic-features/static-assets/svg-assets.mdx +0 -4
  116. package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
  117. package/docs/zh/guides/troubleshooting/builder.mdx +1 -1
  118. package/docs/zh/guides/upgrade/config.mdx +132 -1
  119. package/docs/zh/plugin/_meta.json +5 -0
  120. package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
  121. package/docs/zh/plugin/cli-plugins/api.mdx +15 -65
  122. package/docs/zh/plugin/cli-plugins/life-cycle.mdx +0 -4
  123. package/docs/zh/plugin/introduction.mdx +4 -16
  124. package/docs/zh/plugin/plugin-system.mdx +3 -14
  125. package/docs/zh/plugin/runtime-plugins/_meta.json +1 -1
  126. package/docs/zh/plugin/runtime-plugins/api.mdx +1 -1
  127. package/docs/zh/plugin/server-plugins/_meta.json +1 -0
  128. package/docs/zh/plugin/server-plugins/api.mdx +210 -1
  129. package/docs/zh/plugin/server-plugins/life-cycle.mdx +41 -1
  130. package/package.json +2 -2
  131. package/src/components/FrameworkCode/index.tsx +605 -0
  132. package/docs/en/configure/app/performance/bundle-analyze.mdx +0 -24
  133. package/docs/en/configure/app/tools/babel.mdx +0 -225
  134. package/docs/en/plugin/cli-plugins/migration.mdx +0 -83
  135. package/docs/en/plugin/runtime-plugins/migration.mdx +0 -110
  136. package/docs/zh/components/default-mwa-generate.mdx +0 -4
  137. package/docs/zh/configure/app/performance/bundle-analyze.mdx +0 -24
  138. package/docs/zh/configure/app/tools/babel.mdx +0 -224
  139. package/docs/zh/plugin/cli-plugins/migration.mdx +0 -83
  140. package/docs/zh/plugin/runtime-plugins/migration.mdx +0 -110
  141. /package/docs/en/components/{router-legacy-tip.mdx → upgrade-config-deploy.mdx} +0 -0
  142. /package/docs/zh/components/{router-legacy-tip.mdx → upgrade-config-deploy.mdx} +0 -0
@@ -1,3 +1,212 @@
1
1
  # Plugin API
2
2
 
3
- Comming soon...
3
+ Modern.js's Server plugins allow you to extend and customize functionality during the server-side request processing phase, such as adding middleware, modifying request responses, etc.
4
+
5
+ :::info
6
+
7
+ Server plugins need to be configured via the `plugins` field in `server/modern.server.ts`.
8
+
9
+ :::
10
+
11
+ ## Plugin Basic Structure
12
+
13
+ A typical Server plugin structure is as follows:
14
+
15
+ ```typescript
16
+ import type { ServerPlugin } from '@modern-js/server-runtime';
17
+
18
+ const myServerPlugin = (): ServerPlugin => ({
19
+ name: '@my-org/my-server-plugin', // Plugin name, ensure uniqueness
20
+ setup: api => {
21
+ // Use the API here to register hooks, add middleware, etc.
22
+ api.onPrepare(() => {
23
+ const { middlewares } = api.getServerContext();
24
+ middlewares?.push({
25
+ name: 'my-middleware',
26
+ handler: async (c, next) => {
27
+ console.log('Processing request...');
28
+ await next();
29
+ },
30
+ });
31
+ });
32
+ },
33
+ });
34
+
35
+ export default myServerPlugin;
36
+ ```
37
+
38
+ - `name`: A unique identifier for the plugin.
39
+ - The `setup` function receives an `api` object, which provides all available Server plugin APIs.
40
+
41
+ ## Information Retrieval
42
+
43
+ #### `api.getServerContext`
44
+
45
+ Gets the context information of the Modern.js server.
46
+
47
+ - **Returns:** A `ServerContext` object containing the following fields:
48
+
49
+ | Field Name | Type | Description |
50
+ | ------------------- | ----------------- | ------------------------------------- |
51
+ | `middlewares` | `MiddlewareObj[]` | Middleware list |
52
+ | `renderMiddlewares` | `MiddlewareObj[]` | Render middleware list |
53
+ | `routes` | `ServerRoute[]` | Server routing information |
54
+ | `appDirectory` | `string` | Absolute path to the project root |
55
+ | `apiDirectory` | `string` | Absolute path to the API module dir |
56
+ | `lambdaDirectory` | `string` | Absolute path to the Lambda module dir|
57
+ | `sharedDirectory` | `string` | Absolute path to the shared module dir|
58
+ | `distDirectory` | `string` | Absolute path to the output directory|
59
+ | `plugins` | `ServerPlugin[]` | List of currently registered plugins |
60
+
61
+ - **Example:**
62
+
63
+ ```typescript
64
+ api.onPrepare(() => {
65
+ const serverContext = api.getServerContext();
66
+ console.log(`App directory: ${serverContext.appDirectory}`);
67
+ console.log(`${serverContext.plugins.length} plugins registered`);
68
+ });
69
+ ```
70
+
71
+ :::info
72
+ The context information returned by `getServerContext` is read-only. Use `updateServerContext` if you need to modify it.
73
+ :::
74
+
75
+ ---
76
+
77
+ #### `api.getServerConfig`
78
+
79
+ Gets the server configuration defined by the user in the `server/modern.server.ts` file.
80
+
81
+ - **Returns:** The user-defined server configuration object.
82
+ - **Example:**
83
+
84
+ ```typescript
85
+ api.onPrepare(() => {
86
+ const serverConfig = api.getServerConfig();
87
+ if (serverConfig.middlewares) {
88
+ console.log('User has customized middleware configuration');
89
+ }
90
+ });
91
+ ```
92
+
93
+ ---
94
+
95
+ #### `api.getHooks`
96
+
97
+ Gets all registered hook functions.
98
+
99
+ - **Returns:** An object containing all hook functions.
100
+ - **Example:**
101
+
102
+ ```typescript
103
+ const hooks = api.getHooks();
104
+ // Manually trigger the onPrepare hook
105
+ await hooks.onPrepare.call();
106
+ ```
107
+
108
+ :::warning
109
+ In custom plugins, you can only manually call the hooks registered by the corresponding plugin and cannot call official hooks to avoid affecting the normal execution order of the application.
110
+ :::
111
+
112
+ ---
113
+
114
+
115
+ ## Context Modification
116
+
117
+ #### `api.updateServerContext`
118
+
119
+ Updates the server context information.
120
+
121
+ - **Type:** `api.updateServerContext(updateContext: DeepPartial<ServerContext>)`
122
+ - **Parameters:**
123
+ - `updateContext`: The context object to update (partial update).
124
+ - **Execution Phase:** Can be used at any stage.
125
+ - **Example:**
126
+
127
+ ```typescript
128
+ api.onPrepare(() => {
129
+ const context = api.getServerContext();
130
+ api.updateServerContext({
131
+ middlewares: [
132
+ ...context.middlewares,
133
+ {
134
+ name: 'new-middleware',
135
+ handler: async (c, next) => {
136
+ await next();
137
+ },
138
+ },
139
+ ],
140
+ });
141
+ });
142
+ ```
143
+
144
+ ---
145
+
146
+
147
+ ## Lifecycle Hooks
148
+
149
+ #### `api.onPrepare`
150
+
151
+ Adds additional logic during the server preparation phase.
152
+
153
+ - **Type:** `api.onPrepare(prepareFn: () => void | Promise<void>)`
154
+ - **Parameters:**
155
+ - `prepareFn`: A preparation function, without parameters, can be asynchronous.
156
+ - **Execution Phase:** After the server completes configuration validation and before applying middleware.
157
+ - **Example:**
158
+
159
+ ```typescript
160
+ api.onPrepare(async () => {
161
+ const { middlewares } = api.getServerContext();
162
+
163
+ // Add custom middleware
164
+ middlewares.push({
165
+ name: 'request-logger',
166
+ handler: async (c, next) => {
167
+ const start = Date.now();
168
+ await next();
169
+ const duration = Date.now() - start;
170
+ console.log(`Request duration: ${duration}ms`);
171
+ },
172
+ });
173
+ });
174
+ ```
175
+
176
+ :::info
177
+ In the `onPrepare` hook, you can modify the context object returned by `getServerContext()` (such as `middlewares`, `renderMiddlewares`), and these modifications will take effect when the server starts.
178
+ :::
179
+
180
+ ---
181
+
182
+ #### `api.onReset`
183
+
184
+ Adds additional logic when the server resets.
185
+
186
+ - **Type:** `api.onReset(resetFn: (params: { event: ResetEvent }) => void | Promise<void>)`
187
+ - **Parameters:**
188
+ - `resetFn`: A reset handler function that receives reset event parameters.
189
+ - `event.type`: Event type, possible values:
190
+ - `'repack'`: Repack event
191
+ - `'file-change'`: File change event
192
+ - `event.payload`: When `type` is `'file-change'`, contains an array of file change information.
193
+ - **Execution Phase:** When files change or repack is needed.
194
+ - **Example:**
195
+
196
+ ```typescript
197
+ api.onReset(async ({ event }) => {
198
+ if (event.type === 'file-change') {
199
+ console.log('File changes detected:', event.payload);
200
+ // Perform cleanup or re-initialization operations
201
+ } else if (event.type === 'repack') {
202
+ }
203
+ });
204
+ ```
205
+
206
+ ---
207
+
208
+
209
+ ## Other Notes
210
+
211
+ - Refer to [Server Plugin Lifecycle](./life-cycle.mdx) to understand the execution order of plugin hooks.
212
+ - The execution order of middleware can be controlled through the `order` field (`'pre'`, `'default'`, `'post'`), or through the `before` field to specify execution before other middleware.
@@ -1,3 +1,43 @@
1
1
  # Life Cycle
2
2
 
3
- Comming soon...
3
+ import Mermaid from '@site/src/components/Mermaid';
4
+
5
+ <Mermaid>
6
+ {`
7
+ flowchart TD
8
+ init{{"Server Initialization"}}
9
+ load_config(Load User Config File)
10
+ server_plugin(Load Server Plugins<br><sub>Plugins registered in config file<br>Server plugins registered by CLI plugins<br>Plugins used by plugins</sub>)
11
+ registry_hooks(Register Hooks Functions<br><sub>Execute plugin setup function, register Hooks defined in plugins, logic in plugin setup will also execute here</sub>)
12
+
13
+ modifyConfig(modifyConfig<br><sub>Modify server configuration</sub>)
14
+ onPrepare(onPrepare<br><sub>Execute after server completes configuration validation, before applying middleware</sub>)
15
+ apply_middlewares(Apply Middleware<br><sub>Apply middleware according to order and before rules</sub>)
16
+
17
+ init --> load_config
18
+ load_config --> server_plugin
19
+ server_plugin --> registry_hooks
20
+ registry_hooks --> modifyConfig
21
+ modifyConfig --> onPrepare
22
+ onPrepare --> apply_middlewares
23
+
24
+ style init fill:#FDE68A,font-size:10px;
25
+ style load_config stroke-dasharray:5 5,fill:#86EFAC,font-size:10px;
26
+ style server_plugin stroke-dasharray:5 5,fill:#86EFAC,font-size:10px;
27
+ style registry_hooks stroke-dasharray:5 5,fill:#9CA3AF,font-size:10px;
28
+ style modifyConfig font-size:10px;
29
+ style onPrepare font-size:10px;
30
+ style apply_middlewares font-size:10px;
31
+ `}
32
+ </Mermaid>
33
+
34
+ :::info
35
+
36
+ **Runtime Hooks**
37
+
38
+ The `onReset` hook is triggered at runtime when files change or code recompilation (rspack) is needed. It is not part of the initialization flow, so it is not shown in the diagram above.
39
+
40
+ - **Trigger Timing**: File changes (`event.type: 'file-change'`) or code recompilation (rspack) completion (`event.type: 'repack'`)
41
+ - **Use Cases**: Clearing cache, re-initializing resources, etc.
42
+
43
+ :::
@@ -19,7 +19,6 @@ Options:
19
19
  -e --entry <entry> 指定入口,只编译特定的页面
20
20
  -c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
21
21
  -h, --help 显示命令帮助
22
- --analyze 分析构建产物体积,查看各个模块打包后的大小
23
22
  --web-only 仅启动 Web 服务
24
23
  --api-only 仅启动 API 接口服务
25
24
  ```
@@ -79,31 +78,8 @@ Options:
79
78
  -c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
80
79
  -h, --help 显示命令帮助
81
80
  -w --watch 开启 watch 模式, 监听文件变更并重新构建
82
- --analyze 分析构建产物体积,查看各个模块打包后的大小
83
81
  ```
84
82
 
85
- ### 分析构建产物体积
86
-
87
- 执行 `npx modern build --analyze` 命令,可以在打包生产环境代码的同时,产出一个分析构建产物体积的 HTML 文件:
88
-
89
- ```
90
- Bundle Analyzer saved report to /example/dist/report.html
91
-
92
- info Production file sizes:
93
-
94
- File Size Gzipped
95
- dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
96
- dist/html/main/index.html 5.8 kB 2.5 kB
97
- dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
98
- dist/static/css/main.03221f72.css 1.4 kB 741 B
99
- ```
100
-
101
- 手动在浏览器中打开上述 HTML 文件,可以看到打包产物的瓦片图,并进行包体积分析和优化:
102
-
103
- <img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/mwa-build-analyze-8784f762c1ab0cb20935829d5f912c4c.png" />
104
-
105
- > 该功能基于 [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) 实现。
106
-
107
83
  ## modern new
108
84
 
109
85
  `modern new` 命令用于在已有项目中添加项目元素。
@@ -174,7 +150,7 @@ Options:
174
150
 
175
151
  ## modern inspect
176
152
 
177
- `modern inspect` 命令用于查看项目的 Modern.js 配置、[Rsbuild 配置](https://rsbuild.rs/zh/config/index) 以及 webpack 或 Rspack 配置。
153
+ `modern inspect` 命令用于查看项目的 Modern.js 配置、[Rsbuild 配置](https://rsbuild.rs/zh/config/index) 以及 Rspack 配置。
178
154
 
179
155
  ```bash
180
156
  Usage: modern inspect [options]
@@ -191,7 +167,7 @@ Options:
191
167
 
192
168
  - `modern.js.config.mjs`: 表示当前使用的 Modern.js 配置。
193
169
  - `rsbuild.config.mjs`: 表示在构建时使用的 Rsbuild 配置。
194
- - `webpack.config.web.mjs`: 表示在构建时使用的 webpack 配置。
170
+ - `rspack.config.web.mjs`: 表示在构建时使用的 Rspack 配置。
195
171
 
196
172
  ```bash
197
173
  ➜ npx modern inspect
@@ -199,7 +175,7 @@ Options:
199
175
  Inspect config succeed, open following files to view the content:
200
176
 
201
177
  - Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs
202
- - Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
178
+ - Rspack Config (web): /root/my-project/dist/rspack.config.web.mjs
203
179
  - Modern.js Config: /root/my-project/dist/modern.js.config.mjs
204
180
  ```
205
181
 
@@ -221,7 +197,7 @@ modern inspect --verbose
221
197
 
222
198
  ### SSR 构建配置
223
199
 
224
- 如果项目开启了 SSR 能力,则在 `dist` 目录会另外生成一份 `webpack.config.node.mjs` 文件,对应 SSR 构建时的 webpack 配置。
200
+ 如果项目开启了 SSR 能力,则在 `dist` 目录会另外生成一份 `rspack.config.node.mjs` 文件,对应 SSR 构建时的 Rspack 配置。
225
201
 
226
202
  ```bash
227
203
  ➜ npx modern inspect
@@ -229,8 +205,8 @@ modern inspect --verbose
229
205
  Inspect config succeed, open following files to view the content:
230
206
 
231
207
  - Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs
232
- - Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
233
- - Webpack Config (node): /root/my-project/dist/webpack.config.node.mjs
208
+ - Rspack Config (web): /root/my-project/dist/rspack.config.web.mjs
209
+ - Rspack Config (node): /root/my-project/dist/rspack.config.node.mjs
234
210
  - Modern.js Config: /root/my-project/dist/modern.js.config.mjs
235
211
  ```
236
212
 
@@ -0,0 +1,5 @@
1
+ import FrameworkCode from '@site/src/components/FrameworkCode';
2
+
3
+ <FrameworkCode sourcePath="@modern-js/plugin-bff/server">
4
+ {props.children}
5
+ </FrameworkCode>
@@ -1,5 +1,3 @@
1
- BFF 搭配运行时框架提供了文件上传能力,支持一体化调用及纯函数手动调用。
2
-
3
1
  ### BFF 函数
4
2
 
5
3
  首先创建 `api/lambda/upload.ts` 文件:
@@ -1,3 +1,3 @@
1
- 指 [webpack](https://webpack.js.org/)、[Rspack](https://rspack.rs/) 等模块打包工具。
1
+ 指 [Rspack](https://rspack.rs/) 等模块打包工具。
2
2
 
3
3
  打包工具的主要目标是将 JavaScript、CSS 等文件打包在一起,打包后的文件可以在浏览器、Node.js 等环境中使用。当 Bundler 处理 Web 应用时,它会构建一个依赖关系图,其中包含应用需要的各个模块,然后将所有模块打包成一个或多个 bundle。
@@ -23,7 +23,7 @@ pnpm add @modern-js/plugin-bff@<版本号>
23
23
 
24
24
  :::
25
25
 
26
- 2. 配置 modern.config.ts
26
+ 2. 配置 `modern.config.ts`
27
27
 
28
28
  在 `modern.config.[tj]s` 文件中导入并添加 BFF 插件:
29
29
 
@@ -47,6 +47,10 @@ export default defineConfig({
47
47
  ..., // other paths,
48
48
  "@api/*": ["./api/lambda/*"]
49
49
  }
50
- }
50
+ },
51
+ "include": [
52
+ ..., // 其他 include 目录
53
+ "api" // 添加 api 目录
54
+ ]
51
55
  }
52
56
  ```
@@ -9,6 +9,7 @@ pnpm add @modern-js/plugin-ssg
9
9
  ```
10
10
 
11
11
  :::tip 版本一致性
12
+
12
13
  请确保 `@modern-js/plugin-ssg` 的版本与项目中 `@modern-js/app-tools` 的版本保持一致。Modern.js 的所有官方包使用统一版本号发布,版本不一致可能导致兼容性问题。
13
14
 
14
15
  请先查看 `@modern-js/app-tools` 的版本,然后安装相同版本的 `@modern-js/plugin-ssg`:
@@ -44,4 +45,5 @@ export default defineConfig({
44
45
  - 单入口请使用 `output.ssg`。
45
46
  - 多入口请使用 `output.ssgByEntries`。
46
47
  - 当仅设置 `output.ssg: true` 且未配置 `output.ssgByEntries` 时,所有入口下的所有路由都会作为 SSG 路由处理。
47
- :::
48
+
49
+ :::
@@ -1,3 +1,3 @@
1
- [esbuild](https://esbuild.github.io/) 是一款基于 Golang 开发的前端构建工具,具有打包、编译和压缩 JavaScript 代码的功能,相比传统的打包编译工具,esbuild 在性能上有显著提升。在代码压缩方面,相比 webpack 内置的 terser 压缩器,esbuild 在性能上有数十倍的提升。
1
+ [esbuild](https://esbuild.github.io/) 是一款基于 Golang 开发的前端构建工具,具有打包、编译和压缩 JavaScript 代码的功能,相比传统的打包编译工具,esbuild 在性能上有显著提升。在代码压缩方面,esbuild 在性能上有数十倍的提升。
2
2
 
3
- Modern.js 提供了 esbuild 插件,让你能使用 esbuild 代替 babel-loader、ts-loader 和 terser 等库进行代码编译和压缩。在大型工程中启用 esbuild 后,**可以大幅度减少代码编译和压缩所需的时间,同时有效避免 OOM (heap out of memory) 问题**。
3
+ Modern.js 提供了 esbuild 插件,让你能使用 esbuild 代替 babel-loader、ts-loader 等库进行代码编译和压缩。在大型工程中启用 esbuild 后,**可以大幅度减少代码编译和压缩所需的时间,同时有效避免 OOM (heap out of memory) 问题**。
@@ -1,5 +1,3 @@
1
- 普通的 BFF 函数写法有时并不能满足需求,我们正在设计一套更强大的 BFF 函数写法,让开发者更方便地扩展 BFF 函数。
1
+ 普通的 BFF 函数写法有时并不能满足需求,例如业务场景下复杂的 TS 类型需要,EdenX 提供了一种更强大的 BFF 函数写法。
2
2
 
3
- :::note
4
- 敬请期待
5
- :::
3
+ 详细内容可参考 - [创建可扩展的 BFF 函数](/guides/advanced-features/bff/operators)。
@@ -0,0 +1,119 @@
1
+ ### 获取请求上下文
2
+
3
+ 在 BFF 函数中,有时需要获取请求上下文,来处理更多逻辑。此时,你可以通过 `useHonoContext` 来获取:
4
+
5
+ ```ts title="api/lambda/hello.ts"
6
+ import { useHonoContext } from '@modern-js/plugin-bff/server';
7
+
8
+ export const get = async () => {
9
+ const c = useHonoContext();
10
+ console.info(`access url: ${c.req.url}`);
11
+ return 'Hello Modern.js';
12
+ };
13
+ ```
14
+
15
+ :::info
16
+ 详细内容可以参考 [useHonoContext](/apis/app/runtime/bff/use-hono-context)。
17
+ :::
18
+
19
+ ### 获取 Cookie
20
+
21
+ 在 BFF 函数中获取 Cookie 时,需要通过 `useHonoContext` 获取请求上下文,然后使用 `c.req.header('cookie')` 获取 Cookie 字符串并手动解析:
22
+
23
+ ```ts title="api/lambda/cookies.ts"
24
+ import { Api, Get, useHonoContext } from '@modern-js/plugin-bff/server';
25
+
26
+ // 解析 Cookie 字符串的辅助函数
27
+ function parseCookies(
28
+ cookieHeader: string | undefined,
29
+ ): Record<string, string> {
30
+ const cookies: Record<string, string> = {};
31
+ if (!cookieHeader) return cookies;
32
+
33
+ cookieHeader.split(';').forEach(cookie => {
34
+ const [name, ...rest] = cookie.trim().split('=');
35
+ if (name) {
36
+ cookies[name] = rest.join('=');
37
+ }
38
+ });
39
+
40
+ return cookies;
41
+ }
42
+
43
+ export const getCookies = Api(Get('/cookies'), async () => {
44
+ const c = useHonoContext();
45
+ const cookieHeader = c.req.header('cookie');
46
+ const cookies = parseCookies(cookieHeader);
47
+ const token = cookies.token;
48
+ const sessionId = cookies.sessionId;
49
+ return {
50
+ hasToken: !!token,
51
+ token: token || null,
52
+ sessionId: sessionId || null,
53
+ };
54
+ });
55
+ ```
56
+
57
+ :::caution 注意
58
+ `c.req.cookie()` 方法在当前版本中不存在,需要使用 `c.req.header('cookie')` 获取 Cookie 字符串,然后手动解析。
59
+ :::
60
+
61
+ ### 定义 BFF 函数
62
+
63
+ 使用 Hono 作为运行时框架时,可以通过 [Api 函数](/guides/advanced-features/bff/operators.html) 定义接口:
64
+
65
+ ```ts title="api/lambda/user.ts"
66
+ import { Api, Get, Query } from '@modern-js/plugin-bff/server';
67
+ import { z } from 'zod';
68
+
69
+ const QuerySchema = z.object({
70
+ id: z.string(),
71
+ });
72
+
73
+ export const getUser = Api(
74
+ Get('/user'),
75
+ Query(QuerySchema),
76
+ async ({ query }) => {
77
+ return {
78
+ id: query.id,
79
+ name: 'Modern.js',
80
+ email: 'modernjs@bytedance.com',
81
+ };
82
+ },
83
+ );
84
+ ```
85
+
86
+ :::info
87
+ 更多关于 Api 函数和操作符的详细内容,可以参考 [创建可扩展的 BFF 函数](/guides/advanced-features/bff/operators.html)。
88
+ :::
89
+
90
+ ### 使用中间件
91
+
92
+ Hono 支持丰富的中间件生态,可以在 BFF 函数中使用中间件:
93
+
94
+ ```ts title="api/lambda/user.ts"
95
+ import { Api, Get, Middleware } from '@modern-js/plugin-bff/server';
96
+
97
+ export const getUser = Api(
98
+ Get('/user'),
99
+ Middleware(async (c, next) => {
100
+ // 在中间件中可以访问 Hono 的 Context
101
+ c.res.headers.set('X-Powered-By', 'Modern.js');
102
+ await next();
103
+ }),
104
+ async () => {
105
+ return {
106
+ name: 'Modern.js',
107
+ email: 'modernjs@bytedance.com',
108
+ };
109
+ },
110
+ );
111
+ ```
112
+
113
+ :::info
114
+ 更多关于中间件的详细内容,可以参考 [创建可扩展的 BFF 函数](/guides/advanced-features/bff/operators.html#中间件-middleware)。
115
+ :::
116
+
117
+ ### 更多 Hono 文档
118
+
119
+ 更多关于 Hono 的详细信息可查看 [Hono 官方文档](https://hono.dev/)。
@@ -0,0 +1,16 @@
1
+ ```ts
2
+ import { defineRuntimeConfig } from '@modern-js/runtime';
3
+ import i18next from 'i18next';
4
+
5
+ // 创建自定义实例
6
+ const customI18n = i18next.createInstance({
7
+ // 自定义配置
8
+ });
9
+
10
+ export default defineRuntimeConfig({
11
+ i18n: {
12
+ i18nInstance: customI18n,
13
+ },
14
+ });
15
+ ```
16
+
@@ -0,0 +1 @@
1
+ `initOptions` 会传递给 i18next 的 `init` 方法,支持所有 i18next 配置选项:
@@ -2,6 +2,21 @@
2
2
  pnpm add @modern-js/plugin-i18n i18next react-i18next
3
3
  ```
4
4
 
5
+ :::tip 版本一致性
6
+ 请确保 `@modern-js/plugin-i18n` 的版本与项目中 `@modern-js/app-tools` 的版本一致。所有 Modern.js 官方包都使用统一的版本号发布,版本不匹配可能会导致兼容性问题。
7
+
8
+ 请先检查 `@modern-js/app-tools` 的版本,然后安装相同版本的 `@modern-js/plugin-i18n`:
9
+
10
+ ```bash
11
+ # 检查当前 @modern-js/app-tools 的版本
12
+ pnpm list @modern-js/app-tools
13
+
14
+ # 安装相同版本的 @modern-js/plugin-i18n
15
+ pnpm add @modern-js/plugin-i18n@<version> i18next react-i18next
16
+ ```
17
+
18
+ :::
19
+
5
20
  :::info
6
21
  `i18next` 和 `react-i18next` 是 peer dependencies,需要手动安装。
7
22
 
@@ -0,0 +1,26 @@
1
+ 创建 `src/modern.runtime.ts` 文件并配置 i18n 运行时选项:
2
+
3
+ ```ts
4
+ import { defineRuntimeConfig } from '@modern-js/runtime';
5
+ import i18next from 'i18next';
6
+
7
+ // 建议创建一个新的 i18next 实例,避免使用全局默认实例
8
+ const i18nInstance = i18next.createInstance();
9
+
10
+ export default defineRuntimeConfig({
11
+ i18n: {
12
+ i18nInstance: i18nInstance,
13
+ initOptions: {
14
+ fallbackLng: 'en',
15
+ supportedLngs: ['zh', 'en'],
16
+ },
17
+ },
18
+ });
19
+ ```
20
+
21
+ :::info
22
+ `modern.runtime.ts` 是运行时配置文件,用于配置 i18next 的初始化选项。即使是最基础的配置,也建议创建此文件以确保插件正常工作。
23
+
24
+ 建议使用 `i18next.createInstance()` 创建一个新的实例,而不是直接使用默认导出的 `i18next`,这样可以避免全局状态污染,并确保每个应用都有独立的 i18n 实例。
25
+
26
+ :::
@@ -18,8 +18,7 @@ Rsbuild 是 Modern.js 底层的构建工具,请阅读 [构建能力](/guides/c
18
18
  该选项**用于配置 Rsbuild 构建插件**,如果你需要配置其他类型的插件,请选择对应的配置方式:
19
19
 
20
20
  - 配置 Modern.js 框架插件,请使用 [plugins](/configure/app/plugins) 配置项。
21
- - 配置 Rspack 或 webpack 插件,请使用 [tools.bundlerChain](/configure/app/tools/bundler-chain) 配置项。
22
- - 配置 Babel 插件,请使用 [tools.babel](/configure/app/tools/babel) 配置项。
21
+ - 配置 Rspack 插件,请使用 [tools.bundlerChain](/configure/app/tools/bundler-chain) 配置项。
23
22
 
24
23
  ## 何时使用
25
24