@babel/plugin-transform-react-jsx-development

transform-react-jsx-development - 图1info

This plugin is included in @babel/preset-react

This plugin is a developer version of @babel/plugin-transform-react-jsx. It is designed to provide enhanced validation error messages and precise code location information for debugging React apps. Note that this plugin is intended to be used in a development environment, as it generates significantly more outputs than the production build.

If you are using @babel/preset-react, it will be automatically enabled by the development option so you don’t have to install it.

Example

In

input.jsx

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

Out

output.js

  1. import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
  2. const _jsxFileName = "input.jsx";
  3. const profile = _jsxDEV("div", {
  4. children: [
  5. _jsxDEV("img", {
  6. src: "avatar.png",
  7. className: "profile",
  8. }, undefined, false, { fileName: _jsxFileName, lineNumber: 3, columnNumber: 5 }, this),
  9. _jsxDEV("h3", {
  10. children: [user.firstName, user.lastName].join(" "),
  11. }, undefined, false, { fileName: _jsxFileName, lineNumber: 4, columnNumber: 5 }, this),
  12. ]},
  13. undefined, false, { fileName: _jsxFileName, lineNumber: 2, columnNumber: 3 }, this
  14. );

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-transform-react-jsx-development
  1. yarn add --dev @babel/plugin-transform-react-jsx-development
  1. pnpm add --save-dev @babel/plugin-transform-react-jsx-development

Usage

Without options:

babel.config.json

  1. {
  2. "plugins": ["@babel/plugin-transform-react-jsx-development"]
  3. }

With options:

babel.config.json

  1. {
  2. "plugins": [
  3. [
  4. "@babel/plugin-transform-react-jsx-development",
  5. {
  6. "throwIfNamespace": false, // defaults to true
  7. "runtime": "automatic", // defaults to classic
  8. "importSource": "custom-jsx-library" // defaults to react
  9. }
  10. ]
  11. ]
  12. }

Via CLI

Shell

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

Via Node API

JavaScript

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

Options

It accepts the same options as @babel/plugin-transform-react-jsx.