Skip to main content

@babel/plugin-transform-react-jsx

info

这个插件包含在 @babel/preset-react 中

这个插件可以生成可用于生产的 JS 代码。如果你正在开发环境中开发 React 应用,请使用 @babel/plugin-transform-react-jsx-development 来获得更好的调试体验。

🌐 This plugin generates production-ready JS code. If you are developing a React app in a development environment, please use @babel/plugin-transform-react-jsx-development for a better debugging experience.

示例​

🌐 Example

React 自动运行时​

🌐 React Automatic Runtime

自动运行时是 v7.9.0 中新增的一个功能。启用这个运行时后,JSX 编译的函数会自动被导入。

🌐 Automatic runtime is a feature added in v7.9.0. With this runtime enabled, the functions that JSX compiles to will be imported automatically.

在

JavaScript
const profile = (
<div>
<img src="avatar.png" className="profile" />
<h3>{[user.firstName, user.lastName].join(" ")}</h3>
</div>
);

出

JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";

const profile = _jsxs("div", {
children: [
_jsx("img", {
src: "avatar.png",
className: "profile",
}),
_jsx("h3", {
children: [user.firstName, user.lastName].join(" "),
}),
],
});

自定义自动运行时导入​

🌐 Customizing the Automatic Runtime Import

在

JavaScript
/** @jsxImportSource custom-jsx-library */

const profile = (
<div>
<img src="avatar.png" className="profile" />
<h3>{[user.firstName, user.lastName].join(" ")}</h3>
</div>
);

出

JavaScript
import { jsx as _jsx } from "custom-jsx-library/jsx-runtime";
import { jsxs as _jsxs } from "custom-jsx-library/jsx-runtime";

const profile = _jsxs("div", {
children: [
_jsx("img", {
src: "avatar.png",
className: "profile",
}),
_jsx("h3", {
children: [user.firstName, user.lastName].join(" "),
}),
],
});

在

JavaScript
/** @jsxRuntime classic */

const profile = (
<div>
<img src="avatar.png" className="profile" />
<h3>{[user.firstName, user.lastName].join(" ")}</h3>
</div>
);

出

JavaScript
const profile = React.createElement(
"div",
null,
React.createElement("img", { src: "avatar.png", className: "profile" }),
React.createElement("h3", null, [user.firstName, user.lastName].join(" "))
);

React 经典运行时​

🌐 React Classic Runtime

如果你不想(或不能使用)自动导入,你可以使用经典运行时,这是 v7 和更早版本的默认行为。

🌐 If you do not want (or cannot use) auto importing, you can use the classic runtime, which is the default behavior for v7 and prior.

在

JavaScript
const profile = (
<div>
<img src="avatar.png" className="profile" />
<h3>{[user.firstName, user.lastName].join(" ")}</h3>
</div>
);

出

JavaScript
const profile = React.createElement(
"div",
null,
React.createElement("img", { src: "avatar.png", className: "profile" }),
React.createElement("h3", null, [user.firstName, user.lastName].join(" "))
);

自定义经典运行时导入​

🌐 Customizing the Classic Runtime Import

在

JavaScript
/** @jsx Preact.h */

import Preact from "preact";

const profile = (
<div>
<img src="avatar.png" className="profile" />
<h3>{[user.firstName, user.lastName].join(" ")}</h3>
</div>
);

出

JavaScript
/** @jsx Preact.h */

import Preact from "preact";

const profile = Preact.h(
"div",
null,
Preact.h("img", { src: "avatar.png", className: "profile" }),
Preact.h("h3", null, [user.firstName, user.lastName].join(" "))
);

片段​

🌐 Fragments

Fragments 是 React 16.2.0 及以上版本提供的一个特性。

React 自动运行时​

🌐 React Automatic Runtime

在

JavaScript
const descriptions = items.map((item) => (
<>
<dt>{item.name}</dt>
<dd>{item.value}</dd>
</>
));

出

JavaScript
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";

const descriptions = items.map((item) =>
_jsxs(_Fragment, {
children: [
_jsx("dt", {
children: item.name,
}),
_jsx("dd", {
children: item.value,
}),
],
})
);

