@babel/plugin-transform-react-jsx

Example

React

In

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

Out

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

Custom

In

  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

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

Fragments

Fragments are a feature available in React 16.2.0+.

React

In

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

Out

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

Custom

In

  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

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

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

Installation

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

Usage

.babelrc

Without options:

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

With options:

  1. {
  2. "plugins": [
  3. ["@babel/plugin-transform-react-jsx", {
  4. "pragma": "Preact.h", // default pragma is React.createElement
  5. "pragmaFrag": "Preact.Fragment", // default is React.Fragment
  6. "throwIfNamespace": false // defaults to true
  7. }]
  8. ]
  9. }

Via CLI

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

Via Node API

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

Options

pragma

string, defaults to React.createElement.

Replace the function used when compiling JSX expressions.

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.

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.

throwIfNamespace

boolean, defaults to true.

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

  1. <f:image />

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

You can read more about configuring plugin options here