@babel/plugin-transform-react-jsx

transform-react-jsx - 图1info

This plugin is included in @babel/preset-react

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 Automatic Runtime

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

In

JavaScript

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

Out

JavaScript

  1. import { jsx as _jsx } from "react/jsx-runtime";
  2. import { jsxs as _jsxs } from "react/jsx-runtime";
  3. const profile = _jsxs("div", {
  4. children: [
  5. _jsx("img", {
  6. src: "avatar.png",
  7. className: "profile",
  8. }),
  9. _jsx("h3", {
  10. children: [user.firstName, user.lastName].join(" "),
  11. }),
  12. ],
  13. });

Customizing the Automatic Runtime Import

In

JavaScript

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

Out

JavaScript

  1. import { jsx as _jsx } from "custom-jsx-library/jsx-runtime";
  2. import { jsxs as _jsxs } from "custom-jsx-library/jsx-runtime";
  3. const profile = _jsxs("div", {
  4. children: [
  5. _jsx("img", {
  6. src: "avatar.png",
  7. className: "profile",
  8. }),
  9. _jsx("h3", {
  10. children: [user.firstName, user.lastName].join(" "),
  11. }),
  12. ],
  13. });

In

JavaScript

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

Out

JavaScript

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

React Classic Runtime

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.

In

JavaScript

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

Out

JavaScript

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

Customizing the Classic Runtime Import

In

JavaScript

  1. /** @jsx Preact.h */
  2. import Preact from "preact";
  3. const profile = (
  4. <div>
  5. <img src="avatar.png" className="profile" />
  6. <h3>{[user.firstName, user.lastName].join(" ")}</h3>
  7. </div>
  8. );

Out

JavaScript

  1. /** @jsx Preact.h */
  2. import Preact from "preact";
  3. const profile = Preact.h(
  4. "div",
  5. null,
  6. Preact.h("img", { src: "avatar.png", className: "profile" }),
  7. Preact.h("h3", null, [user.firstName, user.lastName].join(" "))
  8. );

Fragments

Fragments are a feature available in React 16.2.0+.

React Automatic Runtime

In

JavaScript

  1. const descriptions = items.map((item) => (
  2. <>
  3. <dt>{item.name}</dt>
  4. <dd>{item.value}</dd>
  5. </>
  6. ));

Out

JavaScript

  1. import { jsxs as _jsxs } from "react/jsx-runtime";
  2. import { Fragment as _Fragment } from "react/jsx-runtime";
  3. import { jsx as _jsx } from "react/jsx-runtime";
  4. const descriptions = items.map((item) =>
  5. _jsxs(_Fragment, {
  6. children: [
  7. _jsx("dt", {
  8. children: item.name,
  9. }),
  10. _jsx("dd", {
  11. children: item.value,
  12. }),
  13. ],
  14. })
  15. );

React Classic Runtime

In

JavaScript

  1. const descriptions = items.map((item) => (
  2. <>
  3. <dt>{item.name}</dt>
  4. <dd>{item.value}</dd>
  5. </>
  6. ));

Out

JavaScript

  1. const descriptions = items.map((item) =>
  2. React.createElement(
  3. React.Fragment,
  4. null,
  5. React.createElement("dt", null, item.name),
  6. React.createElement("dd", null, item.value)
  7. )
  8. );

Customizing with the Classic Runtime

In

JavaScript

  1. /** @jsx Preact.h */
  2. /** @jsxFrag Preact.Fragment */
  3. import Preact from "preact";
  4. var descriptions = items.map((item) => (
  5. <>
  6. <dt>{item.name}</dt>
  7. <dd>{item.value}</dd>
  8. </>
  9. ));

Out

JavaScript

  1. /** @jsx Preact.h */
  2. /** @jsxFrag Preact.Fragment */
  3. import Preact from "preact";
  4. var descriptions = items.map((item) =>
  5. Preact.h(
  6. Preact.Fragment,
  7. null,
  8. Preact.h("dt", null, item.name),
  9. Preact.h("dd", null, item.value)
  10. )
  11. );

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
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-transform-react-jsx
  1. yarn add --dev @babel/plugin-transform-react-jsx
  1. pnpm add --save-dev @babel/plugin-transform-react-jsx

Usage

Without options:

babel.config.json

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

With options:

babel.config.json

  1. {
  2. "plugins": [
  3. [
  4. "@babel/plugin-transform-react-jsx",
  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 script.js

Via Node API

JavaScript

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

Options

Both Runtimes

throwIfNamespace

boolean, defaults to true.

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

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

transform-react-jsx - 图2tip

You can read more about configuring plugin options here

runtime

classic | automatic, defaults to classic

Added in: v7.9.0

Decides which runtime to use.

automatic auto imports the functions that JSX transpiles to. classic does not automatically import anything.

React Automatic Runtime

importSource

string, defaults to react.

Added in: v7.9.0

Replaces the import source when importing functions.

React Classic Runtime

pragma

string, defaults to React.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).

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

pragmaFrag

string, defaults to React.Fragment.

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

useBuiltIns

boolean, defaults to false.

When spreading props, use Object.assign directly instead of Babel’s extend helper.

useSpread

boolean, defaults to false.

When spreading props, use inline object with spread elements directly instead of Babel’s extend helper or Object.assign.