Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@vue/babel-plugin-transform-vue-jsx

vuejs2.6mMIT1.4.0

Babel plugin for Vue 2.0 JSX

readme

@vue/babel-plugin-transform-vue-jsx

Babel plugin for Vue 2.0 JSX

Babel Compatibility Notes

Requirements

  • Assumes you are using Babel with a module bundler e.g. Webpack, because the spread merge helper is imported as a module to avoid duplication.

  • This is mutually exclusive with babel-plugin-transform-react-jsx.

Usage

npm install @vue/babel-plugin-transform-vue-jsx --save-dev
npm install @vue/babel-helper-vue-jsx-merge-props --save

In your .babelrc:

{
  "plugins": ["transform-vue-jsx"]
}

However it is recommended to use the configurable preset instead.

Details

The plugin transpiles the following JSX:

<div id="foo">{this.text}</div>

To the following JavaScript:

h(
  'div',
  {
    attrs: {
      id: 'foo',
    },
  },
  [this.text],
)

Note the h function, which is a shorthand for a Vue instance's $createElement method, must be in the scope where the JSX is. Since this method is passed to component render functions as the first argument, in most cases you'd do this:

Vue.component('jsx-example', {
  render(h) {
    // <-- h must be in scope
    return <div id="foo">bar</div>
  },
})

Difference from React JSX

First, Vue 2.0's vnode format is different from React's. The second argument to the createElement call is a "data object" that accepts nested objects. Each nested object will be then processed by corresponding modules:

render (h) {
  return h('div', {
    // Component props
    props: {
      msg: 'hi'
    },
    // Normal HTML attributes
    attrs: {
      id: 'foo'
    },
    // DOM props
    domProps: {
      innerHTML: 'bar'
    },
    // Event handlers are nested under "on", though
    // modifiers such as in v-on:keyup.enter are not
    // supported. You'll have to manually check the
    // keyCode in the handler instead.
    on: {
      click: this.clickHandler
    },
    // For components only. Allows you to listen to
    // native events, rather than events emitted from
    // the component using vm.$emit.
    nativeOn: {
      click: this.nativeClickHandler
    },
    // Class is a special module, same API as `v-bind:class`
    class: {
      foo: true,
      bar: false
    },
    // Style is also same as `v-bind:style`
    style: {
      color: 'red',
      fontSize: '14px'
    },
    // Other special top-level properties
    key: 'key',
    ref: 'ref',
    // Assign the `ref` is used on elements/components with v-for
    refInFor: true,
    slot: 'slot'
  })
}

The equivalent of the above in Vue 2.0 JSX is:

render (h) {
  return (
    <div
      // Component props
      propsMsg="hi"
      // Normal attributes or component props.
      id="foo"
      // DOM properties are prefixed with `domProps`
      domPropsInnerHTML="bar"
      // event listeners are prefixed with `on` or `nativeOn`
      onClick={this.clickHandler}
      nativeOnClick={this.nativeClickHandler}
      // other special top-level properties
      class={{ foo: true, bar: false }}
      style={{ color: 'red', fontSize: '14px' }}
      key="key"
      ref="ref"
      // assign the `ref` is used on elements/components with v-for
      refInFor
      slot="slot">
    </div>
  )
}

Component Tip

If a custom element starts with lowercase, it will be treated as a string id and used to lookup a registered component. If it starts with uppercase, it will be treated as an identifier, which allows you to do:

import Todo from './Todo.js'

export default {
  render(h) {
    return <Todo /> // no need to register Todo via components option
  },
}

JSX Spread

JSX spread is supported, and this plugin will intelligently merge nested data properties. For example:

const data = {
  class: ['b', 'c'],
}
const vnode = <div class="a" {...data} />

The merged data will be:

{ class: ['a', 'b', 'c'] }

Vue directives

Vue directives are usable the same way as in template with a few key differences:

  1. You can use directives camelCased instead of kebab-cased (vMyDirective is treated as v-my-directive)
  2. You have to use underscore sign instead of dots for modifiers because of JSXIdentifier limitation.
  3. Only runtime directives work (only v-show and custom directives), compile-time directives are out of this project's scope.

A full example would be: <MyComponent vMyDirective:argument_modifier1_modifier2={someExpression} />

changelog

1.3.1 (2022-07-21)

other

Bug Fixes

  • relax the vue peer dependency version requirement (8b5f023)

1.3.0 (2022-07-06)

other

Features

  • add importSource option (#284) (abffc65)
  • rework the compositionAPI option of the preset to support Vue 2.7 (e7d094e)

1.2.4 (2020-10-27)

other

Bug Fixes

  • composition-api-render-instance: store currentInstance in variable for render instance (#168) (a3525bf)

1.2.3 (2020-10-20)

other

Bug Fixes

1.2.2 (2020-10-17)

other

Bug Fixes

  • functional-vue & inject-h should traverse before JSX plugin (#166) (8969609), closes #165

1.2.1 (2020-10-16)

other

Bug Fixes

  • add composition-api packages to dependencies (cd9db9f)

1.2.0 (2020-10-16)

other

Features

  • add @vue/composition-api support (#142) (ecc6ed6)
  • allow prior babel plugins to traverse JSX tree throughly, close #86 (b49fa8a)
  • change all sugar plugins to work without pre-traversing the Program (0943580)

1.1.2 (2019-11-09)

other

Bug Fixes

1.1.1 (2019-10-11)

other

Bug Fixes

  • v-model: create non-existent properties as reactive (05b9b3a)

1.1.0 (2019-07-23)

other

Bug Fixes

  • support for .passive modifier (01177c8)

1.0.0 (2019-05-08)

other

Bug Fixes

  • Support props with underscore, close #55 (852481c)

1.0.0-beta.3 (2019-03-22)

other

Bug Fixes

  • filter out jsx comments in getChildren (7f0c84c), closes #46
  • fix incorrect repository urls (99380b3)

1.0.0-beta.2 (2019-01-11)

other

Bug Fixes

  • remove extraneous peer deps (29414a7)
  • Trim whitespaces properly, fix #37 (54c75ee)

    Features

  • Support root-level attributes, close #32 (96b182c)

1.0.0-beta.1 (2018-12-25)

other

Bug Fixes

  • Add events at the begining of argument list (0604214)
  • Add staticClass as root attribute (cd3bab1)
  • Do not trim all spaces (c5ebfac)
  • Fix failing tests (21213df)
  • Force html & svg tags to always be treated as string tags (12a311e)
  • proper support for camelCase (a903610)
  • Support camelCase directives (6a43377)
  • Support default export in functional component (7e6f893)
  • throw an error if v-model is used with a string (82d6bcb)

    Features

  • Add release utilities (4bb22fb)

  • add support for argument and modifiers for directives (0085b8f)
  • change the syntax for argument and modifiers (b1c8036)
  • Event modifiers for v-on (cef09bb)
  • implement babel preset (1137c1d)
  • Support vModel in kebab-case components (dc0e29f)
  • Treat string as component if declared in scope (51ca488)