Skip to content

Commit 4cb72bb

Browse files
committed
docs(en): merging all conflicts
2 parents 2591dc6 + 9467bc5 commit 4cb72bb

File tree

1 file changed

+103
-4
lines changed

1 file changed

+103
-4
lines changed

src/content/learn/react-compiler.md

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ title: React Compiler
33
---
44

55
<Intro>
6+
<<<<<<< HEAD
67
本页面将为你介绍新的实验性 React Compiler,以及如何成功试用。
8+
=======
9+
This page will give you an introduction to React Compiler and how to try it out successfully.
10+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
711
</Intro>
812
913
<Wip>
@@ -19,12 +23,37 @@ title: React Compiler
1923
</YouWillLearn>
2024

2125
<Note>
26+
<<<<<<< HEAD
2227
React Compiler 是一个新的实验性编译器,我们已经将其开源,以便从社区中获得早期反馈。它仍然存在一些问题,所以还没有完全准备好投入生产。
2328
</Note>
2429

2530
React Compiler 是一个新的实验性编译器,我们已经将其开源,以便从社区中获得早期反馈。它是一个仅在构建时使用的工具,可以自动优化你的 React 应用程序。它可以与纯 JavaScript 一起使用,并且了解 [React 规则](/reference/rules),因此你无需重写任何代码即可使用它。
2631

