@emotion/babel-plugin-jsx-pragmatic

中文文档
0.3.0发布于 2024/07/19MITTypeScriptESM + CJS近 30 天下载 96.5万

Insert code to load a module corresponding to JSX pragma.

原始使用文档
文档目录4 节
  1. Options
  2. module
  3. import
  4. export

@emotion/babel-plugin-jsx-pragmatic

This package is a fork of babel-plugin-jsx-pragmatic to support React Fragments.

The original README of babel-plugin-jsx-pragmatic with some modifications is shown below.


@babel/plugin-transform-react-jsx has a pragma option that's used when transforming JSX to function calls instead of the default function React.createElement.

This Babel plugin is a companion to that feature that allows you to dynamically load a module associated with the pragma value.

Example:

Given this file:

<Some jsx="element" />

babel would normally transform the JSX to:

React.createElement(Some, { jsx: 'element' })

By setting the pragma option like this:

babel.transformSync(code, {
  plugins: [
    [
      '@babel/plugin-transform-react-jsx',
      {
        pragma: 'whatever'
      }
    ]
  ]
})

It would instead transform it to:

whatever(Some, { jsx: 'element' })

However, you might need to load a module corresponding to whatever in each module containing JSX:

import whatever from 'whatever'
// or
var whatever = require('whatever')

This plugin allows you to make that part dynamic as well:

babel.transformSync(code, {
  plugins: [
    [
      '@babel/plugin-transform-react-jsx',
      {
        pragma: 'whatever'
      }
    ],

    [
      '@emotion/babel-plugin-jsx-pragmatic',
      {
        module: '/something/whatever',
        import: 'whatever'
      }
    ]
  ]
})

Results in:

import { default as whatever } from '/something/whatever'

Options

module

String. Module ID or pathname. The value of the ModuleSpecifier of an import. Required.

import

String. The identifier that you want to import the module with. This should correspond to the root identifier of the pragma value. Required. Examples:

{
  plugins: [
    [
      '@babel/plugin-transform-react-jsx',
      {
        pragma: 'x'
      }
    ],

    [
      '@emotion/babel-plugin-jsx-pragmatic',
      {
        module: '/something/whatever',
        import: 'x'
      }
    ]
  ]
}

{
  plugins: [
    [
      '@babel/plugin-transform-react-jsx',
      {
        pragma: 'x.y'
      }
    ],

    [
      '@emotion/babel-plugin-jsx-pragmatic',
      {
        module: '/something/whatever',
        import: 'x'
      }
    ]
  ]
}

export

String. The export that you want to import as import from module. Default value is default (the default export). Examples:

// Will import the default export (`default`)
{
  module: "whatever",
  import: "x"
}
// import {default as x} from "whatever"


// Will import the default export (`default`)
{
  module: "whatever",
  import: "x",
  export: "default",
}
// import {default as x} from "whatever"


// Will import the export named `something`
{
  module: "whatever",
  import: "x",
  export: "something",
}
// import {something as x} from "whatever"

Known Issues

  • Doesn't do anything special in the case that the file being transformed already imports or declares an identifier with the same name as import.

  • Doesn't take into account when a file actually contains a JSX pragma comment.

版本与发布记录

@emotion/babel-plugin-jsx-pragmatic 共发布 9 个版本,首次发布于 2019/02/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.0latest2024/07/192 年前
0.2.12023/05/063 年前
0.2.02022/07/314 年前
0.1.52019/12/236 年前
0.1.42019/10/297 年前
0.1.32019/06/267 年前

关于 @emotion/babel-plugin-jsx-pragmatic 的常见问题

@emotion/babel-plugin-jsx-pragmatic 是什么?

Insert code to load a module corresponding to JSX pragma.

如何安装 @emotion/babel-plugin-jsx-pragmatic?

在项目目录运行 npm install @emotion/babel-plugin-jsx-pragmatic,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@babel/core(^7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@emotion/babel-plugin-jsx-pragmatic 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@emotion/babel-plugin-jsx-pragmatic 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@emotion/babel-plugin-jsx-pragmatic 可以商用吗?

@emotion/babel-plugin-jsx-pragmatic 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@emotion/babel-plugin-jsx-pragmatic 的下载量有多少?

根据 npm 官方下载统计,@emotion/babel-plugin-jsx-pragmatic 在 2026/10/14 – 2026/11/12 这 30 天内被下载 964,719 次,日均约 32,157 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@emotion/babel-plugin-jsx-pragmatic 还在维护吗?

最新版本 0.3.0 发布于 2024/07/19(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/02/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。