mirror of
https://github.com/vuejs/babel-plugin-jsx.git
synced 2025-07-04 02:53:28 +08:00
support Fragment
This commit is contained in:
28
src/babel-sugar-fragment.js
Normal file
28
src/babel-sugar-fragment.js
Normal file
@ -0,0 +1,28 @@
|
||||
const syntaxJsx = require('@babel/plugin-syntax-jsx').default;
|
||||
const t = require('@babel/types');
|
||||
const helperModuleImports = require('@babel/helper-module-imports');
|
||||
|
||||
const transformFragment = (path, { name }) => {
|
||||
const children = path.get('children') || [];
|
||||
return t.jsxElement(
|
||||
t.jsxOpeningElement(t.jsxIdentifier(name), []),
|
||||
t.jsxClosingElement(t.jsxIdentifier(name)),
|
||||
children.map(({ node }) => node),
|
||||
false,
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = () => ({
|
||||
name: 'babel-sugar-fragment',
|
||||
inherits: syntaxJsx,
|
||||
visitor: {
|
||||
JSXFragment: {
|
||||
enter(path, state) {
|
||||
if (!state.vueFragment) {
|
||||
state.vueFragment = helperModuleImports.addNamed(path, 'Fragment', 'vue');
|
||||
}
|
||||
path.replaceWith(transformFragment(path, state.vueFragment));
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
@ -1,7 +1,9 @@
|
||||
const babelPluginTransformVueJsx = require('./babel-plugin-transform-vue-jsx');
|
||||
const babelSugarFragment = require('./babel-sugar-fragment');
|
||||
|
||||
module.exports = () => ({
|
||||
plugins: [
|
||||
babelPluginTransformVueJsx,
|
||||
babelSugarFragment,
|
||||
],
|
||||
});
|
||||
|
Reference in New Issue
Block a user