@cabloy/vue-babel-plugin-jsx

中文文档
2.0.1发布于 2026/03/22MITTypeScriptES Module近 30 天下载 5780

Vue 3 JSX 的 Babel 插件

中文使用文档
文档目录11 节
  1. 安装
  2. 使用量
  3. 选项
  4. 语法
  5. 内容
  6. 属性/ 道具
  7. 指令
  8. 槽
  9. 在类型脚本中
  10. 谁在用
  11. 兼容性

Vue 3 的 Babel 插件 JSX

npm package issues-helper

加入Vue JSX支持.

页:1 简体中文

安装

安装插件 :

npm install @vue/babel-plugin-jsx -D

然后将插件添加到您的 bail 配置 :

{
  "plugins": ["@vue/babel-plugin-jsx"]
}

使用量

选项

打开

类型 : boolean

默认 : false

转换 on: { click: xx } 改为 onClick: xxx

优化

类型 : boolean

默认 : false

启用时, 此插件将使用优化运行时代码 PatchFlags 财务报告和审定财务报表 SlotFlags 提高渲染性能。 然而,由于JSX的动态性质,优化不如Vue官方模板编译器中那样全面.

由于优化后的代码可能跳过某些重置以提高性能,因此我们强烈建议在允许此选项确保一切如所期望的那样工作后,彻底测试您的应用.

海关元素

类型 : (tag: string) => boolean

默认 : undefined

配置自定义元素

合并选项

类型 : boolean

默认 : true

合并静态和动态类/ 样式属性 / 在 XXX 处理器上

启用对象槽

类型 : boolean

默认 : true

是否启用 object slots (以下文中提及)语法". 在JSX中也许有用,但会增加很多 _isSlot 增加捆绑大小的条件表达式。 还有 v-slots 即使 enableObjectSlots 关闭.

贴纸

类型 : string

默认 : createVNode

替换汇编 JSX 表达式时使用的函数 .

分辨率类型

类型 : boolean

默认 : false

(单位:千美元)实验从类型推断组件元数据(例如:. props, (中文(简体) ), emits, (中文(简体) ), name) (中文(简体) ). 这是一个实验性特征,可能并非在所有情况下都可行.

语法

内容

职能构成部分

const App = () => <div>Vue 3.0</div>

已生效

const App = {
  render() {
    return <div>Vue 3.0</div>
  },
}
import { defineComponent, withModifiers } from 'vue'

const App = defineComponent({
  setup() {
    const count = ref(0)

    const inc = () => {
      count.value++
    }

    return () => <div onClick={withModifiers(inc, ['self'])}>{count.value}</div>
  },
})

断裂

const App = () => (
  <>
    <span>I'm</span>
    <span>Fragment</span>
  </>
)

属性/ 道具

const App = () => <input type="email" />

带有动态绑定的:

const placeholderText = 'email'
const App = () => <input type="email" placeholder={placeholderText} />

指令

v 显示

const App = {
  data() {
    return { visible: true }
  },
  render() {
    return <input v-show={this.visible} />
  },
}

v 型号

注意: 您应该通过第二个参数作为字符串来使用 arg。 。 。 .

<input v-model={val} />
<input v-model:argument={val} />
;<input v-model={[val, ['modifier']]} />
// Or
;<input v-model_modifier={val} />
;<A v-model={[val, 'argument', ['modifier']]} />
// Or
;<input v-model:argument_modifier={val} />

将汇编到:

h(A, {
  argument: val,
  argumentModifiers: {
    modifier: true,
  },
  'onUpdate:argument': ($event) => (val = $event),
})

注意:您应该通过一个二维矩阵给v-models.

<A v-models={[[foo], [bar, 'bar']]} />
<A
  v-models={[
    [foo, 'foo'],
    [bar, 'bar'],
  ]}
/>
<A
  v-models={[
    [foo, ['modifier']],
    [bar, 'bar', ['modifier']],
  ]}
/>

将汇编到:

h(A, {
  modelValue: foo,
  modelModifiers: {
    modifier: true,
  },
  'onUpdate:modelValue': ($event) => (foo = $event),
  bar,
  barModifiers: {
    modifier: true,
  },
  'onUpdate:bar': ($event) => (bar = $event),
})

自定义指令

使用字符串参数时推荐

const App = {
  directives: { custom: customDirective },
  setup() {
    return () => <a v-custom:arg={val} />
  },
}
const App = {
  directives: { custom: customDirective },
  setup() {
    return () => <a v-custom={[val, 'arg', ['a', 'b']]} />
  },
}

槽

说明: jsx, (中文(简体) ), v-slot 应替换为 v-slots

const A = (props, { slots }) => (
  <>
    <h1>{slots.default ? slots.default() : 'foo'}</h1>
    <h2>{slots.bar?.()}</h2>
  </>
)

const App = {
  setup() {
    const slots = {
      bar: () => <span>B</span>,
    }
    return () => (
      <A v-slots={slots}>
        <div>A</div>
      </A>
    )
  },
}

// or

const App2 = {
  setup() {
    const slots = {
      default: () => <div>A</div>,
      bar: () => <span>B</span>,
    }
    return () => <A v-slots={slots} />
  },
}

// or you can use object slots when `enableObjectSlots` is not false.
const App3 = {
  setup() {
    return () => (
      <>
        <A>
          {{
            default: () => <div>A</div>,
            bar: () => <span>B</span>,
          }}
        </A>
        <B>{() => 'foo'}</B>
      </>
    )
  },
}

在类型脚本中

tsconfig.json数字 :

{
  "compilerOptions": {
    "jsx": "preserve"
  }
}

谁在用


蚂蚁设计Vue

范特语

元素加号

韦杰森漂亮

兼容性

此 repo 仅兼容 :

  • 贝贝 7+
  • 第3号+

版本与发布记录

@cabloy/vue-babel-plugin-jsx 共发布 2 个版本,首次发布于 2025/01/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.0.1latest2026/03/227 个月前
1.2.52025/01/151.8 年前

关于 @cabloy/vue-babel-plugin-jsx 的常见问题

@cabloy/vue-babel-plugin-jsx 是什么?

Vue 3 JSX 的 Babel 插件

如何安装 @cabloy/vue-babel-plugin-jsx?

在项目目录运行 npm install @cabloy/vue-babel-plugin-jsx,或使用 pnpm add / yarn add / bun add。

@cabloy/vue-babel-plugin-jsx 支持 TypeScript 吗?

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

@cabloy/vue-babel-plugin-jsx 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cabloy/vue-babel-plugin-jsx 可以商用吗?

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

@cabloy/vue-babel-plugin-jsx 的下载量有多少?

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

@cabloy/vue-babel-plugin-jsx 还在维护吗?

最新版本 2.0.1 发布于 2026/03/22(7 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/01/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。