@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.
- package/docs/en/apis/app/hooks/server/server.mdx +10 -0
- package/docs/en/apis/app/hooks/src/routes.mdx +3 -3
- package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +30 -0
- package/docs/en/components/enable-bff.mdx +1 -27
- package/docs/en/components/rsbuild-config-tooltip.mdx +2 -2
- package/docs/en/components/tech-stack-node-framework.mdx +1 -1
- package/docs/en/configure/app/dev/asset-prefix.mdx +2 -3
- package/docs/en/configure/app/dev/client.mdx +2 -3
- package/docs/en/configure/app/dev/hmr.mdx +2 -3
- package/docs/en/configure/app/dev/live-reload.mdx +2 -3
- package/docs/en/configure/app/dev/progress-bar.mdx +2 -3
- package/docs/en/configure/app/dev/setup-middlewares.mdx +2 -3
- package/docs/en/configure/app/dev/watch-files.mdx +2 -3
- package/docs/en/configure/app/dev/write-to-disk.mdx +2 -3
- package/docs/en/configure/app/html/app-icon.mdx +2 -3
- package/docs/en/configure/app/html/crossorigin.mdx +2 -3
- package/docs/en/configure/app/html/favicon.mdx +2 -3
- package/docs/en/configure/app/html/inject.mdx +2 -3
- package/docs/en/configure/app/html/meta.mdx +2 -3
- package/docs/en/configure/app/html/mount-id.mdx +2 -3
- package/docs/en/configure/app/html/output-structure.mdx +2 -3
- package/docs/en/configure/app/html/script-loading.mdx +2 -3
- package/docs/en/configure/app/html/tags.mdx +2 -3
- package/docs/en/configure/app/html/template-parameters.mdx +2 -3
- package/docs/en/configure/app/html/template.mdx +2 -3
- package/docs/en/configure/app/html/title.mdx +2 -3
- package/docs/en/configure/app/output/asset-prefix.mdx +2 -3
- package/docs/en/configure/app/output/charset.mdx +2 -3
- package/docs/en/configure/app/output/copy.mdx +2 -3
- package/docs/en/configure/app/output/css-modules.mdx +2 -3
- package/docs/en/configure/app/output/data-uri-limit.mdx +2 -3
- package/docs/en/configure/app/output/dist-path.mdx +2 -4
- package/docs/en/configure/app/output/externals.mdx +2 -3
- package/docs/en/configure/app/output/filename-hash.mdx +2 -3
- package/docs/en/configure/app/output/filename.mdx +2 -3
- package/docs/en/configure/app/output/inject-styles.mdx +2 -3
- package/docs/en/configure/app/output/inline-scripts.mdx +2 -3
- package/docs/en/configure/app/output/inline-styles.mdx +2 -3
- package/docs/en/configure/app/output/legal-comments.mdx +2 -3
- package/docs/en/configure/app/output/minify.mdx +2 -3
- package/docs/en/configure/app/output/override-browserslist.mdx +2 -3
- package/docs/en/configure/app/output/polyfill.mdx +2 -3
- package/docs/en/configure/app/output/source-map.mdx +2 -3
- package/docs/en/configure/app/performance/build-cache.mdx +2 -3
- package/docs/en/configure/app/performance/bundle-analyze.mdx +2 -3
- package/docs/en/configure/app/performance/chunk-split.mdx +2 -3
- package/docs/en/configure/app/performance/dns-prefetch.mdx +2 -3
- package/docs/en/configure/app/performance/preconnect.mdx +2 -3
- package/docs/en/configure/app/performance/prefetch.mdx +2 -3
- package/docs/en/configure/app/performance/preload.mdx +2 -3
- package/docs/en/configure/app/performance/print-file-size.mdx +2 -3
- package/docs/en/configure/app/performance/profile.mdx +2 -3
- package/docs/en/configure/app/performance/remove-console.mdx +2 -3
- package/docs/en/configure/app/performance/remove-moment-locale.mdx +2 -3
- package/docs/en/configure/app/plugins.mdx +13 -33
- package/docs/en/configure/app/runtime/master-app.mdx +1 -5
- package/docs/en/configure/app/runtime/plugins.mdx +58 -0
- package/docs/en/configure/app/security/nonce.mdx +2 -3
- package/docs/en/configure/app/security/sri.mdx +0 -1
- package/docs/en/configure/app/server/port.mdx +2 -3
- package/docs/en/configure/app/source/alias-strategy.mdx +2 -3
- package/docs/en/configure/app/source/alias.mdx +2 -3
- package/docs/en/configure/app/source/decorators.mdx +2 -3
- package/docs/en/configure/app/source/define.mdx +2 -3
- package/docs/en/configure/app/source/exclude.mdx +2 -3
- package/docs/en/configure/app/source/include.mdx +2 -3
- package/docs/en/configure/app/source/pre-entry.mdx +2 -3
- package/docs/en/configure/app/source/transform-import.mdx +2 -3
- package/docs/en/configure/app/tools/css-extract.mdx +2 -3
- package/docs/en/configure/app/tools/css-loader.mdx +2 -2
- package/docs/en/configure/app/tools/html-plugin.mdx +7 -3
- package/docs/en/configure/app/tools/lightningcss-loader.mdx +2 -3
- package/docs/en/configure/app/tools/postcss.mdx +2 -3
- package/docs/en/configure/app/tools/rspack.mdx +2 -3
- package/docs/en/configure/app/tools/style-loader.mdx +2 -3
- package/docs/en/configure/app/tools/swc.mdx +1 -1
- package/docs/en/configure/app/usage.mdx +1 -1
- package/docs/en/guides/advanced-features/bff/extend-server.mdx +33 -82
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +12 -68
- package/docs/en/guides/advanced-features/bff.mdx +1 -1
- package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/_meta.json +1 -1
- package/docs/en/guides/advanced-features/page-performance/inline-assets.mdx +2 -0
- package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/en/guides/advanced-features/web-server.mdx +378 -14
- package/docs/en/guides/basic-features/data/data-fetch.mdx +2 -1
- package/docs/en/guides/basic-features/deploy.mdx +3 -3
- package/docs/en/guides/basic-features/html.mdx +3 -3
- package/docs/en/guides/basic-features/output-files.mdx +0 -28
- package/docs/en/guides/basic-features/render/ssr.mdx +2 -2
- package/docs/en/guides/concept/entries.mdx +1 -1
- package/docs/en/plugin/cli-plugins/api.mdx +6 -0
- package/docs/en/plugin/runtime-plugins/api.mdx +37 -12
- package/docs/en/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/en/tutorials/first-app/c05-loader.mdx +5 -2
- package/docs/zh/apis/app/hooks/server/server.mdx +10 -0
- package/docs/zh/apis/app/hooks/src/routes.mdx +3 -3
- package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +31 -0
- package/docs/zh/components/enable-bff.mdx +2 -27
- package/docs/zh/components/rsbuild-config-tooltip.mdx +2 -2
- package/docs/zh/components/tech-stack-node-framework.mdx +1 -1
- package/docs/zh/configure/app/dev/asset-prefix.mdx +2 -3
- package/docs/zh/configure/app/dev/client.mdx +2 -3
- package/docs/zh/configure/app/dev/hmr.mdx +2 -3
- package/docs/zh/configure/app/dev/live-reload.mdx +2 -3
- package/docs/zh/configure/app/dev/progress-bar.mdx +2 -3
- package/docs/zh/configure/app/dev/setup-middlewares.mdx +2 -3
- package/docs/zh/configure/app/dev/watch-files.mdx +2 -3
- package/docs/zh/configure/app/dev/write-to-disk.mdx +2 -3
- package/docs/zh/configure/app/html/app-icon.mdx +2 -3
- package/docs/zh/configure/app/html/crossorigin.mdx +2 -3
- package/docs/zh/configure/app/html/favicon.mdx +2 -3
- package/docs/zh/configure/app/html/inject.mdx +2 -3
- package/docs/zh/configure/app/html/meta.mdx +2 -3
- package/docs/zh/configure/app/html/mount-id.mdx +2 -3
- package/docs/zh/configure/app/html/output-structure.mdx +2 -3
- package/docs/zh/configure/app/html/script-loading.mdx +2 -3
- package/docs/zh/configure/app/html/tags.mdx +2 -3
- package/docs/zh/configure/app/html/template-parameters.mdx +2 -3
- package/docs/zh/configure/app/html/template.mdx +2 -3
- package/docs/zh/configure/app/html/title.mdx +2 -3
- package/docs/zh/configure/app/output/asset-prefix.mdx +2 -3
- package/docs/zh/configure/app/output/charset.mdx +2 -3
- package/docs/zh/configure/app/output/copy.mdx +2 -3
- package/docs/zh/configure/app/output/css-modules.mdx +2 -3
- package/docs/zh/configure/app/output/data-uri-limit.mdx +2 -3
- package/docs/zh/configure/app/output/dist-path.mdx +2 -4
- package/docs/zh/configure/app/output/externals.mdx +2 -3
- package/docs/zh/configure/app/output/filename-hash.mdx +2 -3
- package/docs/zh/configure/app/output/filename.mdx +2 -3
- package/docs/zh/configure/app/output/inject-styles.mdx +2 -3
- package/docs/zh/configure/app/output/inline-scripts.mdx +2 -3
- package/docs/zh/configure/app/output/inline-styles.mdx +2 -3
- package/docs/zh/configure/app/output/legal-comments.mdx +2 -3
- package/docs/zh/configure/app/output/minify.mdx +2 -3
- package/docs/zh/configure/app/output/override-browserslist.mdx +2 -3
- package/docs/zh/configure/app/output/polyfill.mdx +2 -3
- package/docs/zh/configure/app/output/source-map.mdx +2 -3
- package/docs/zh/configure/app/performance/build-cache.mdx +2 -3
- package/docs/zh/configure/app/performance/bundle-analyze.mdx +2 -3
- package/docs/zh/configure/app/performance/chunk-split.mdx +2 -3
- package/docs/zh/configure/app/performance/dns-prefetch.mdx +2 -3
- package/docs/zh/configure/app/performance/preconnect.mdx +2 -3
- package/docs/zh/configure/app/performance/prefetch.mdx +2 -3
- package/docs/zh/configure/app/performance/preload.mdx +2 -3
- package/docs/zh/configure/app/performance/print-file-size.mdx +2 -3
- package/docs/zh/configure/app/performance/profile.mdx +2 -3
- package/docs/zh/configure/app/performance/remove-console.mdx +2 -3
- package/docs/zh/configure/app/performance/remove-moment-locale.mdx +2 -3
- package/docs/zh/configure/app/plugins.mdx +3 -24
- package/docs/zh/configure/app/runtime/master-app.mdx +1 -5
- package/docs/zh/configure/app/runtime/plugins.mdx +58 -0
- package/docs/zh/configure/app/security/nonce.mdx +2 -3
- package/docs/zh/configure/app/security/sri.mdx +0 -1
- package/docs/zh/configure/app/server/port.mdx +2 -3
- package/docs/zh/configure/app/source/alias-strategy.mdx +2 -3
- package/docs/zh/configure/app/source/alias.mdx +2 -3
- package/docs/zh/configure/app/source/decorators.mdx +2 -3
- package/docs/zh/configure/app/source/define.mdx +2 -3
- package/docs/zh/configure/app/source/exclude.mdx +2 -3
- package/docs/zh/configure/app/source/include.mdx +2 -3
- package/docs/zh/configure/app/source/pre-entry.mdx +2 -3
- package/docs/zh/configure/app/source/transform-import.mdx +2 -3
- package/docs/zh/configure/app/tools/css-extract.mdx +2 -3
- package/docs/zh/configure/app/tools/css-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/html-plugin.mdx +6 -3
- package/docs/zh/configure/app/tools/lightningcss-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/postcss.mdx +2 -3
- package/docs/zh/configure/app/tools/rspack.mdx +2 -3
- package/docs/zh/configure/app/tools/style-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/configure/app/usage.mdx +1 -1
- package/docs/zh/guides/advanced-features/bff/extend-server.mdx +28 -76
- package/docs/zh/guides/advanced-features/bff/frameworks.mdx +6 -66
- package/docs/zh/guides/advanced-features/page-performance/_meta.json +1 -1
- package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/zh/guides/advanced-features/web-server.mdx +375 -18
- package/docs/zh/guides/basic-features/deploy.mdx +4 -3
- package/docs/zh/guides/basic-features/output-files.mdx +0 -28
- package/docs/zh/plugin/cli-plugins/api.mdx +6 -0
- package/docs/zh/plugin/runtime-plugins/api.mdx +37 -12
- package/docs/zh/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/zh/tutorials/first-app/c05-loader.mdx +4 -1
- package/package.json +7 -4
- package/rspress.config.ts +16 -1
- package/src/components/RsbuildLink/index.tsx +2 -2
- package/src/i18n/index.ts +1 -1
- package/src/index.ts +1 -5
- package/src/pages/index.tsx +3 -3
- package/docs/en/apis/app/hooks/api/middleware.mdx +0 -11
- package/docs/en/apis/app/runtime/bff/hook.mdx +0 -44
- package/docs/en/apis/app/runtime/bff/use-context.mdx +0 -38
- package/docs/en/configure/app/bff/enable-handle-web.mdx +0 -24
- package/docs/zh/apis/app/hooks/api/middleware.mdx +0 -11
- package/docs/zh/apis/app/runtime/bff/hook.mdx +0 -44
- package/docs/zh/apis/app/runtime/bff/use-context.mdx +0 -38
- 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
|
12
|
+
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
14
13
|
|
15
|
-
<
|
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
|
12
|
+
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
14
13
|
|
15
|
-
<
|
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
|
-
|
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
|
5
|
+
Modern.js 支持用户通过 [Middleware](/guides/advanced-features/web-server.html#middleware) 的方式来自由扩展 BFF Server。
|
6
6
|
|
7
|
-
## 中间件
|
8
7
|
|
9
|
-
|
8
|
+
## 使用中间件
|
10
9
|
|
11
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|

|
99
107
|
|
100
|
-
以上代码模拟了在
|
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
|
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
|
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 {
|
15
|
+
import { useHonoContext } from '@modern-js/plugin-bff/hono'
|
35
16
|
export const get = async () => {
|
36
|
-
const
|
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
|
-
详细内容可以参考 [
|
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)
|