support Fragment

This commit is contained in:
Amour1688
2020-05-17 16:36:38 +08:00
parent 9608e0afca
commit 68da671c5a
6 changed files with 90 additions and 13 deletions

View 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));
},
},
},
});

View File

@ -1,7 +1,9 @@
const babelPluginTransformVueJsx = require('./babel-plugin-transform-vue-jsx');
const babelSugarFragment = require('./babel-sugar-fragment');
module.exports = () => ({
plugins: [
babelPluginTransformVueJsx,
babelSugarFragment,
],
});