mirror of
https://github.com/vuejs/babel-plugin-jsx.git
synced 2024-11-10 17:49:16 +08:00
Rename babel-preset-jsx to babel-plugin-jsx
This commit is contained in:
parent
a118fb1df4
commit
4b954484bf
4
.babelrc
4
.babelrc
@ -1,6 +1,8 @@
|
|||||||
{
|
{
|
||||||
"presets": [
|
"presets": [
|
||||||
"@babel/env",
|
"@babel/env"
|
||||||
|
],
|
||||||
|
"plugins": [
|
||||||
"./src/index.js"
|
"./src/index.js"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
10
README.md
10
README.md
@ -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"]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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",
|
||||||
|
@ -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));
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
19
src/index.js
19
src/index.js
@ -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
23
src/sugar-fragment.js
Normal 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));
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
@ -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,47 +162,43 @@ const parseVModel = (path) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = () => ({
|
module.exports = {
|
||||||
name: 'babel-sugar-v-model',
|
JSXAttribute: {
|
||||||
inherits: syntaxJsx,
|
exit(path, state) {
|
||||||
visitor: {
|
const parsed = parseVModel(path);
|
||||||
JSXAttribute: {
|
if (!parsed) {
|
||||||
exit(path, state) {
|
return;
|
||||||
const parsed = parseVModel(path);
|
}
|
||||||
if (!parsed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { modifiers, value } = parsed;
|
const { modifiers, value } = parsed;
|
||||||
|
|
||||||
const parent = path.parentPath;
|
const parent = path.parentPath;
|
||||||
// v-model={xx} --> v-_model={[directive, xx, void 0, { a: true, b: true }]}
|
// v-model={xx} --> v-_model={[directive, xx, void 0, { a: true, b: true }]}
|
||||||
const directive = getModelDirective(parent, state, value);
|
const directive = getModelDirective(parent, state, value);
|
||||||
if (directive) {
|
if (directive) {
|
||||||
path.replaceWith(
|
path.replaceWith(
|
||||||
t.jsxAttribute(
|
t.jsxAttribute(
|
||||||
t.jsxIdentifier('v-_model'), // TODO
|
t.jsxIdentifier('v-_model'), // TODO
|
||||||
t.jsxExpressionContainer(
|
t.jsxExpressionContainer(
|
||||||
t.arrayExpression([
|
t.arrayExpression([
|
||||||
directive,
|
directive,
|
||||||
value,
|
value,
|
||||||
modifiers.size && t.unaryExpression('void', t.numericLiteral(0), true),
|
modifiers.size && t.unaryExpression('void', t.numericLiteral(0), true),
|
||||||
modifiers.size && t.objectExpression(
|
modifiers.size && t.objectExpression(
|
||||||
[...modifiers].map(
|
[...modifiers].map(
|
||||||
(modifier) => t.objectProperty(
|
(modifier) => t.objectProperty(
|
||||||
t.identifier(modifier),
|
t.identifier(modifier),
|
||||||
t.booleanLiteral(true),
|
t.booleanLiteral(true),
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
].filter(Boolean)),
|
),
|
||||||
),
|
].filter(Boolean)),
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
} else {
|
);
|
||||||
path.remove();
|
} else {
|
||||||
}
|
path.remove();
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
};
|
@ -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,37 +309,33 @@ const transformJSXElement = (path, injected) => {
|
|||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = () => ({
|
module.exports = {
|
||||||
name: 'babel-plugin-transform-vue-jsx',
|
JSXElement: {
|
||||||
inherits: syntaxJsx,
|
exit(path, state) {
|
||||||
visitor: {
|
if (!state.vueCreateElementInjected) {
|
||||||
JSXElement: {
|
state.vueCreateElementInjected = addNamed(path, 'h', 'vue');
|
||||||
exit(path, state) {
|
}
|
||||||
if (!state.vueCreateElementInjected) {
|
if (!state.vueMergePropsInjected) {
|
||||||
state.vueCreateElementInjected = addNamed(path, 'h', 'vue');
|
state.vueMergePropsInjected = addNamed(path, 'mergeProps', 'vue');
|
||||||
}
|
}
|
||||||
if (!state.vueMergePropsInjected) {
|
if (!state.vueWithDirectivesInjected) {
|
||||||
state.vueMergePropsInjected = addNamed(path, 'mergeProps', 'vue');
|
state.vueWithDirectivesInjected = addNamed(path, 'withDirectives', 'vue');
|
||||||
}
|
}
|
||||||
if (!state.vueWithDirectivesInjected) {
|
if (!state.vueResolveDirectiveInjected) {
|
||||||
state.vueWithDirectivesInjected = addNamed(path, 'withDirectives', 'vue');
|
state.vueResolveDirectiveInjected = addNamed(path, 'resolveDirective', 'vue');
|
||||||
}
|
}
|
||||||
if (!state.vueResolveDirectiveInjected) {
|
if (!state.vueVShowInjected) {
|
||||||
state.vueResolveDirectiveInjected = addNamed(path, 'resolveDirective', 'vue');
|
state.vueVShowInjected = addNamed(path, 'vShow', 'vue');
|
||||||
}
|
}
|
||||||
if (!state.vueVShowInjected) {
|
path.replaceWith(
|
||||||
state.vueVShowInjected = addNamed(path, 'vShow', 'vue');
|
transformJSXElement(path, {
|
||||||
}
|
h: state.vueCreateElementInjected,
|
||||||
path.replaceWith(
|
mergeProps: state.vueMergePropsInjected,
|
||||||
transformJSXElement(path, {
|
withDirectives: state.vueWithDirectivesInjected,
|
||||||
h: state.vueCreateElementInjected,
|
resolveDirective: state.vueResolveDirectiveInjected,
|
||||||
mergeProps: state.vueMergePropsInjected,
|
vShow: state.vueVShowInjected,
|
||||||
withDirectives: state.vueWithDirectivesInjected,
|
}),
|
||||||
resolveDirective: state.vueResolveDirectiveInjected,
|
);
|
||||||
vShow: state.vueVShowInjected,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
};
|
Loading…
Reference in New Issue
Block a user