@modern-js/main-doc 2.67.4 → 2.67.6

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 (198) hide show
  1. package/docs/en/apis/app/hooks/server/server.mdx +10 -0
  2. package/docs/en/apis/app/hooks/src/routes.mdx +3 -3
  3. package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +30 -0
  4. package/docs/en/components/enable-bff.mdx +1 -27
  5. package/docs/en/components/rsbuild-config-tooltip.mdx +2 -2
  6. package/docs/en/components/tech-stack-node-framework.mdx +1 -1
  7. package/docs/en/configure/app/dev/asset-prefix.mdx +2 -3
  8. package/docs/en/configure/app/dev/client.mdx +2 -3
  9. package/docs/en/configure/app/dev/hmr.mdx +2 -3
  10. package/docs/en/configure/app/dev/live-reload.mdx +2 -3
  11. package/docs/en/configure/app/dev/progress-bar.mdx +2 -3
  12. package/docs/en/configure/app/dev/setup-middlewares.mdx +2 -3
  13. package/docs/en/configure/app/dev/watch-files.mdx +2 -3
  14. package/docs/en/configure/app/dev/write-to-disk.mdx +2 -3
  15. package/docs/en/configure/app/html/app-icon.mdx +2 -3
  16. package/docs/en/configure/app/html/crossorigin.mdx +2 -3
  17. package/docs/en/configure/app/html/favicon.mdx +2 -3
  18. package/docs/en/configure/app/html/inject.mdx +2 -3
  19. package/docs/en/configure/app/html/meta.mdx +2 -3
  20. package/docs/en/configure/app/html/mount-id.mdx +2 -3
  21. package/docs/en/configure/app/html/output-structure.mdx +2 -3
  22. package/docs/en/configure/app/html/script-loading.mdx +2 -3
  23. package/docs/en/configure/app/html/tags.mdx +2 -3
  24. package/docs/en/configure/app/html/template-parameters.mdx +2 -3
  25. package/docs/en/configure/app/html/template.mdx +2 -3
  26. package/docs/en/configure/app/html/title.mdx +2 -3
  27. package/docs/en/configure/app/output/asset-prefix.mdx +2 -3
  28. package/docs/en/configure/app/output/charset.mdx +2 -3
  29. package/docs/en/configure/app/output/copy.mdx +2 -3
  30. package/docs/en/configure/app/output/css-modules.mdx +2 -3
  31. package/docs/en/configure/app/output/data-uri-limit.mdx +2 -3
  32. package/docs/en/configure/app/output/dist-path.mdx +2 -4
  33. package/docs/en/configure/app/output/externals.mdx +2 -3
  34. package/docs/en/configure/app/output/filename-hash.mdx +2 -3
  35. package/docs/en/configure/app/output/filename.mdx +2 -3
  36. package/docs/en/configure/app/output/inject-styles.mdx +2 -3
  37. package/docs/en/configure/app/output/inline-scripts.mdx +2 -3
  38. package/docs/en/configure/app/output/inline-styles.mdx +2 -3
  39. package/docs/en/configure/app/output/legal-comments.mdx +2 -3
  40. package/docs/en/configure/app/output/minify.mdx +2 -3
  41. package/docs/en/configure/app/output/override-browserslist.mdx +2 -3
  42. package/docs/en/configure/app/output/polyfill.mdx +2 -3
  43. package/docs/en/configure/app/output/source-map.mdx +2 -3
  44. package/docs/en/configure/app/performance/build-cache.mdx +2 -3
  45. package/docs/en/configure/app/performance/bundle-analyze.mdx +2 -3
  46. package/docs/en/configure/app/performance/chunk-split.mdx +2 -3
  47. package/docs/en/configure/app/performance/dns-prefetch.mdx +2 -3
  48. package/docs/en/configure/app/performance/preconnect.mdx +2 -3
  49. package/docs/en/configure/app/performance/prefetch.mdx +2 -3
  50. package/docs/en/configure/app/performance/preload.mdx +2 -3
  51. package/docs/en/configure/app/performance/print-file-size.mdx +2 -3
  52. package/docs/en/configure/app/performance/profile.mdx +2 -3
  53. package/docs/en/configure/app/performance/remove-console.mdx +2 -3
  54. package/docs/en/configure/app/performance/remove-moment-locale.mdx +2 -3
  55. package/docs/en/configure/app/plugins.mdx +13 -33
  56. package/docs/en/configure/app/runtime/master-app.mdx +1 -5
  57. package/docs/en/configure/app/runtime/plugins.mdx +58 -0
  58. package/docs/en/configure/app/security/nonce.mdx +2 -3
  59. package/docs/en/configure/app/security/sri.mdx +0 -1
  60. package/docs/en/configure/app/server/port.mdx +2 -3
  61. package/docs/en/configure/app/source/alias-strategy.mdx +2 -3
  62. package/docs/en/configure/app/source/alias.mdx +2 -3
  63. package/docs/en/configure/app/source/decorators.mdx +2 -3
  64. package/docs/en/configure/app/source/define.mdx +2 -3
  65. package/docs/en/configure/app/source/exclude.mdx +2 -3
  66. package/docs/en/configure/app/source/include.mdx +2 -3
  67. package/docs/en/configure/app/source/pre-entry.mdx +2 -3
  68. package/docs/en/configure/app/source/transform-import.mdx +2 -3
  69. package/docs/en/configure/app/tools/css-extract.mdx +2 -3
  70. package/docs/en/configure/app/tools/css-loader.mdx +2 -2
  71. package/docs/en/configure/app/tools/html-plugin.mdx +7 -3
  72. package/docs/en/configure/app/tools/lightningcss-loader.mdx +2 -3
  73. package/docs/en/configure/app/tools/postcss.mdx +2 -3
  74. package/docs/en/configure/app/tools/rspack.mdx +2 -3
  75. package/docs/en/configure/app/tools/style-loader.mdx +2 -3
  76. package/docs/en/configure/app/tools/swc.mdx +1 -1
  77. package/docs/en/configure/app/usage.mdx +1 -1
  78. package/docs/en/guides/advanced-features/bff/extend-server.mdx +33 -82
  79. package/docs/en/guides/advanced-features/bff/frameworks.mdx +12 -68
  80. package/docs/en/guides/advanced-features/bff.mdx +1 -1
  81. package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
  82. package/docs/en/guides/advanced-features/page-performance/_meta.json +1 -1
  83. package/docs/en/guides/advanced-features/page-performance/inline-assets.mdx +2 -0
  84. package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
  85. package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
  86. package/docs/en/guides/advanced-features/web-server.mdx +378 -14
  87. package/docs/en/guides/basic-features/data/data-fetch.mdx +2 -1
  88. package/docs/en/guides/basic-features/deploy.mdx +3 -3
  89. package/docs/en/guides/basic-features/html.mdx +3 -3
  90. package/docs/en/guides/basic-features/output-files.mdx +0 -28
  91. package/docs/en/guides/basic-features/render/ssr.mdx +2 -2
  92. package/docs/en/guides/concept/entries.mdx +1 -1
  93. package/docs/en/plugin/cli-plugins/api.mdx +6 -0
  94. package/docs/en/plugin/runtime-plugins/api.mdx +37 -12
  95. package/docs/en/tutorials/first-app/c04-routes.mdx +4 -2
  96. package/docs/en/tutorials/first-app/c05-loader.mdx +5 -2
  97. package/docs/zh/apis/app/hooks/server/server.mdx +10 -0
  98. package/docs/zh/apis/app/hooks/src/routes.mdx +3 -3
  99. package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +31 -0
  100. package/docs/zh/components/enable-bff.mdx +2 -27
  101. package/docs/zh/components/rsbuild-config-tooltip.mdx +2 -2
  102. package/docs/zh/components/tech-stack-node-framework.mdx +1 -1
  103. package/docs/zh/configure/app/dev/asset-prefix.mdx +2 -3
  104. package/docs/zh/configure/app/dev/client.mdx +2 -3
  105. package/docs/zh/configure/app/dev/hmr.mdx +2 -3
  106. package/docs/zh/configure/app/dev/live-reload.mdx +2 -3
  107. package/docs/zh/configure/app/dev/progress-bar.mdx +2 -3
  108. package/docs/zh/configure/app/dev/setup-middlewares.mdx +2 -3
  109. package/docs/zh/configure/app/dev/watch-files.mdx +2 -3
  110. package/docs/zh/configure/app/dev/write-to-disk.mdx +2 -3
  111. package/docs/zh/configure/app/html/app-icon.mdx +2 -3
  112. package/docs/zh/configure/app/html/crossorigin.mdx +2 -3
  113. package/docs/zh/configure/app/html/favicon.mdx +2 -3
  114. package/docs/zh/configure/app/html/inject.mdx +2 -3
  115. package/docs/zh/configure/app/html/meta.mdx +2 -3
  116. package/docs/zh/configure/app/html/mount-id.mdx +2 -3
  117. package/docs/zh/configure/app/html/output-structure.mdx +2 -3
  118. package/docs/zh/configure/app/html/script-loading.mdx +2 -3
  119. package/docs/zh/configure/app/html/tags.mdx +2 -3
  120. package/docs/zh/configure/app/html/template-parameters.mdx +2 -3
  121. package/docs/zh/configure/app/html/template.mdx +2 -3
  122. package/docs/zh/configure/app/html/title.mdx +2 -3
  123. package/docs/zh/configure/app/output/asset-prefix.mdx +2 -3
  124. package/docs/zh/configure/app/output/charset.mdx +2 -3
  125. package/docs/zh/configure/app/output/copy.mdx +2 -3
  126. package/docs/zh/configure/app/output/css-modules.mdx +2 -3
  127. package/docs/zh/configure/app/output/data-uri-limit.mdx +2 -3
  128. package/docs/zh/configure/app/output/dist-path.mdx +2 -4
  129. package/docs/zh/configure/app/output/externals.mdx +2 -3
  130. package/docs/zh/configure/app/output/filename-hash.mdx +2 -3
  131. package/docs/zh/configure/app/output/filename.mdx +2 -3
  132. package/docs/zh/configure/app/output/inject-styles.mdx +2 -3
  133. package/docs/zh/configure/app/output/inline-scripts.mdx +2 -3
  134. package/docs/zh/configure/app/output/inline-styles.mdx +2 -3
  135. package/docs/zh/configure/app/output/legal-comments.mdx +2 -3
  136. package/docs/zh/configure/app/output/minify.mdx +2 -3
  137. package/docs/zh/configure/app/output/override-browserslist.mdx +2 -3
  138. package/docs/zh/configure/app/output/polyfill.mdx +2 -3
  139. package/docs/zh/configure/app/output/source-map.mdx +2 -3
  140. package/docs/zh/configure/app/performance/build-cache.mdx +2 -3
  141. package/docs/zh/configure/app/performance/bundle-analyze.mdx +2 -3
  142. package/docs/zh/configure/app/performance/chunk-split.mdx +2 -3
  143. package/docs/zh/configure/app/performance/dns-prefetch.mdx +2 -3
  144. package/docs/zh/configure/app/performance/preconnect.mdx +2 -3
  145. package/docs/zh/configure/app/performance/prefetch.mdx +2 -3
  146. package/docs/zh/configure/app/performance/preload.mdx +2 -3
  147. package/docs/zh/configure/app/performance/print-file-size.mdx +2 -3
  148. package/docs/zh/configure/app/performance/profile.mdx +2 -3
  149. package/docs/zh/configure/app/performance/remove-console.mdx +2 -3
  150. package/docs/zh/configure/app/performance/remove-moment-locale.mdx +2 -3
  151. package/docs/zh/configure/app/plugins.mdx +3 -24
  152. package/docs/zh/configure/app/runtime/master-app.mdx +1 -5
  153. package/docs/zh/configure/app/runtime/plugins.mdx +58 -0
  154. package/docs/zh/configure/app/security/nonce.mdx +2 -3
  155. package/docs/zh/configure/app/security/sri.mdx +0 -1
  156. package/docs/zh/configure/app/server/port.mdx +2 -3
  157. package/docs/zh/configure/app/source/alias-strategy.mdx +2 -3
  158. package/docs/zh/configure/app/source/alias.mdx +2 -3
  159. package/docs/zh/configure/app/source/decorators.mdx +2 -3
  160. package/docs/zh/configure/app/source/define.mdx +2 -3
  161. package/docs/zh/configure/app/source/exclude.mdx +2 -3
  162. package/docs/zh/configure/app/source/include.mdx +2 -3
  163. package/docs/zh/configure/app/source/pre-entry.mdx +2 -3
  164. package/docs/zh/configure/app/source/transform-import.mdx +2 -3
  165. package/docs/zh/configure/app/tools/css-extract.mdx +2 -3
  166. package/docs/zh/configure/app/tools/css-loader.mdx +2 -3
  167. package/docs/zh/configure/app/tools/html-plugin.mdx +6 -3
  168. package/docs/zh/configure/app/tools/lightningcss-loader.mdx +2 -3
  169. package/docs/zh/configure/app/tools/postcss.mdx +2 -3
  170. package/docs/zh/configure/app/tools/rspack.mdx +2 -3
  171. package/docs/zh/configure/app/tools/style-loader.mdx +2 -3
  172. package/docs/zh/configure/app/tools/swc.mdx +1 -1
  173. package/docs/zh/configure/app/usage.mdx +1 -1
  174. package/docs/zh/guides/advanced-features/bff/extend-server.mdx +28 -76
  175. package/docs/zh/guides/advanced-features/bff/frameworks.mdx +6 -66
  176. package/docs/zh/guides/advanced-features/page-performance/_meta.json +1 -1
  177. package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
  178. package/docs/zh/guides/advanced-features/web-server.mdx +375 -18
  179. package/docs/zh/guides/basic-features/deploy.mdx +4 -3
  180. package/docs/zh/guides/basic-features/output-files.mdx +0 -28
  181. package/docs/zh/plugin/cli-plugins/api.mdx +6 -0
  182. package/docs/zh/plugin/runtime-plugins/api.mdx +37 -12
  183. package/docs/zh/tutorials/first-app/c04-routes.mdx +4 -2
  184. package/docs/zh/tutorials/first-app/c05-loader.mdx +4 -1
  185. package/package.json +7 -4
  186. package/rspress.config.ts +16 -1
  187. package/src/components/RsbuildLink/index.tsx +2 -2
  188. package/src/i18n/index.ts +1 -1
  189. package/src/index.ts +1 -5
  190. package/src/pages/index.tsx +3 -3
  191. package/docs/en/apis/app/hooks/api/middleware.mdx +0 -11
  192. package/docs/en/apis/app/runtime/bff/hook.mdx +0 -44
  193. package/docs/en/apis/app/runtime/bff/use-context.mdx +0 -38
  194. package/docs/en/configure/app/bff/enable-handle-web.mdx +0 -24
  195. package/docs/zh/apis/app/hooks/api/middleware.mdx +0 -11
  196. package/docs/zh/apis/app/runtime/bff/hook.mdx +0 -44
  197. package/docs/zh/apis/app/runtime/bff/use-context.mdx +0 -38
  198. package/docs/zh/configure/app/bff/enable-handle-web.mdx +0 -24
