@babel/plugin-transform-react-jsx-development
信息
此插件包含在 @babel/preset-react
中
此插件是 @babel/plugin-transform-react-jsx
的开发版本。它旨在为调试 React 应用程序提供增强的验证错误消息和精确的代码位置信息。请注意,此插件旨在在开发环境中使用,因为它生成的输出比生产版本多得多。
如果您正在使用 @babel/preset-react
,它将由 development
选项自动启用,因此您无需安装它。
示例
输入
input.jsx
const profile = (
<div>
<img src="avatar.png" className="profile" />
<h3>{[user.firstName, user.lastName].join(" ")}</h3>
</div>
);
输出
output.js
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
const _jsxFileName = "input.jsx";
const profile = _jsxDEV("div", {
children: [
_jsxDEV("img", {
src: "avatar.png",
className: "profile",
}, undefined, false, { fileName: _jsxFileName, lineNumber: 3, columnNumber: 5 }, this),
_jsxDEV("h3", {
children: [user.firstName, user.lastName].join(" "),
}, undefined, false, { fileName: _jsxFileName, lineNumber: 4, columnNumber: 5 }, this),
]},
undefined, false, { fileName: _jsxFileName, lineNumber: 2, columnNumber: 3 }, this
);
安装
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-react-jsx-development
yarn add --dev @babel/plugin-transform-react-jsx-development
pnpm add --save-dev @babel/plugin-transform-react-jsx-development
用法
使用配置文件(推荐)
无选项
babel.config.json
{
"plugins": ["@babel/plugin-transform-react-jsx-development"]
}
带选项
babel.config.json
{
"plugins": [
[
"@babel/plugin-transform-react-jsx-development",
{
"throwIfNamespace": false, // defaults to true
"runtime": "automatic", // defaults to classic
"importSource": "custom-jsx-library" // defaults to react
}
]
]
}
通过 CLI
Shell
babel --plugins @babel/plugin-transform-react-jsx-development script.js
通过 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-react-jsx-development"],
});
选项
它接受与 @babel/plugin-transform-react-jsx
相同的选项。