babel-plugin-vue-jsx-modifier
Support template modifiers (.sync, .capture, etc) in JSX
Overview
In Vue template, we can use some modifiers. See:
<!-- sync modifier --> <!-- passive modifier --> <!-- snip -->
But they are not supported in JSX. We must write messy JSX-spread instead.
<MyComponent = > <div /></MyComponent>
By this plugin, we can write equivalent JSX as below:
<MyComponent => <div = /></MyComponent>
Install
First, setup Vue environment and enable JSX transpilation (see https://github.com/vuejs/jsx).
And install this
npm install babel-plugin-vue-jsx-modifier -D
Usage
add "vue-jsx-modifier"
to "plugins" in your .babelrc
.
Example code for TypeScript:
// modifier functions can be imported from "lib/modifiers";;; ;
<MyComponent myProp={ __sync(this.value) }
will be transpiled to:
<MyComponent = >
Modifier functions
These functions are available.
NOTE: They should be removed by babel, and will throw Error if called at runtime.
Prop modifier
__sync
Insert handler which listens update event from child
// original code<MyComponent = /> // transpiled code<MyComponent = />
You can specify setter code as second argument.
// original code<MyComponent = /> // transpiled code<MyComponent = />
__relay
Similar to __sync
, and re-emit update event to parent instead of direct assignment.
// original code<MyComponent = /> // transpiled code<MyComponent = />
You can specify emit
method as second argument if you want to use method other than this.$emit
.
This is useful when using with @vue/composition-api
// original code<MyComponent = /> // transpiled code<MyComponent = />
Event modifiers
These modifiers can be used with event handler.
Transipilation will fail if target JSX attribute name does not start with on
or nativeOn
__capture
// original code<div = /> // transpiled code<div />
nativeOn is also supported
// original code<MyComponent = /> // transpiled code<MyComponent />
__passive
Same as __capture
, except for prefix (&
)
// original code<div = /> // transpiled code<div />
__once
Same as __capture
, except for prefix (~
)
__captureOnce
Same as __capture
, except for prefix (~!
)
Miscellaneous
Why don't use existing plugins?
There are similar projects.
I uses TypeScript, and try to make Vue+JSX more typesafe by vue-tsx-support.
babel-plugin-jsx-event-modifiers
uses JSX namespaced attribute (like onKeyup:up
),
but TypeScript does not support it.
When using babel-plugin-vue-jsx-sync
, we must specify different attribute name from original definition
(e.g. visible$sync
for visible
),
but it will break type check provided by vue-tsx-support
.
("visible must be boolean"
, "visible must be specified"
, etc)
These are the reason why I wrote another plugin.
Other modifiers?
Some other modifiers (.stop
, .prevent
, .enter
, ...) are available in vue-tsx-support.