target
选项指定编译器应为哪个 React 版本生成代码。
¥The target
option specifies which React version the compiler should generate code for.
{
target: '19' // or '18', '17'
}
参考
¥Reference
target
配置编译输出的 React 版本兼容性。
¥Configures the React version compatibility for the compiled output.
类型
¥Type
'17' | '18' | '19'
默认值
¥Default value
'19'
有效值
¥Valid values
-
'19'
:目标版本:React 19(默认)。无需额外的运行时。¥**
'19'
**: Target React 19 (default). No additional runtime required. -
'18'
:目标版本:React 18需要react-compiler-runtime
包。¥**
'18'
**: Target React 18. Requiresreact-compiler-runtime
package. -
'17'
:目标版本:React 17需要react-compiler-runtime
包。¥**
'17'
**: Target React 17. Requiresreact-compiler-runtime
package.
注意事项
¥Caveats
-
请务必使用字符串值,而不是数字(例如,
'17'
而不是17
)¥Always use string values, not numbers (e.g.,
'17'
not17
) -
不要包含补丁版本(例如,使用
'18'
而不是'18.2.0'
)¥Don’t include patch versions (e.g., use
'18'
not'18.2.0'
) -
React 19 包含内置编译器运行时 API
¥React 19 includes built-in compiler runtime APIs
-
React 17 和 18 需要安装
react-compiler-runtime@rc
¥React 17 and 18 require installing
react-compiler-runtime@rc
用法
¥Usage
目标版本:React 19(默认)
¥Targeting React 19 (default)
对于 React 19,无需特殊配置:
¥For React 19, no special configuration is needed:
{
// defaults to target: '19'
}
编译器将使用 React 19 的内置运行时 API:
¥The compiler will use React 19’s built-in runtime APIs:
// Compiled output uses React 19's native APIs
import { c as _c } from 'react/compiler-runtime';
目标平台:React 17 或 18
¥Targeting React 17 or 18
对于 React 17 和 React 18 项目,你需要两个步骤:
¥For React 17 and React 18 projects, you need two steps:
-
安装运行时包:
¥Install the runtime package:
npm install react-compiler-runtime@rc
-
配置目标:
¥Configure the target:
// For React 18
{
target: '18'
}
// For React 17
{
target: '17'
}
编译器将使用两个版本的 polyfill 运行时:
¥The compiler will use the polyfill runtime for both versions:
// Compiled output uses the polyfill
import { c as _c } from 'react-compiler-runtime';
故障排除
¥Troubleshooting
关于缺少编译器运行时的运行时错误
¥Runtime errors about missing compiler runtime
如果你看到类似 “找不到模块 ‘react/compiler-runtime’” 的错误:
¥If you see errors like “Cannot find module ‘react/compiler-runtime’“:
-
检查你的 React 版本:
¥Check your React version:
npm why react -
如果使用的是 React 17 或 18,请安装运行时:
¥If using React 17 or 18, install the runtime:
npm install react-compiler-runtime@rc -
确保你的目标与你的 React 版本匹配:
¥Ensure your target matches your React version:
{target: '18' // Must match your React major version}
运行时包无法正常工作
¥Runtime package not working
确保运行时包是:
¥Ensure the runtime package is:
-
安装在你的项目中(而非全局安装)
¥Installed in your project (not globally)
-
列在你的
package.json
依赖中¥Listed in your
package.json
dependencies -
正确的版本(
@rc
标签)¥The correct version (
@rc
tag) -
devDependencies
中没有(运行时需要)¥Not in
devDependencies
(it’s needed at runtime)
检查编译输出
¥Checking compiled output
要验证是否使用了正确的运行时,请注意不同的导入(react/compiler-runtime
为内置包,react-compiler-runtime
为 17/18 的独立包):
¥To verify the correct runtime is being used, note the different import (react/compiler-runtime
for builtin, react-compiler-runtime
standalone package for 17/18):
// For React 19 (built-in runtime)
import { c } from 'react/compiler-runtime'
// ^
// For React 17/18 (polyfill runtime)
import { c } from 'react-compiler-runtime'
// ^