2732
编译器还包括一个 [ESLint 插件](#installing-eslint-plugin-react-compiler),可以在你的编辑器中直接显示编译器的分析结果。该插件独立运行,即使你的应用程序中没有使用编译器也可以使用。我们建议所有 React 开发人员使用这个 ESLint 插件来帮助提高代码库的质量。
33+
=======
34+
React Compiler is a new compiler currently in Beta, that we've open sourced to get early feedback from the community. While it has been used in production at companies like Meta, rolling out the compiler to production for your app will depend on the health of your codebase and how well you’ve followed the [Rules of React](/reference/rules).
35+
36+
The latest Beta release can be found with the `@beta` tag, and daily experimental releases with `@experimental`.
37+
</Note>
38+
39+
React Compiler is a new compiler that we've open sourced to get early feedback from the community. It is a build-time only tool that automatically optimizes your React app. It works with plain JavaScript, and understands the [Rules of React](/reference/rules), so you don't need to rewrite any code to use it.
40+
41+
The compiler also includes an [eslint plugin](#installing-eslint-plugin-react-compiler) that surfaces the analysis from the compiler right in your editor. **We strongly recommend everyone use the linter today.** The linter does not require that you have the compiler installed, so you can use it even if you are not ready to try out the compiler.
42+
43+
The compiler is currently released as `beta`, and is available to try out on React 17+ apps and libraries. To install the Beta:
44+
45+
<TerminalBlock>
46+
npm install -D babel-plugin-react-compiler@beta eslint-plugin-react-compiler@beta
47+
</TerminalBlock>
48+
49+
Or, if you're using Yarn:
50+
51+
<TerminalBlock>
52+
yarn add -D babel-plugin-react-compiler@beta eslint-plugin-react-compiler@beta
53+
</TerminalBlock>
54+
55+
If you are not using React 19 yet, please see [the section below](#using-react-compiler-with-react-17-or-18) for further instructions.
56+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
2857
2958
### 编译器是做什么的? {/*what-does-the-compiler-do*/}
3059

@@ -94,6 +123,7 @@ function TableContainer({ items }) {
94123
因此,如果在许多不同的组件中使用 `expensivelyProcessAReallyLargeArrayOfObjects`,即使传递相同的 `items`,那昂贵的计算也会被重复运行。我们建议先进行 [性能分析](/reference/react/useMemo#how-to-tell-if-a-calculation-is-expensive),看看是否真的那么昂贵,然后再使代码更加复杂。
95124
</DeepDive>
96125

126+
<<<<<<< HEAD
97127
### 编译器假设什么? {/*what-does-the-compiler-assume*/}
98128

99129
React Compiler 假设你的代码:
@@ -107,6 +137,11 @@ React Compiler 可以静态验证 React 的许多规则,并且在检测到错
107137
### 我应该尝试一下编译器吗? {/*should-i-try-out-the-compiler*/}
108138

109139
请注意,编译器仍处于实验阶段,存在许多不完善之处。虽然它已经在 Meta 等公司的生产环境中使用过,但将编译器应用于你的应用程序生产环境将取决于你的代码库的健康状况以及你是否遵循了 [React的规则](/reference/rules)
140+
=======
141+
### Should I try out the compiler? {/*should-i-try-out-the-compiler*/}
142+
143+
Please note that the compiler is still in Beta and has many rough edges. While it has been used in production at companies like Meta, rolling out the compiler to production for your app will depend on the health of your codebase and how well you've followed the [Rules of React](/reference/rules).
144+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
110145
111146
**你现在不必急着使用编译器。在采用它之前等到它达到稳定版本是可以的。** 然而,我们确实赞赏在你的应用程序中进行小型实验,以便你可以向我们 [提供反馈](#reporting-issues),帮助使编译器更好。
112147

@@ -119,7 +154,7 @@ React Compiler 可以静态验证 React 的许多规则,并且在检测到错
119154
在安装编译器之前,你可以先检查你的代码库是否兼容:
120155

121156
<TerminalBlock>
122-
npx react-compiler-healthcheck@experimental
157+
npx react-compiler-healthcheck@beta
123158
</TerminalBlock>
124159

125160
此脚本将:
@@ -141,7 +176,7 @@ Found no usage of incompatible libraries.
141176
React Compiler 还为 ESLint 插件提供支持。ESLint 插件可以**独立**于编译器使用,这意味着即使你不使用编译器,也可以使用 ESLint 插件。
142177

143178
<TerminalBlock>
144-
npm install eslint-plugin-react-compiler@experimental
179+
npm install -D eslint-plugin-react-compiler@beta
145180
</TerminalBlock>
146181

147182
然后,将其添加到你的 ESLint 配置中:
@@ -176,6 +211,7 @@ const ReactCompilerConfig = {
176211
};
177212
```
178213

214+
<<<<<<< HEAD
179215
在罕见的情况下,你还可以使用 `compilationMode: "annotation"` 选项将编译器配置为以 "opt-in" 模式运行。这样编译器将只编译带有 `"use memo"` 指令的组件和钩子。请注意,`annotation` 模式是为了帮助早期采用者而设立的临时模式,我们并不打算长期使用 `"use memo"` 指令。
180216

181217
```js {2,7}
@@ -191,17 +227,59 @@ export default function App() {
191227
```
192228

193229
当你对编译器的推出更有信心时,你也可以将覆盖范围扩展到其他目录,并逐渐将其推出到整个应用程序。
230+
=======
231+
When you have more confidence with rolling out the compiler, you can expand coverage to other directories as well and slowly roll it out to your whole app.
232+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
194233
195234
#### 新项目 {/*new-projects*/}
196235

197236
如果你正在启动一个新项目,你可以在整个代码库上启用编译器,这是默认行为。
198237

238+
<<<<<<< HEAD
199239
## 用法 {/*installation*/}
240+
=======
241+
### Using React Compiler with React 17 or 18 {/*using-react-compiler-with-react-17-or-18*/}
242+
243+
React Compiler works best with React 19 RC. If you are unable to upgrade, you can install the extra `react-compiler-runtime` package which will allow the compiled code to run on versions prior to 19. However, note that the minimum supported version is 17.
244+
245+
<TerminalBlock>
246+
npm install react-compiler-runtime@beta
247+
</TerminalBlock>
248+
249+
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
250+
251+
```js {3}
252+
// babel.config.js
253+
const ReactCompilerConfig = {
254+
target: '18' // '17' | '18' | '19'
255+
};
256+
257+
module.exports = function () {
258+
return {
259+
plugins: [
260+
['babel-plugin-react-compiler', ReactCompilerConfig],
261+
],
262+
};
263+
};
264+
```
265+
266+
### Using the compiler on libraries {/*using-the-compiler-on-libraries*/}
267+
268+
React Compiler can also be used to compile libraries. Because React Compiler needs to run on the original source code prior to any code transformations, it is not possible for an application's build pipeline to compile the libraries they use. Hence, our recommendation is for library maintainers to independently compile and test their libraries with the compiler, and ship compiled code to npm.
269+
270+
Because your code is pre-compiled, users of your library will not need to have the compiler enabled in order to benefit from the automatic memoization applied to your library. If your library targets apps not yet on React 19, specify a minimum [`target` and add `react-compiler-runtime` as a direct dependency](#using-react-compiler-with-react-17-or-18). The runtime package will use the correct implementation of APIs depending on the application's version, and polyfill the missing APIs if necessary.
271+
272+
Library code can often require more complex patterns and usage of escape hatches. For this reason, we recommend ensuring that you have sufficient testing in order to identify any issues that might arise from using the compiler on your library. If you identify any issues, you can always opt-out the specific components or hooks with the [`'use no memo'` directive](#something-is-not-working-after-compilation).
273+
274+
Similarly to apps, it is not necessary to fully compile 100% of your components or hooks to see benefits in your library. A good starting point might be to identify the most performance sensitive parts of your library and ensuring that they don't break the [Rules of React](/reference/rules), which you can use `eslint-plugin-react-compiler` to identify.
275+
276+
## Usage {/*installation*/}
277+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
200278
201279
### Babel {/*usage-with-babel*/}
202280

203281
<TerminalBlock>
204-
npm install babel-plugin-react-compiler@experimental
282+
npm install babel-plugin-react-compiler@beta
205283
</TerminalBlock>
206284

207285
编译器包含一个 Babel 插件,你可以在构建流水线中使用它来运行编译器。
@@ -224,6 +302,7 @@ module.exports = function () {
224302

225303
`babel-plugin-react-compiler` 应该在其他 Babel 插件之前运行,因为编译器需要输入源信息进行声音分析。
226304

305+
<<<<<<< HEAD
227306
React Compiler 与 React 19 RC 配合使用效果最佳。如果你无法升级,可以安装额外的 `react-compiler-runtime` 包来编译代码并在 19 之前的版本上运行。 但请注意,支持的最低版本是 17。
228307

229308
<TerminalBlock>
@@ -247,6 +326,8 @@ module.exports = function () {
247326
};
248327
```
249328

329+
=======
330+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
250331
### Vite {/*usage-with-vite*/}
251332

252333
如果你使用 Vite,你可以将插件添加到 vite-plugin-react 中:
@@ -273,6 +354,7 @@ export default defineConfig(() => {
273354

274355
### Next.js {/*usage-with-nextjs*/}
275356

357+
<<<<<<< HEAD
276358
Next.js 有一个实验性配置来启用 React 编译器。它会自动确保 Babel 已经配置了 `babel-plugin-react-compiler`
277359

278360
- 安装使用 React 19 RC 版本的 Next.js canary
@@ -303,6 +385,9 @@ module.exports = nextConfig;
303385
- Webpack (default)
304386
- Turbopack (通过 `--turbo` 接入)
305387

388+
=======
389+
Please refer to the [Next.js docs](https://nextjs.org/docs/canary/app/api-reference/next-config-js/reactCompiler) for more information.
390+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
306391
307392
### Remix {/*usage-with-remix*/}
308393
安装 `vite-plugin-babel`, 并将编译器的 Babel 插件添加到其中:
@@ -335,6 +420,7 @@ export default defineConfig({
335420

336421
### Webpack {/*usage-with-webpack*/}
337422

423+
<<<<<<< HEAD
338424
你可以为 React Compiler 创建自己的 loader,就像这样:
339425

340426
```js
@@ -369,6 +455,9 @@ function reactCompilerLoader(sourceCode, sourceMap) {
369455

370456
module.exports = reactCompilerLoader;
371457
```
458+
=======
459+
A community Webpack loader is [now available here](https://github.com/SukkaW/react-compiler-webpack).
460+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
372461
373462
### Expo {/*usage-with-expo*/}
374463

@@ -392,11 +481,21 @@ React Native 通过 Metro 使用 Babel,因此请参考 [使用 Babel](#usage-w
392481

393482
你也可以通过申请成为成员,在 React Compiler 工作组中提供反馈意见。请查看 [README](https://github.com/reactwg/react-compiler) 以获取更多加入详情。
394483

395-
### `(0 , _c) is not a function` error {/*0--_c-is-not-a-function-error*/}
484+
### What does the compiler assume? {/*what-does-the-compiler-assume*/}
396485

486+
<<<<<<< HEAD
397487
如果你没有使用 React 19 RC 及更高版本,则会发生这种情况。要解决此问题,请先 [将你的项目升级到 React 19 RC](https://react.dev/blog/2024/04/25/react-19-upgrade-guide)
398488

399489
如果你无法升级到 React 19,你可以尝试根据 [工作组](https://github.com/reactwg/react-compiler/discussions/6) 描述的缓存功能的用户空间实现。但是,请注意这并不建议,你应尽快升级到React 19。
490+
=======
491+
React Compiler assumes that your code:
492+
493+
1. Is valid, semantic JavaScript.
494+
2. Tests that nullable/optional values and properties are defined before accessing them (for example, by enabling [`strictNullChecks`](https://www.typescriptlang.org/tsconfig/#strictNullChecks) if using TypeScript), i.e., `if (object.nullableProperty) { object.nullableProperty.foo }` or with optional-chaining `object.nullableProperty?.foo`.
495+
3. Follows the [Rules of React](https://react.dev/reference/rules).
496+
497+
React Compiler can verify many of the Rules of React statically, and will safely skip compilation when it detects an error. To see the errors we recommend also installing [eslint-plugin-react-compiler](https://www.npmjs.com/package/eslint-plugin-react-compiler).
498+
>>>>>>> 9467bc58868e66c53ca9385c8531dcf7b02178c2
400499
401500
### 我如何知道我的组件已被优化? {/*how-do-i-know-my-components-have-been-optimized*/}
402501

0 commit comments

Comments
 (0)