@@ -1,6 +1,5 @@
1
1
  ---
2
2
  title: rspack
3
- configName: tools.rspack
4
3
  ---
5
4
 
6
5
  # tools.rspack
@@ -10,6 +9,6 @@ configName: tools.rspack
10
9
 
11
10
  `tools.rspack` 选项用于修改 [Rspack](https://rspack.dev/) 的配置项。
12
11
 
13
- import RsbuildConig from '@site-docs/components/rsbuild-config-tooltip';
12
+ import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
14
13
 
15
- <RsbuildConig />
14
+ <RsbuildConfig configName="tools.rspack" />
@@ -1,6 +1,5 @@
1
1
  ---
2
2
  title: styleLoader
3
- configName: tools.styleLoader
4
3
  ---
5
4
 
6
5
  # tools.styleLoader
@@ -10,6 +9,6 @@ configName: tools.styleLoader
10
9
 
11
10
  通过 `tools.styleLoader` 可以设置 [style-loader](https://github.com/webpack-contrib/style-loader) 的配置项。
12
11
 
13
- import RsbuildConig from '@site-docs/components/rsbuild-config-tooltip';
12
+ import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
14
13
 
15
- <RsbuildConig />
14
+ <RsbuildConfig configName="tools.styleLoader" />
@@ -21,7 +21,7 @@ import SWC from '@site-docs/components/swc.mdx';
21
21
 
22
22
  通过 `tools.swc` 可以设置 Rspack [builtin:swc-loader](https://rspack.dev/zh/guide/features/builtin-swc-loader) 的选项。
23
23
 
24
- ```
24
+ ```ts
25
25
  import { defineConfig } from '@modern-js/app-tools';
26
26
 
27
27
  export default defineConfig<'rspack'>({
@@ -13,7 +13,7 @@ Modern.js 不支持同时在 `package.json` 中和 `modern.config.ts` 中配置
13
13
 
14
14
  **运行时配置**可以在 `src/modern.runtime.(ts|js|mjs)` 文件中配置。
15
15
 
16
- **服务端运行时配置**可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)` 中进行配置。
16
+ **服务端运行时配置**可以在 `server/modern.server.(ts|js|mjs)` 中进行配置。
17
17
 
18
18
  ## 编译时配置
19
19
 
@@ -2,31 +2,39 @@
2
2
 
3
3
  部分应用中,开发者可能希望对所有 BFF 函数做统一的处理,例如鉴权、日志、数据处理等。
4
4
 
5
- Modern.js 提供了两种方式,允许开发者根据运行时框架自由扩展 BFF Server。
5
+ Modern.js 支持用户通过 [Middleware](/guides/advanced-features/web-server.html#middleware) 的方式来自由扩展 BFF Server。
6
6
 
7
- ## 中间件
8
7
 
9
- 开发者可以在编写 `api/_app.ts` 文件中编写中间件,用来扩展 BFF Server。以 Express 作为运行时框架为例,介绍如何手写一个中间件,添加权限校验:
8
+ ## 使用中间件
10
9
 
11
- ```ts title="api/_app.ts"
12
- import { hook } from '@modern-js/runtime/server';
13
- import { Request, Response, NextFunction } from 'express';
10
+ 开发者可以在 `server/modern.server.ts` 中通过 配置 middlewares 来使用中间件。下面介绍如何手写一个 BFF 中间件,添加权限校验:
14
11
 
15
- export default hook(({ addMiddleware }) => {
16
- addMiddleware(async (req: Request, res: Response, next: NextFunction) => {
17
- if (req.url !== '/api/login') {
18
- const sid = req?.cookies?.sid;
19
- if (!sid) {
20
- res.status(400);
21
- res.json({ code: -1, message: 'need login' });
22
- } else {
23
- next();
24
- }
25
- } else {
26
- next();
12
+
13
+ ```ts title="server/modern.server.ts"
14
+ import {
15
+ type MiddlewareHandler,
16
+ defineServerConfig,
17
+ } from '@modern-js/server-runtime';
18
+
19
+ const requireAuthForApi: MiddlewareHandler = async (c, next) => {
20
+ if (c.req.path.startsWith('/api') && c.req.path !== '/api/login') {
21
+ const sid = c.req.cookie('sid');
22
+ if (!sid) {
23
+ return c.json({ code: -1, message: 'need login' }, 400);
27
24
  }
28
- });
25
+ }
26
+ await next();
27
+ };
28
+
29
+ export default defineServerConfig({
30
+ middlewares: [
31
+ {
32
+ name: 'require-auth-for-api',
33
+ handler: requireAuthForApi,
34
+ },
35
+ ]
29
36
  });
37
+
30
38
  ```
31
39
 
32
40
 
@@ -97,60 +105,4 @@ export default () => {
97
105
 
98
106
  ![Network](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/network3.png)
99
107
 
100
- 以上代码模拟了在 `/api/_app.ts` 中添加中间件的方式,实现了简易的登录功能。同样,可以在这个钩子文件中实现其他功能来扩展 BFF Server。
101
-
102
- :::info
103
- 不同运行时框架中,中间件的写法不一定相同,详情可见[运行时框架](/guides/advanced-features/bff/frameworks)。
104
-
105
- :::
106
-
107
- ## 定义 Server 实例
108
-
109
- 除了中间件之外,还可以通过 `api/app.ts` 文件来定义 BFF Server 的实例。开发者需要默认导出一个能被运行时框架插件识别的实例。这里简单展示一下 Koa 和 Express 如何定义 Server 实例。
110
-
111
- import { Tabs, Tab as TabItem } from "@theme";
112
-
113
- <Tabs>
114
- <TabItem value="express" label="Express.js" default>
115
-
116
- ```ts title="api/app.ts"
117
- import express from 'express';
118
-
119
- const app = express();
120
- app.use(async (req, res, next) => {
121
- console.info(`access url: ${req.url}`);
122
- next();
123
- });
124
-
125
- export default app;
126
- ```
127
-
128
- </TabItem>
129
- <TabItem value="koa" label="Koa.js">
130
-
131
- ```ts title="api/app.ts"
132
- import koa from 'koa';
133
-
134
- const app = new Koa();
135
- app.use(async (ctx, next) => {
136
- console.info(`access url: ${ctx.url}`);
137
- await next();
138
- });
139
-
140
- export default app;
141
- ```
142
-
143
- </TabItem>
144
- </Tabs>
145
-
146
- 在复杂的 BFF 逻辑中,定义 Server 实例可以更方便通过一个入口来组织代码逻辑,设计目录结构。在这个文件中,可以执行初始化逻辑,添加全局中间件,声明路由,甚至扩展原有框架。
147
-
148
- BFF 函数定义的路由会在 `app.ts` 文件定义的路由之后注册,所以在这里你也可以拦截 BFF 函数定义的路由,进行预处理或是提前响应。
149
-
150
- :::note
151
- 此时,如果应用中同时存在 `api/_app.ts`,则定义的中间件会被放在 `api/app.ts` 导出实例的最后执行。多数情况下,中间件就能覆盖大多数 BFF 函数的定制需求。只有当应用的服务端逻辑比较复杂时,才需要自定义 Server 实例。
152
- :::
153
-
154
- :::caution
155
- 当应用中没有 `api/app.ts` 的时候,Modern.js 默认会添加 [koa-body](https://www.npmjs.com/package/koa-body)。当应用中存在 `api/app.ts` 时,如果开发者希望使用带有 Body 的 BFF 函数,需要**自行添加** `koa-body`。
156
- :::
108
+ 以上代码模拟了在 `server/Modern.server.ts` 中定义中间件的方式,实现了简易的登录功能。同样,可以在这个配置文件中实现其他功能来扩展 BFF Server。
@@ -5,82 +5,22 @@ title: 运行时框架
5
5
 
6
6
  # 运行时框架
7
7
 
8
- Modern.js BFF 支持不同的运行时框架,当前 Modern.js 的 BFF 支持两种运行时框架 [Express.js](https://expressjs.com/) 和 [Koa.js](https://koajs.com/)。
9
-
10
- 使用不同运行时框架时,有部分 API 会存在差异。
8
+ Modern.js [Hono.js](https://hono.dev/) 作为 BFF Server 运行时框架,因此可以基于 Hono.js 生态[扩展 BFF Server](/guides/advanced-features/bff/extend-server.html)。
11
9
 
12
10
  ## 获取请求上下文
13
11
 
14
- 在 BFF 函数中,有时需要获取请求上下文,来处理更多逻辑。此时,根据不同的运行时框架,你需要从通过不同的 API 来获取:
15
-
16
- import { Tabs, Tab as TabItem } from "@theme";
17
-
18
- <Tabs>
19
- <TabItem value="express" label="Express.js" default>
20
-
21
- ```ts title="api/lambda/hello.ts"
22
- import { useContext } from '@modern-js/runtime/express'
23
- export const get = async () => {
24
- const { req } = useContext();
25
- console.info(`access url: ${req.url}`);
26
- return 'Hello Modern.js'
27
- };
28
- ```
29
-
30
- </TabItem>
31
- <TabItem value="koa" label="Koa.js">
12
+ 在 BFF 函数中,有时需要获取请求上下文,来处理更多逻辑。此时,你可以通过 `useHonoContext` 来获取:
32
13
 
33
14
  ```ts title="api/lambda/hello.ts"
34
- import { useContext } from '@modern-js/runtime/koa'
15
+ import { useHonoContext } from '@modern-js/plugin-bff/hono'
35
16
  export const get = async () => {
36
- const ctx = useContext();
37
- console.info(`access url: ${req.url}`);
17
+ const c = useHonoContext();
18
+ console.info(`access url: ${c.req.url}`);
38
19
  return 'Hello Modern.js'
39
20
  };
40
21
  ```
41
22
 
42
- </TabItem>
43
- </Tabs>
44
-
45
23
  :::info
46
- 详细内容可以参考 [useContext](/apis/app/runtime/bff/use-context)。
24
+ 详细内容可以参考 [useHonoContext](/apis/app/runtime/bff/use-hono-context)。
47
25
  :::
48
26
 
49
- ## 使用中间件
50
-
51
- 在 BFF 函数中,有时需要使用中间件,来处理更多逻辑。此时,根据不同的运行时框架,你需要从通过不同的 API 来获取:
52
-
53
- <Tabs>
54
- <TabItem value="express" label="Express.js" default>
55
-
56
- ```ts title="api/_app.ts"
57
- import { hook } from '@modern-js/runtime/express';
58
-
59
- export default hook(({ addMiddleware }) => {
60
- addMiddleware((req, res, next) => {
61
- req.query.id = 'koa';
62
- next();
63
- });
64
- });
65
- ```
66
-
67
- </TabItem>
68
- <TabItem value="koa" label="Koa.js">
69
-
70
- ```ts title="api/_app.ts"
71
- import { hook } from '@modern-js/runtime/koa';
72
-
73
- export default hook(({ addMiddleware }) => {
74
- addMiddleware(async (ctx, next) => {
75
- ctx.req.query.id = 'koa';
76
- await next();
77
- });
78
- });
79
- ```
80
-
81
- </TabItem>
82
- </Tabs>
83
-
84
- :::info
85
- 详细内容可以参考 [hook](/apis/app/runtime/bff/hook)。
86
- :::
@@ -1 +1 @@
1
- ["code-split", "inline-assets", "optimize-bundle"]
1
+ ["code-split", "inline-assets", "optimize-bundle", "react-compiler"]
@@ -0,0 +1,44 @@
1
+ # React Compiler
2
+
3
+ React Compiler 是 React 19 引入的一个实验性编译器,它可以自动优化你的 React 代码。
4
+
5
+ 在开始使用 React Compiler 之前,建议阅读 [React Compiler 文档](https://zh-hans.react.dev/learn/react-compiler),以了解 React Compiler 的功能、当前状态和使用方法。
6
+
7
+ ## 如何使用
8
+
9
+ 在 Modern.js 中使用 React Compiler 的步骤如下:
10
+
11
+ 1. Modern.js 目前项目还未正式支持 React 19,可以在 React 17 或 18 项目中安装 `react-compiler-runtime@rc`,以允许编译后的代码在 19 之前的版本上运行。
12
+
13
+ 2. 目前 React Compiler 仅提供了 Babel 插件,你需要安装 `babel-plugin-react-compiler`。
14
+
15
+ 3. 在你的 Modern.js 配置文件中注册 Babel 插件:
16
+
17
+ ```ts title="modern.config.ts"
18
+ import { appTools, defineConfig } from '@modern-js/app-tools';
19
+
20
+ export default defineConfig({
21
+ runtime: {
22
+ router: true,
23
+ },
24
+ tools: {
25
+ babel(_, { addPlugins }) {
26
+ addPlugins([
27
+ [
28
+ 'babel-plugin-react-compiler',
29
+ {
30
+ target: '18',
31
+ },
32
+ ],
33
+ ]);
34
+ },
35
+ },
36
+ plugins: [
37
+ appTools({
38
+ bundler: 'rspack',
39
+ }),
40
+ ],
41
+ });
42
+ ```
43
+
44
+ > 详细代码可以参考:[Modern.js & React Compiler 示例项目](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/react-compiler)