|
1 | 1 | # React Compiler |
2 | 2 |
|
3 | | -The React Compiler is an experimental compiler introduced in React 19 that can automatically optimize your React code. |
| 3 | +React Compiler is a build-time compiler from the React team that reduces unnecessary re-renders through automatic memoization, without manually writing `useMemo`, `useCallback`, or `React.memo`. |
4 | 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. |
| 5 | +Before starting to use React Compiler, it is recommended to read the [React Compiler documentation](https://react.dev/learn/react-compiler) to understand its features, current status, and usage. |
6 | 6 |
|
7 | 7 | ## How to Use |
8 | 8 |
|
9 | | -### React 19 |
| 9 | +Modern.js provides built-in support for React Compiler via the [source.reactCompiler](/configure/app/source/react-compiler) option, powered by the Rust-based React Compiler implementation in Rspack's `builtin:swc-loader`. It reuses Rspack's built-in SWC transform chain without introducing Babel (`babel-plugin-react-compiler`). |
10 | 10 |
|
11 | | -If you are using React 19, Modern.js has built-in support for React Compiler, and no additional configuration is required. |
| 11 | +This capability is **disabled by default** and must be enabled explicitly for any React version. |
12 | 12 |
|
13 | | -### React 18 |
| 13 | +### React 19 |
14 | 14 |
|
15 | | -If you are using React 18, you need to configure it as follows: |
| 15 | +If you are using React 19, simply enable `source.reactCompiler` — no additional dependencies are required: |
16 | 16 |
|
17 | | -1. Install `react-compiler-runtime` to allow the compiled code to run on versions before 19: |
| 17 | +```ts title="modern.config.ts" |
| 18 | +import { appTools, defineConfig } from '@modern-js/app-tools'; |
18 | 19 |
|
19 | | -```bash |
20 | | -npm install react-compiler-runtime |
| 20 | +export default defineConfig({ |
| 21 | + source: { |
| 22 | + reactCompiler: true, |
| 23 | + }, |
| 24 | + plugins: [appTools()], |
| 25 | +}); |
21 | 26 | ``` |
22 | 27 |
|
23 | | -2. Install `babel-plugin-react-compiler`: |
| 28 | +### React 18 |
| 29 | + |
| 30 | +If you are using React 18, configure it as follows: |
| 31 | + |
| 32 | +1. Install `react-compiler-runtime` as a **runtime dependency** to allow the compiled code to run on versions before React 19: |
24 | 33 |
|
25 | 34 | ```bash |
26 | | -npm install babel-plugin-react-compiler |
| 35 | +npm add react-compiler-runtime |
27 | 36 | ``` |
28 | 37 |
|
29 | | -3. Register the Babel plugin in your Modern.js configuration file: |
| 38 | +2. Specify the React version via `target` in the configuration: |
30 | 39 |
|
31 | 40 | ```ts title="modern.config.ts" |
32 | 41 | import { appTools, defineConfig } from '@modern-js/app-tools'; |
33 | | -import { pluginBabel } from '@rsbuild/plugin-babel'; |
34 | 42 |
|
35 | 43 | export default defineConfig({ |
36 | | - builderPlugins: [ |
37 | | - pluginBabel({ |
38 | | - babelLoaderOptions: (config, { addPlugins }) => { |
39 | | - addPlugins([ |
40 | | - [ |
41 | | - 'babel-plugin-react-compiler', |
42 | | - { |
43 | | - target: '18', // 或 '17',根据你使用的 React 版本 |
44 | | - }, |
45 | | - ], |
46 | | - ]); |
47 | | - }, |
48 | | - }); |
49 | | - ]; |
| 44 | + source: { |
| 45 | + reactCompiler: { |
| 46 | + target: '18', |
| 47 | + }, |
| 48 | + }, |
50 | 49 | plugins: [appTools()], |
51 | 50 | }); |
52 | 51 | ``` |
53 | 52 |
|
| 53 | +## Customize Compilation Behavior |
| 54 | + |
| 55 | +`source.reactCompiler` accepts an object to configure the compilation behavior, with the same options as Rspack's `jsc.transform.reactCompiler`. For example, use `compilationMode: 'annotation'` to only compile functions annotated with the `"use memo"` directive, enabling React Compiler incrementally: |
| 56 | + |
| 57 | +```ts title="modern.config.ts" |
| 58 | +export default defineConfig({ |
| 59 | + source: { |
| 60 | + reactCompiler: { |
| 61 | + compilationMode: 'annotation', |
| 62 | + }, |
| 63 | + }, |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +For the complete list of options, see [Rsbuild - reactCompiler](https://rsbuild.rs/plugins/list/plugin-react#reactcompiler) and the [React Compiler configuration docs](https://react.dev/reference/react-compiler/configuration). |
| 68 | + |
54 | 69 | > For detailed code, you can refer to the [Modern.js & React Compiler example project](https://github.com/web-infra-dev/modern.js/tree/main/examples/react-compiler) |
0 commit comments