Skip to content

Commit 56f5be3

Browse files
authored
docs: add react compiler usage docs (#7176)
1 parent b96d480 commit 56f5be3

File tree

6 files changed

+90
-59
lines changed

6 files changed

+90
-59
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["code-split", "inline-assets", "optimize-bundle"]
1+
["code-split", "inline-assets", "optimize-bundle", "react-compiler"]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# React Compiler
2+
3+
The React Compiler is an experimental compiler introduced in React 19 that can automatically optimize your React code.
4+
5+
Before starting to use the React Compiler, it is recommended to read the [React Compiler documentation](https://zh-hans.react.dev/learn/react-compiler) to understand its features, current status, and usage.
6+
7+
## How to Use
8+
9+
The steps to use the React Compiler in Modern.js are as follows:
10+
11+
1. Modern.js does not officially support React 19 yet, but you can install `react-compiler-runtime@rc` in React 17 or 18 projects to allow the compiled code to run on versions before 19.
12+
13+
2. Currently, the React Compiler only provides a Babel plugin, so you need to install `babel-plugin-react-compiler`.
14+
15+
3. Register the Babel plugin in your Modern.js configuration file:
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+
> For detailed code, you can refer to the [Modern.js & React Compiler example project](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/react-compiler)

packages/document/main-doc/docs/en/guides/basic-features/output-files.mdx

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,6 @@ dist
9696
└── qux.[hash].mp4
9797
```
9898

99-
100-
<!-- ## Node.js Output Directory
101-
102-
When you enable SSR or SSG features in Modern.js, Modern.js will generate a Node.js bundle after the build and output it to the `bundles` directory.
103-
104-
```bash
105-
dist
106-
├── bundles
107-
└── [name].js
108-
├── static
109-
└── html
110-
```
111-
112-
Node.js files usually only contain JS files, no HTML or CSS. Also, JS file names will not contain hash.
113-
114-
You can modify the output path of Node.js files through the [output.distPath.server](/configure/app/output/dist-path) config.
115-
116-
For example, output Node.js files to the `server` directory:
117-
118-
```ts
119-
export default {
120-
output: {
121-
distPath: {
122-
server: 'server',
123-
},
124-
},
125-
};
126-
``` -->
127-
12899
## Flatten the Directory
129100

130101
Sometimes you don't want the dist directory to have too many levels, you can set the directory to an empty string to flatten the generated directory.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["code-split", "inline-assets", "optimize-bundle"]
1+
["code-split", "inline-assets", "optimize-bundle", "react-compiler"]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

packages/document/main-doc/docs/zh/guides/basic-features/output-files.mdx

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,6 @@ dist
9696
└── qux.[hash].mp4
9797
```
9898

99-
<!-- ## Node.js 产物目录
100-
101-
当你在 Modern.js 中开启了 SSR SSG 等服务端功能时,Modern.js 会在构建后生成一份 Node.js 产物,并输出到 `bundles` 目录下:
102-
103-
```bash
104-
dist
105-
├── bundles
106-
└── [name].js
107-
├── static
108-
└── html
109-
```
110-
111-
Node.js 产物通常只包含 JS 文件,不包含 HTML、CSS 等文件。此外,Node 产物的 JS 文件名称也不会自动生成哈希值。
112-
113-
你可以通过 [output.distPath.server](/configure/app/output/dist-path) 配置项来修改 Node 产物的输出路径。
114-
115-
比如,将 Node.js 产物输出到 `server` 目录:
116-
117-
```ts
118-
export default {
119-
output: {
120-
distPath: {
121-
server: 'server',
122-
},
123-
},
124-
};
125-
``` -->
126-
12799
## 扁平化产物目录
128100

129101
有时候你不想产物目录有太多层级,可以将目录设置为空字符串,使生成的产物目录扁平化。

0 commit comments

Comments
 (0)