What is @vue/babel-plugin-transform-vue-jsx?
@vue/babel-plugin-transform-vue-jsx is a Babel plugin that allows you to use JSX syntax in Vue.js applications. This plugin transforms JSX into Vue render functions, enabling a more React-like development experience within the Vue ecosystem.
What are @vue/babel-plugin-transform-vue-jsx's main functionalities?
Basic JSX Syntax
This feature allows you to write Vue components using JSX syntax, which can be more concise and readable compared to traditional Vue templates.
const MyComponent = { render() { return <div>Hello, JSX!</div>; } };
Using Props
You can easily pass props to your components using JSX, making it straightforward to manage dynamic data.
const MyComponent = { props: ['message'], render() { return <div>{this.message}</div>; } };
Event Handling
JSX syntax allows for inline event handling, making it easy to attach event listeners directly within your render functions.
const MyComponent = { render() { return <button onClick={() => alert('Clicked!')}>Click Me</button>; } };
Conditional Rendering
Conditional rendering in JSX is straightforward, allowing you to use JavaScript expressions to determine what gets rendered.
const MyComponent = { data() { return { isVisible: true }; }, render() { return this.isVisible ? <div>Visible</div> : <div>Hidden</div>; } };
Looping Through Data
JSX makes it easy to loop through arrays and render lists of elements, leveraging JavaScript's array methods.
const MyComponent = { data() { return { items: ['Item 1', 'Item 2', 'Item 3'] }; }, render() { return <ul>{this.items.map(item => <li>{item}</li>)}</ul>; } };
Other packages similar to @vue/babel-plugin-transform-vue-jsx
babel-plugin-transform-react-jsx
This Babel plugin transforms JSX syntax into React.createElement calls. It is primarily used in React projects and offers similar functionality to @vue/babel-plugin-transform-vue-jsx but is tailored for React instead of Vue.
vue-jsx
vue-jsx is another package that provides JSX support for Vue.js. It offers similar functionality to @vue/babel-plugin-transform-vue-jsx but may have different configuration options and performance characteristics.
babel-plugin-jsx-pragmatic
This plugin allows you to use JSX with any library by specifying a pragma. It is more flexible than @vue/babel-plugin-transform-vue-jsx but requires additional configuration to work with Vue.
@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) {
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', {
props: {
msg: 'hi'
},
attrs: {
id: 'foo'
},
domProps: {
innerHTML: 'bar'
},
on: {
click: this.clickHandler
},
nativeOn: {
click: this.nativeClickHandler
},
class: {
foo: true,
bar: false
},
style: {
color: 'red',
fontSize: '14px'
},
key: 'key',
ref: 'ref',
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 />
},
}
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:
- You can use directives camelCased instead of kebab-cased (vMyDirective is treated as
v-my-directive
)
- You have to use underscore sign instead of dots for modifiers because of JSXIdentifier limitation.
- 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} />