React 经典运行时​

🌐 React Classic Runtime

在

JavaScript
const descriptions = items.map((item) => (
<>
<dt>{item.name}</dt>
<dd>{item.value}</dd>
</>
));

出

JavaScript
const descriptions = items.map((item) =>
React.createElement(
React.Fragment,
null,
React.createElement("dt", null, item.name),
React.createElement("dd", null, item.value)
)
);

使用经典运行时进行自定义​

🌐 Customizing with the Classic Runtime

在

JavaScript
/** @jsx Preact.h */
/** @jsxFrag Preact.Fragment */

import Preact from "preact";

var descriptions = items.map((item) => (
<>
<dt>{item.name}</dt>
<dd>{item.value}</dd>
</>
));

出

JavaScript
/** @jsx Preact.h */
/** @jsxFrag Preact.Fragment */

import Preact from "preact";

var descriptions = items.map((item) =>
Preact.h(
Preact.Fragment,
null,
Preact.h("dt", null, item.name),
Preact.h("dd", null, item.value)
)
);

注意,如果指定了自定义 pragma,那么如果使用片段语法 <></>,也必须指定自定义片段 pragma。否则,会抛出错误。

🌐 Note that if a custom pragma is specified, then a custom fragment pragma must also be specified if the fragment syntax <></> is used. Otherwise, an error will be thrown.

安装​

🌐 Installation

npm install --save-dev @babel/plugin-transform-react-jsx

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

没有选项:

🌐 Without options:

babel.config.json
{
"plugins": ["@babel/plugin-transform-react-jsx"]
}

有选项:

🌐 With options:

babel.config.json
{
"plugins": [
[
"@babel/plugin-transform-react-jsx",
{
"throwIfNamespace": false, // defaults to true
"runtime": "automatic", // defaults to autoamtic
"importSource": "custom-jsx-library" // defaults to react
}
]
]
}

通过 CLI​

🌐 Via CLI

Shell
babel --plugins @babel/plugin-transform-react-jsx script.js

通过 Node API​

🌐 Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-react-jsx"],
});

选项​

🌐 Options

两个运行时​

🌐 Both Runtimes

throwIfNamespace​

boolean,默认值为 true。

切换是否在使用带命名空间的 XML 标签名时抛出错误。例如:

🌐 Toggles whether or not to throw an error if an XML namespaced tag name is used. For example:

尽管 JSX 规范允许这样做,但默认情况下它是禁用的,因为 React 的 JSX 目前不支持它。

🌐 Though the JSX spec allows this, it is disabled by default since React's JSX does not currently have support for it.

tip

你可以在这里了解更多关于配置插件选项的内容

runtime​

classic | automatic,默认值为 automatic

已添加于:v7.9.0

🌐 Added in: v7.9.0

决定使用哪个运行时。

🌐 Decides which runtime to use.

automatic 会自动导入 JSX 转译所用的函数。classic 不会自动导入任何东西。

React 自动运行时​

🌐 React Automatic Runtime

importSource​

string,默认值为 react。

已添加于:v7.9.0

🌐 Added in: v7.9.0

导入函数时替换导入源。

🌐 Replaces the import source when importing functions.

React 经典运行时​

🌐 React Classic Runtime

pragma​

string,默认是 React.createElement。

替换在编译 JSX 表达式时使用的函数。它应该是一个限定名称(例如 React.createElement)或一个标识符(例如 createElement)。

🌐 Replace the function used when compiling JSX expressions. It should be a qualified name (e.g. React.createElement) or an identifier (e.g. createElement).

注意,@jsx React.DOM 指令从 React v0.12 起已经废弃了

🌐 Note that the @jsx React.DOM pragma has been deprecated as of React v0.12

pragmaFrag​

string,默认是 React.Fragment。

替换在编译 JSX 片段时使用的组件。它应该是一个有效的 JSX 标签名。

🌐 Replace the component used when compiling JSX fragments. It should be a valid JSX tag name.