Rename babel-preset-jsx to babel-plugin-jsx

This commit is contained in:
Amour1688 2020-05-24 11:33:04 +08:00
parent a118fb1df4
commit 4b954484bf
8 changed files with 105 additions and 115 deletions

View File

@ -1,6 +1,8 @@
{ {
"presets": [ "presets": [
"@babel/env", "@babel/env"
],
"plugins": [
"./src/index.js" "./src/index.js"
] ]
} }

View File

@ -1,20 +1,20 @@
# Babel Preset JSX for Vue 3.0 # Babel Plugin JSX for Vue 3.0
To add Vue JSX support. To add Vue JSX support.
## Installation ## Installation
Install the preset with: Install the plugin with:
``` ```
npm install @ant-design-vue/babel-preset-jsx npm install @ant-design-vue/babel-plugin-jsx
``` ```
Then add the preset to .babelrc: Then add the plugin to .babelrc:
``` ```
{ {
"presets": ["@ant-design-vue/babel-preset-jsx"] "plugins": ["@ant-design-vue/babel-plugin-jsx"]
} }
``` ```

View File

@ -1,7 +1,7 @@
{ {
"name": "@ant-design-vue/babel-preset-jsx", "name": "@ant-design-vue/babel-plugin-jsx",
"version": "1.0.0-alpha.0", "version": "1.0.0-alpha.0",
"description": "Babel preset for Vue 3.0 JSX", "description": "Babel plugin for Vue 3.0 JSX",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
"dev": "webpack-dev-server", "dev": "webpack-dev-server",

View File

@ -1,28 +0,0 @@
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,11 +1,14 @@
const babelSugarVModel = require('./babel-sugar-v-model'); const syntaxJsx = require('@babel/plugin-syntax-jsx').default;
const babelPluginTransformVueJsx = require('./babel-plugin-transform-vue-jsx'); const tranformVueJSX = require('./transform-vue-jsx');
const babelSugarFragment = require('./babel-sugar-fragment'); const sugarVModel = require('./sugar-v-model');
const sugarFragment = require('./sugar-fragment');
module.exports = () => ({ module.exports = () => ({
plugins: [ name: 'babel-plugin-jsx',
babelSugarVModel, inherits: syntaxJsx,
babelPluginTransformVueJsx, visitor: {
babelSugarFragment, ...sugarVModel,
], ...tranformVueJSX,
...sugarFragment,
},
}); });

23
src/sugar-fragment.js Normal file
View File

@ -0,0 +1,23 @@
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 = {
JSXFragment: {
enter(path, state) {
if (!state.vueFragment) {
state.vueFragment = helperModuleImports.addNamed(path, 'Fragment', 'vue');
}
path.replaceWith(transformFragment(path, state.vueFragment));
},
},
};

View File

@ -1,4 +1,3 @@
const syntaxJsx = require('@babel/plugin-syntax-jsx').default;
const t = require('@babel/types'); const t = require('@babel/types');
const htmlTags = require('html-tags'); const htmlTags = require('html-tags');
const svgTags = require('svg-tags'); const svgTags = require('svg-tags');
@ -163,10 +162,7 @@ const parseVModel = (path) => {
}; };
}; };
module.exports = () => ({ module.exports = {
name: 'babel-sugar-v-model',
inherits: syntaxJsx,
visitor: {
JSXAttribute: { JSXAttribute: {
exit(path, state) { exit(path, state) {
const parsed = parseVModel(path); const parsed = parseVModel(path);
@ -205,5 +201,4 @@ module.exports = () => ({
} }
}, },
}, },
}, };
});

View File

@ -1,4 +1,3 @@
const syntaxJsx = require('@babel/plugin-syntax-jsx').default;
const t = require('@babel/types'); const t = require('@babel/types');
const htmlTags = require('html-tags'); const htmlTags = require('html-tags');
const svgTags = require('svg-tags'); const svgTags = require('svg-tags');
@ -310,10 +309,7 @@ const transformJSXElement = (path, injected) => {
]); ]);
}; };
module.exports = () => ({ module.exports = {
name: 'babel-plugin-transform-vue-jsx',
inherits: syntaxJsx,
visitor: {
JSXElement: { JSXElement: {
exit(path, state) { exit(path, state) {
if (!state.vueCreateElementInjected) { if (!state.vueCreateElementInjected) {
@ -342,5 +338,4 @@ module.exports = () => ({
); );
}, },
}, },
}, };
});