|
| 1 | +## @vue/babel-plugin-transform-vue-jsx |
| 2 | + |
| 3 | +> Babel plugin for Vue 2.0 JSX |
| 4 | +
|
| 5 | +### Babel Compatibility Notes |
| 6 | + |
| 7 | +- This repo is only compatible with Babel 7.x, for 6.x please use [vuejs/babel-plugin-transform-vue-jsx](https://github.com/vuejs/babel-plugin-transform-vue-jsx) |
| 8 | + |
| 9 | +### Requirements |
| 10 | + |
| 11 | +- 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. |
| 12 | + |
| 13 | +- This is mutually exclusive with `babel-plugin-transform-react-jsx`. |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install @vue/babel-plugin-transform-vue-jsx --save-dev |
| 19 | +npm install @vue/babel-helper-vue-jsx-merge-props --save |
| 20 | +``` |
| 21 | + |
| 22 | +In your `.babelrc`: |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "plugins": ["transform-vue-jsx"] |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +However it is recommended to use the [configurable preset](../babel-preset-jsx/README.md) instead. |
| 31 | + |
| 32 | +### Details |
| 33 | + |
| 34 | +The plugin transpiles the following JSX: |
| 35 | + |
| 36 | +```jsx |
| 37 | +<div id="foo">{this.text}</div> |
| 38 | +``` |
| 39 | + |
| 40 | +To the following JavaScript: |
| 41 | + |
| 42 | +```js |
| 43 | +h( |
| 44 | + 'div', |
| 45 | + { |
| 46 | + attrs: { |
| 47 | + id: 'foo', |
| 48 | + }, |
| 49 | + }, |
| 50 | + [this.text], |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +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: |
| 55 | + |
| 56 | +```js |
| 57 | +Vue.component('jsx-example', { |
| 58 | + render(h) { |
| 59 | + // <-- h must be in scope |
| 60 | + return <div id="foo">bar</div> |
| 61 | + }, |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +### Difference from React JSX |
| 66 | + |
| 67 | +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: |
| 68 | + |
| 69 | +```js |
| 70 | +render (h) { |
| 71 | + return h('div', { |
| 72 | + // Component props |
| 73 | + props: { |
| 74 | + msg: 'hi' |
| 75 | + }, |
| 76 | + // Normal HTML attributes |
| 77 | + attrs: { |
| 78 | + id: 'foo' |
| 79 | + }, |
| 80 | + // DOM props |
| 81 | + domProps: { |
| 82 | + innerHTML: 'bar' |
| 83 | + }, |
| 84 | + // Event handlers are nested under "on", though |
| 85 | + // modifiers such as in v-on:keyup.enter are not |
| 86 | + // supported. You'll have to manually check the |
| 87 | + // keyCode in the handler instead. |
| 88 | + on: { |
| 89 | + click: this.clickHandler |
| 90 | + }, |
| 91 | + // For components only. Allows you to listen to |
| 92 | + // native events, rather than events emitted from |
| 93 | + // the component using vm.$emit. |
| 94 | + nativeOn: { |
| 95 | + click: this.nativeClickHandler |
| 96 | + }, |
| 97 | + // Class is a special module, same API as `v-bind:class` |
| 98 | + class: { |
| 99 | + foo: true, |
| 100 | + bar: false |
| 101 | + }, |
| 102 | + // Style is also same as `v-bind:style` |
| 103 | + style: { |
| 104 | + color: 'red', |
| 105 | + fontSize: '14px' |
| 106 | + }, |
| 107 | + // Other special top-level properties |
| 108 | + key: 'key', |
| 109 | + ref: 'ref', |
| 110 | + // Assign the `ref` is used on elements/components with v-for |
| 111 | + refInFor: true, |
| 112 | + slot: 'slot' |
| 113 | + }) |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +The equivalent of the above in Vue 2.0 JSX is: |
| 118 | + |
| 119 | +```jsx |
| 120 | +render (h) { |
| 121 | + return ( |
| 122 | + <div |
| 123 | + // Component props |
| 124 | + propsMsg="hi" |
| 125 | + // Normal attributes or component props. |
| 126 | + id="foo" |
| 127 | + // DOM properties are prefixed with `domProps` |
| 128 | + domPropsInnerHTML="bar" |
| 129 | + // event listeners are prefixed with `on` or `nativeOn` |
| 130 | + onClick={this.clickHandler} |
| 131 | + nativeOnClick={this.nativeClickHandler} |
| 132 | + // other special top-level properties |
| 133 | + class={{ foo: true, bar: false }} |
| 134 | + style={{ color: 'red', fontSize: '14px' }} |
| 135 | + key="key" |
| 136 | + ref="ref" |
| 137 | + // assign the `ref` is used on elements/components with v-for |
| 138 | + refInFor |
| 139 | + slot="slot"> |
| 140 | + </div> |
| 141 | + ) |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### Component Tip |
| 146 | + |
| 147 | +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: |
| 148 | + |
| 149 | +```js |
| 150 | +import Todo from './Todo.js' |
| 151 | + |
| 152 | +export default { |
| 153 | + render(h) { |
| 154 | + return <Todo /> // no need to register Todo via components option |
| 155 | + }, |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### JSX Spread |
| 160 | + |
| 161 | +JSX spread is supported, and this plugin will intelligently merge nested data properties. For example: |
| 162 | + |
| 163 | +```jsx |
| 164 | +const data = { |
| 165 | + class: ['b', 'c'], |
| 166 | +} |
| 167 | +const vnode = <div class="a" {...data} /> |
| 168 | +``` |
| 169 | + |
| 170 | +The merged data will be: |
| 171 | + |
| 172 | +```js |
| 173 | +{ class: ['a', 'b', 'c'] } |
| 174 | +``` |
| 175 | + |
| 176 | +### Vue directives |
| 177 | + |
| 178 | +Vue directives are usable the same way as in template with a few key differences: |
| 179 | + |
| 180 | +1. You can use directives camelCased instead of kebab-cased (vMyDirective is treated as `v-my-directive`) |
| 181 | +2. You have to use underscore sign instead of dots for modifiers because of JSXIdentifier limitation. |
| 182 | +3. Only runtime directives work (only v-show and custom directives), compile-time directives are out of this project's scope. |
| 183 | + |
| 184 | +A full example would be: `<MyComponent vMyDirective:argument_modifier1_modifier2={someExpression} />` |
0 commit comments