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