@@ -9,38 +9,38 @@ import type { Node } from '@babel/types'
9
9
import type { Options } from '../types'
10
10
import { transformVIf } from './v-if'
11
11
import { 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
+ } [ ]
13
19
14
20
export function transformVueJsxVapor (
15
21
code : string ,
16
22
id : string ,
17
23
options : Options ,
18
24
) {
19
25
const s = new MagicString ( code )
20
- const exclude : Node [ ] = [ ]
21
26
let hasTextNode = false
22
- const rootNodes : {
23
- node : Node
24
- postCallbacks : ( ( ( ) => void ) | undefined ) [ ]
25
- isAttributeValue ?: boolean
26
- } [ ] = [ ]
27
+ const rootNodes : RootNodes = [ ]
27
28
let postCallbacks : ( ( ( ) => void ) | undefined ) [ ] = [ ]
28
29
walkAST < Node > ( babelParse ( code , getLang ( id ) ) , {
29
30
enter ( node , parent ) {
30
31
if (
31
32
parent ?. type !== 'JSXExpressionContainer'
32
33
&& ! isJSXExpression ( parent )
33
34
&& isJSXExpression ( node )
34
- && ! exclude . includes ( node )
35
35
) {
36
36
rootNodes . unshift ( {
37
37
node,
38
38
postCallbacks : [ ] ,
39
39
} )
40
- postCallbacks = rootNodes [ 0 ] . postCallbacks
40
+ postCallbacks = rootNodes [ 0 ] . postCallbacks !
41
41
}
42
42
else if (
43
- ( parent ?. type === 'JSXAttribute' )
43
+ parent ?. type === 'JSXAttribute'
44
44
&& node . type === 'JSXExpressionContainer'
45
45
&& / ( " | < .* ?\/ .* ?> ) / . test ( s . sliceNode ( node . expression ) )
46
46
) {
@@ -49,7 +49,7 @@ export function transformVueJsxVapor(
49
49
postCallbacks : [ ] ,
50
50
isAttributeValue : true ,
51
51
} )
52
- postCallbacks = rootNodes [ 0 ] . postCallbacks
52
+ postCallbacks = rootNodes [ 0 ] . postCallbacks !
53
53
}
54
54
55
55
if (
@@ -102,9 +102,8 @@ export function transformVueJsxVapor(
102
102
isMapCallExpression ( node )
103
103
) {
104
104
postCallbacks . unshift (
105
- transformVFor ( node , parent , s ) ,
105
+ transformVFor ( node , parent , rootNodes , s ) ,
106
106
)
107
- exclude . unshift ( getReturnExpression ( node . arguments [ 0 ] ) ! )
108
107
}
109
108
else if (
110
109
isConditionalExpression ( node )
@@ -126,12 +125,12 @@ export function transformVueJsxVapor(
126
125
else if ( ! isJSXExpression ( node . expression ) ) {
127
126
s . overwrite ( node . start ! , node . start ! + 1 , '<component :is="__createTextVNode(' )
128
127
s . overwrite ( node . end ! - 1 , node . end ! , ')" />' )
128
+
129
129
if (
130
130
/ ( " | < .* ?\/ .* ?> ) / . test ( s . sliceNode ( node . expression ) )
131
131
) {
132
132
rootNodes . unshift ( {
133
133
node : node . expression ,
134
- postCallbacks : [ ] ,
135
134
isAttributeValue : true ,
136
135
} )
137
136
}
@@ -171,9 +170,10 @@ export function transformVueJsxVapor(
171
170
return content
172
171
}
173
172
}
173
+
174
174
const placeholders : string [ ] = [ ]
175
175
for ( const { node, postCallbacks, isAttributeValue } of rootNodes ) {
176
- postCallbacks . forEach ( callback => callback ?.( ) )
176
+ postCallbacks ? .forEach ( callback => callback ?.( ) )
177
177
178
178
const result = compile ( node )
179
179
. replaceAll ( / _ _ P L A C E H O L D E R _ ( \d ) / g, ( _ , $1 ) => placeholders [ $1 ] )
@@ -189,7 +189,7 @@ export function transformVueJsxVapor(
189
189
importSet . add ( 'createTextVNode as _createTextVNode' )
190
190
importSet . add ( 'toDisplayString as _toDisplayString' )
191
191
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));` ,
193
193
)
194
194
}
195
195
0 commit comments