@@ -9,38 +9,38 @@ import type { Node } from '@babel/types'
99import type { Options } from '../types'
1010import { transformVIf } from './v-if'
1111import { transformVFor } from './v-for'
12- import { getReturnExpression , isConditionalExpression , isJSXElement , isJSXExpression , isLogicalExpression , isMapCallExpression } from './common'
12+ import { isConditionalExpression , isJSXElement , isJSXExpression , isLogicalExpression , isMapCallExpression } from './common'
13+
14+ export type RootNodes = {
15+ node : Node
16+ postCallbacks ?: ( ( ( ) => void ) | undefined ) [ ]
17+ isAttributeValue ?: boolean
18+ } [ ]
1319
1420export function transformVueJsxVapor (
1521 code : string ,
1622 id : string ,
1723 options : Options ,
1824) {
1925 const s = new MagicString ( code )
20- const exclude : Node [ ] = [ ]
2126 let hasTextNode = false
22- const rootNodes : {
23- node : Node
24- postCallbacks : ( ( ( ) => void ) | undefined ) [ ]
25- isAttributeValue ?: boolean
26- } [ ] = [ ]
27+ const rootNodes : RootNodes = [ ]
2728 let postCallbacks : ( ( ( ) => void ) | undefined ) [ ] = [ ]
2829 walkAST < Node > ( babelParse ( code , getLang ( id ) ) , {
2930 enter ( node , parent ) {
3031 if (
3132 parent ?. type !== 'JSXExpressionContainer'
3233 && ! isJSXExpression ( parent )
3334 && isJSXExpression ( node )
34- && ! exclude . includes ( node )
3535 ) {
3636 rootNodes . unshift ( {
3737 node,
3838 postCallbacks : [ ] ,
3939 } )
40- postCallbacks = rootNodes [ 0 ] . postCallbacks
40+ postCallbacks = rootNodes [ 0 ] . postCallbacks !
4141 }
4242 else if (
43- ( parent ?. type === 'JSXAttribute' )
43+ parent ?. type === 'JSXAttribute'
4444 && node . type === 'JSXExpressionContainer'
4545 && / ( " | < .* ?\/ .* ?> ) / . test ( s . sliceNode ( node . expression ) )
4646 ) {
@@ -49,7 +49,7 @@ export function transformVueJsxVapor(
4949 postCallbacks : [ ] ,
5050 isAttributeValue : true ,
5151 } )
52- postCallbacks = rootNodes [ 0 ] . postCallbacks
52+ postCallbacks = rootNodes [ 0 ] . postCallbacks !
5353 }
5454
5555 if (
@@ -102,9 +102,8 @@ export function transformVueJsxVapor(
102102 isMapCallExpression ( node )
103103 ) {
104104 postCallbacks . unshift (
105- transformVFor ( node , parent , s ) ,
105+ transformVFor ( node , parent , rootNodes , s ) ,
106106 )
107- exclude . unshift ( getReturnExpression ( node . arguments [ 0 ] ) ! )
108107 }
109108 else if (
110109 isConditionalExpression ( node )
@@ -126,12 +125,12 @@ export function transformVueJsxVapor(
126125 else if ( ! isJSXExpression ( node . expression ) ) {
127126 s . overwrite ( node . start ! , node . start ! + 1 , '<component :is="__createTextVNode(' )
128127 s . overwrite ( node . end ! - 1 , node . end ! , ')" />' )
128+
129129 if (
130130 / ( " | < .* ?\/ .* ?> ) / . test ( s . sliceNode ( node . expression ) )
131131 ) {
132132 rootNodes . unshift ( {
133133 node : node . expression ,
134- postCallbacks : [ ] ,
135134 isAttributeValue : true ,
136135 } )
137136 }
@@ -171,9 +170,10 @@ export function transformVueJsxVapor(
171170 return content
172171 }
173172 }
173+
174174 const placeholders : string [ ] = [ ]
175175 for ( const { node, postCallbacks, isAttributeValue } of rootNodes ) {
176- postCallbacks . forEach ( callback => callback ?.( ) )
176+ postCallbacks ? .forEach ( callback => callback ?.( ) )
177177
178178 const result = compile ( node )
179179 . replaceAll ( / _ _ P L A C E H O L D E R _ ( \d ) / g, ( _ , $1 ) => placeholders [ $1 ] )
@@ -189,7 +189,7 @@ export function transformVueJsxVapor(
189189 importSet . add ( 'createTextVNode as _createTextVNode' )
190190 importSet . add ( 'toDisplayString as _toDisplayString' )
191191 s . prepend (
192- `const __createTextVNode = (node) => node?.__v_isVNode || typeof node ==='function' ? node: _createTextVNode(_toDisplayString(node));` ,
192+ `const __createTextVNode = (node) => node?.__v_isVNode || typeof node === 'function' || (Array.isArray(node) && node[0]?.__v_isVNode) ? node : _createTextVNode(_toDisplayString(node));` ,
193193 )
194194 }
195195
0 commit comments