mirror of
https://github.com/vuejs/babel-plugin-jsx.git
synced 2024-11-10 09:39:14 +08:00
refactor: improve types
This commit is contained in:
parent
86ba932a03
commit
768fb3ec2a
@ -6,7 +6,7 @@
|
||||
"test": "vitest",
|
||||
"lint": "eslint --cache .",
|
||||
"format": "prettier --write .",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"typecheck": "tsc",
|
||||
"release": "bumpp -r"
|
||||
},
|
||||
"license": "MIT",
|
||||
|
@ -1,2 +0,0 @@
|
||||
<div id="app"></div>
|
||||
<script src="/dist/main.js"></script>
|
@ -31,14 +31,14 @@ const hasJSX = (parentPath: NodePath<t.Program>) => {
|
||||
|
||||
const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
|
||||
|
||||
export default ({ types }: typeof BabelCore) => ({
|
||||
export default ({ types }: typeof BabelCore): BabelCore.PluginObj<State> => ({
|
||||
name: 'babel-plugin-jsx',
|
||||
inherits: syntaxJsx,
|
||||
visitor: {
|
||||
...transformVueJSX,
|
||||
...sugarFragment,
|
||||
Program: {
|
||||
enter(path: NodePath<t.Program>, state: State) {
|
||||
enter(path, state) {
|
||||
if (hasJSX(path)) {
|
||||
const importNames = [
|
||||
'createVNode',
|
||||
@ -168,7 +168,7 @@ export default ({ types }: typeof BabelCore) => ({
|
||||
}
|
||||
}
|
||||
},
|
||||
exit(path: NodePath<t.Program>) {
|
||||
exit(path) {
|
||||
const body = path.get('body') as NodePath[];
|
||||
const specifiersMap = new Map<string, t.ImportSpecifier>();
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
import * as t from '@babel/types';
|
||||
import { type NodePath } from '@babel/traverse';
|
||||
import { type NodePath, type Visitor } from '@babel/traverse';
|
||||
import type { State } from './interface';
|
||||
import { FRAGMENT, createIdentifier } from './utils';
|
||||
|
||||
const transformFragment = (
|
||||
path: NodePath<t.JSXElement>,
|
||||
path: NodePath<t.JSXFragment>,
|
||||
Fragment: t.JSXIdentifier | t.JSXMemberExpression
|
||||
) => {
|
||||
const children = path.get('children') || [];
|
||||
@ -16,9 +16,9 @@ const transformFragment = (
|
||||
);
|
||||
};
|
||||
|
||||
export default {
|
||||
const visitor: Visitor<State> = {
|
||||
JSXFragment: {
|
||||
enter(path: NodePath<t.JSXElement>, state: State) {
|
||||
enter(path, state) {
|
||||
const fragmentCallee = createIdentifier(state, FRAGMENT);
|
||||
path.replaceWith(
|
||||
transformFragment(
|
||||
@ -34,3 +34,5 @@ export default {
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default visitor;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as t from '@babel/types';
|
||||
import { type NodePath } from '@babel/traverse';
|
||||
import { type NodePath, type Visitor } from '@babel/traverse';
|
||||
// @ts-expect-error
|
||||
import { addDefault } from '@babel/helper-module-imports';
|
||||
import {
|
||||
@ -559,10 +559,12 @@ const transformJSXElement = (
|
||||
]);
|
||||
};
|
||||
|
||||
export default {
|
||||
const visitor: Visitor<State> = {
|
||||
JSXElement: {
|
||||
exit(path: NodePath<t.JSXElement>, state: State) {
|
||||
exit(path, state) {
|
||||
path.replaceWith(transformJSXElement(path, state));
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default visitor;
|
||||
|
@ -1,6 +1,5 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"sourceMap": true,
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"lib": ["ES2015", "DOM", "DOM.Iterable"],
|
||||
@ -10,13 +9,14 @@
|
||||
"noUnusedLocals": true,
|
||||
"resolveJsonModule": true,
|
||||
"esModuleInterop": true,
|
||||
"removeComments": false,
|
||||
"jsx": "preserve",
|
||||
"types": ["vitest/globals"],
|
||||
"skipLibCheck": true,
|
||||
"paths": {
|
||||
"@vue/babel-plugin-jsx": ["./packages/babel-plugin-jsx/src"]
|
||||
}
|
||||
},
|
||||
"noEmit": true,
|
||||
"incremental": true
|
||||
},
|
||||
"include": ["packages/*/src", "packages/*/test"]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user