@@ -40,159 +40,166 @@ function interopDefault(m: any) {
4040const syntaxJsx = /*#__PURE__*/ interopDefault ( _syntaxJsx ) ;
4141const template = /*#__PURE__*/ interopDefault ( _template ) ;
4242
43- export default declare < VueJSXPluginOptions , BabelCore . PluginObj < State > > (
44- ( api , opt , dirname ) => {
45- const { types } = api ;
46- let resolveType : BabelCore . PluginObj < BabelCore . PluginPass > | undefined ;
47- if ( opt . resolveType ) {
48- if ( typeof opt . resolveType === 'boolean' ) opt . resolveType = { } ;
49- resolveType = ResolveType ( api , opt . resolveType , dirname ) ;
50- }
51- return {
52- ...( resolveType || { } ) ,
53- name : 'babel-plugin-jsx' ,
54- inherits : /*#__PURE__*/ interopDefault ( syntaxJsx ) ,
55- visitor : {
56- ...( resolveType ?. visitor as Visitor < State > ) ,
57- ...transformVueJSX ,
58- ...sugarFragment ,
59- Program : {
60- enter ( path , state ) {
61- if ( hasJSX ( path ) ) {
62- const importNames = [
63- 'createVNode' ,
64- 'Fragment' ,
65- 'resolveComponent' ,
66- 'withDirectives' ,
67- 'vShow' ,
68- 'vModelSelect' ,
69- 'vModelText' ,
70- 'vModelCheckbox' ,
71- 'vModelRadio' ,
72- 'vModelText' ,
73- 'vModelDynamic' ,
74- 'resolveDirective' ,
75- 'mergeProps' ,
76- 'createTextVNode' ,
77- 'isVNode' ,
78- ] ;
79- if ( isModule ( path ) ) {
80- // import { createVNode } from "vue";
81- const importMap : Record <
82- string ,
83- t . MemberExpression | t . Identifier
84- > = { } ;
85- importNames . forEach ( ( name ) => {
86- state . set ( name , ( ) => {
87- if ( importMap [ name ] ) {
88- return types . cloneNode ( importMap [ name ] ) ;
89- }
90- const identifier = addNamed ( path , name , 'vue' , {
91- ensureLiveReference : true ,
92- } ) ;
93- importMap [ name ] = identifier ;
94- return identifier ;
43+ const plugin : (
44+ api : object ,
45+ options : VueJSXPluginOptions | null | undefined ,
46+ dirname : string
47+ ) => BabelCore . PluginObj < State > = declare <
48+ VueJSXPluginOptions ,
49+ BabelCore . PluginObj < State >
50+ > ( ( api , opt , dirname ) => {
51+ const { types } = api ;
52+ let resolveType : BabelCore . PluginObj < BabelCore . PluginPass > | undefined ;
53+ if ( opt . resolveType ) {
54+ if ( typeof opt . resolveType === 'boolean' ) opt . resolveType = { } ;
55+ resolveType = ResolveType ( api , opt . resolveType , dirname ) ;
56+ }
57+ return {
58+ ...( resolveType || { } ) ,
59+ name : 'babel-plugin-jsx' ,
60+ inherits : /*#__PURE__*/ interopDefault ( syntaxJsx ) ,
61+ visitor : {
62+ ...( resolveType ?. visitor as Visitor < State > ) ,
63+ ...transformVueJSX ,
64+ ...sugarFragment ,
65+ Program : {
66+ enter ( path , state ) {
67+ if ( hasJSX ( path ) ) {
68+ const importNames = [
69+ 'createVNode' ,
70+ 'Fragment' ,
71+ 'resolveComponent' ,
72+ 'withDirectives' ,
73+ 'vShow' ,
74+ 'vModelSelect' ,
75+ 'vModelText' ,
76+ 'vModelCheckbox' ,
77+ 'vModelRadio' ,
78+ 'vModelText' ,
79+ 'vModelDynamic' ,
80+ 'resolveDirective' ,
81+ 'mergeProps' ,
82+ 'createTextVNode' ,
83+ 'isVNode' ,
84+ ] ;
85+ if ( isModule ( path ) ) {
86+ // import { createVNode } from "vue";
87+ const importMap : Record <
88+ string ,
89+ t . MemberExpression | t . Identifier
90+ > = { } ;
91+ importNames . forEach ( ( name ) => {
92+ state . set ( name , ( ) => {
93+ if ( importMap [ name ] ) {
94+ return types . cloneNode ( importMap [ name ] ) ;
95+ }
96+ const identifier = addNamed ( path , name , 'vue' , {
97+ ensureLiveReference : true ,
9598 } ) ;
99+ importMap [ name ] = identifier ;
100+ return identifier ;
96101 } ) ;
97- const { enableObjectSlots = true } = state . opts ;
98- if ( enableObjectSlots ) {
99- state . set ( '@vue/babel-plugin-jsx/runtimeIsSlot' , ( ) => {
100- if ( importMap . runtimeIsSlot ) {
101- return importMap . runtimeIsSlot ;
102- }
103- const { name : isVNodeName } = state . get (
104- 'isVNode'
105- ) ( ) as t . Identifier ;
106- const isSlot = path . scope . generateUidIdentifier ( 'isSlot' ) ;
107- const ast = template . ast `
102+ } ) ;
103+ const { enableObjectSlots = true } = state . opts ;
104+ if ( enableObjectSlots ) {
105+ state . set ( '@vue/babel-plugin-jsx/runtimeIsSlot' , ( ) => {
106+ if ( importMap . runtimeIsSlot ) {
107+ return importMap . runtimeIsSlot ;
108+ }
109+ const { name : isVNodeName } = state . get (
110+ 'isVNode'
111+ ) ( ) as t . Identifier ;
112+ const isSlot = path . scope . generateUidIdentifier ( 'isSlot' ) ;
113+ const ast = template . ast `
108114 function ${ isSlot . name } (s) {
109115 return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${ isVNodeName } (s));
110116 }
111117 ` ;
112- const lastImport = ( path . get ( 'body' ) as NodePath [ ] )
113- . filter ( ( p ) => p . isImportDeclaration ( ) )
114- . pop ( ) ;
115- if ( lastImport ) {
116- lastImport . insertAfter ( ast ) ;
117- }
118- importMap . runtimeIsSlot = isSlot ;
119- return isSlot ;
120- } ) ;
121- }
122- } else {
123- // var _vue = require('vue');
124- let sourceName : t . Identifier ;
125- importNames . forEach ( ( name ) => {
126- state . set ( name , ( ) => {
127- if ( ! sourceName ) {
128- sourceName = addNamespace ( path , 'vue' , {
129- ensureLiveReference : true ,
130- } ) ;
131- }
132- return t . memberExpression ( sourceName , t . identifier ( name ) ) ;
133- } ) ;
118+ const lastImport = ( path . get ( 'body' ) as NodePath [ ] )
119+ . filter ( ( p ) => p . isImportDeclaration ( ) )
120+ . pop ( ) ;
121+ if ( lastImport ) {
122+ lastImport . insertAfter ( ast ) ;
123+ }
124+ importMap . runtimeIsSlot = isSlot ;
125+ return isSlot ;
126+ } ) ;
127+ }
128+ } else {
129+ // var _vue = require('vue');
130+ let sourceName : t . Identifier ;
131+ importNames . forEach ( ( name ) => {
132+ state . set ( name , ( ) => {
133+ if ( ! sourceName ) {
134+ sourceName = addNamespace ( path , 'vue' , {
135+ ensureLiveReference : true ,
136+ } ) ;
137+ }
138+ return t . memberExpression ( sourceName , t . identifier ( name ) ) ;
134139 } ) ;
140+ } ) ;
135141
136- const helpers : Record < string , t . Identifier > = { } ;
142+ const helpers : Record < string , t . Identifier > = { } ;
137143
138- const { enableObjectSlots = true } = state . opts ;
139- if ( enableObjectSlots ) {
140- state . set ( '@vue/babel-plugin-jsx/runtimeIsSlot' , ( ) => {
141- if ( helpers . runtimeIsSlot ) {
142- return helpers . runtimeIsSlot ;
143- }
144- const isSlot = path . scope . generateUidIdentifier ( 'isSlot' ) ;
145- const { object : objectName } = state . get (
146- 'isVNode'
147- ) ( ) as t . MemberExpression ;
148- const ast = template . ast `
144+ const { enableObjectSlots = true } = state . opts ;
145+ if ( enableObjectSlots ) {
146+ state . set ( '@vue/babel-plugin-jsx/runtimeIsSlot' , ( ) => {
147+ if ( helpers . runtimeIsSlot ) {
148+ return helpers . runtimeIsSlot ;
149+ }
150+ const isSlot = path . scope . generateUidIdentifier ( 'isSlot' ) ;
151+ const { object : objectName } = state . get (
152+ 'isVNode'
153+ ) ( ) as t . MemberExpression ;
154+ const ast = template . ast `
149155 function ${ isSlot . name } (s) {
150156 return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${
151157 ( objectName as t . Identifier ) . name
152158 } .isVNode(s));
153159 }
154160 ` ;
155161
156- const nodePaths = path . get ( 'body' ) as NodePath [ ] ;
157- const lastImport = nodePaths
158- . filter (
159- ( p ) =>
160- p . isVariableDeclaration ( ) &&
161- p . node . declarations . some (
162- ( d ) =>
163- ( d . id as t . Identifier ) ?. name === sourceName . name
164- )
165- )
166- . pop ( ) ;
167- if ( lastImport ) {
168- lastImport . insertAfter ( ast ) ;
169- }
170- return isSlot ;
171- } ) ;
172- }
162+ const nodePaths = path . get ( 'body' ) as NodePath [ ] ;
163+ const lastImport = nodePaths
164+ . filter (
165+ ( p ) =>
166+ p . isVariableDeclaration ( ) &&
167+ p . node . declarations . some (
168+ ( d ) =>
169+ ( d . id as t . Identifier ) ?. name === sourceName . name
170+ )
171+ )
172+ . pop ( ) ;
173+ if ( lastImport ) {
174+ lastImport . insertAfter ( ast ) ;
175+ }
176+ return isSlot ;
177+ } ) ;
173178 }
179+ }
174180
175- const {
176- opts : { pragma = '' } ,
177- file,
178- } = state ;
181+ const {
182+ opts : { pragma = '' } ,
183+ file,
184+ } = state ;
179185
180- if ( pragma ) {
181- state . set ( 'createVNode' , ( ) => t . identifier ( pragma ) ) ;
182- }
186+ if ( pragma ) {
187+ state . set ( 'createVNode' , ( ) => t . identifier ( pragma ) ) ;
188+ }
183189
184- if ( file . ast . comments ) {
185- for ( const comment of file . ast . comments ) {
186- const jsxMatches = JSX_ANNOTATION_REGEX . exec ( comment . value ) ;
187- if ( jsxMatches ) {
188- state . set ( 'createVNode' , ( ) => t . identifier ( jsxMatches [ 1 ] ) ) ;
189- }
190+ if ( file . ast . comments ) {
191+ for ( const comment of file . ast . comments ) {
192+ const jsxMatches = JSX_ANNOTATION_REGEX . exec ( comment . value ) ;
193+ if ( jsxMatches ) {
194+ state . set ( 'createVNode' , ( ) => t . identifier ( jsxMatches [ 1 ] ) ) ;
190195 }
191196 }
192197 }
193- } ,
198+ }
194199 } ,
195200 } ,
196- } ;
197- }
198- ) ;
201+ } ,
202+ } ;
203+ } ) ;
204+
205+ export default plugin ;
0 commit comments