@@ -9,7 +9,7 @@ import type { Node } from '@babel/types'
99import type { Options } from '../types'
1010import { transformVIf } from './v-if'
1111import { transformVFor } from './v-for'
12- import { isConditionalExpression , isFunctionExpression , isJSXElement , isJSXExpression , isLogicalExpression , isMapCallExpression } from './common'
12+ import { isComponent , isConditionalExpression , isFunctionExpression , isJSXElement , isJSXExpression , isLogicalExpression , isMapCallExpression } from './common'
1313
1414export type RootNodes = {
1515 node : Node
@@ -124,8 +124,20 @@ export function transformVueJsxVapor(
124124 s . removeNode ( node ) ,
125125 )
126126 }
127+ else if ( parent ?. type === 'JSXElement'
128+ && isComponent ( parent . openingElement )
129+ && parent . children . filter ( child => s . sliceNode ( child ) . trim ( ) ) . length === 1
130+ ) {
131+ rootNodes . unshift ( {
132+ node : node . expression ,
133+ isAttributeValue : true ,
134+ } )
135+ s . prepend ( `const _toSlots = s => (Object.prototype.toString.call(s) === '[object Object]' && !s?.__v_isVNode) ? s : { default: typeof s === 'function' ? s: () => s };` )
136+ s . overwrite ( node . start ! , node . expression . start ! , `<template v-for="(slot, slotName) in _toSlots(` )
137+ s . overwrite ( node . expression . end ! , node . end ! , `)" v-slot:[slotName]="scope" :key="slotName"><component :is="slot" v-bind="scope" /></template>` )
138+ }
127139 else if ( ! isJSXExpression ( node . expression ) ) {
128- s . overwrite ( node . start ! , node . start ! + 1 , '<component :is="__createTextVNode (' )
140+ s . overwrite ( node . start ! , node . start ! + 1 , '<component :is="_resolveJSXExpression (' )
129141 s . overwrite ( node . end ! - 1 , node . end ! , ')" />' )
130142
131143 rootNodes . unshift ( {
@@ -158,7 +170,7 @@ export function transformVueJsxVapor(
158170 . replace ( '_cache' , '_cache = []' )
159171 . replaceAll ( / _ c t x \. (? ! \$ s l o t s ) / g, '' )
160172 . replaceAll ( / { " v \d + \- b i n d " : ( [ \s \S ] * ) } / g, '$1' )
161- . replaceAll ( / _ r e s o l v e C o m p o n e n t \( " ( . * ) " \) / g, ( $0 , $1 ) => `(() => { try { return ${ $1 } } catch { return ${ $0 } } })()` )
173+ . replaceAll ( / (?< ! c o n s t ) _ c o m p o n e n t _ ( \w * ) / g, ( $0 , $1 ) => `(() => { try { return ${ $1 . replaceAll ( / (?< = \w ) 4 6 (? = \w ) / g , '.' ) } } catch { return ${ $0 } } })()` )
162174 return runtime === '"vue"' ? `(${ code } )()` : code
163175 }
164176 else {
@@ -184,7 +196,7 @@ export function transformVueJsxVapor(
184196 importSet . add ( 'createTextVNode as _createTextVNode' )
185197 importSet . add ( 'toDisplayString as _toDisplayString' )
186198 s . prepend (
187- `const __createTextVNode = (node) => node?.__v_isVNode || typeof node === 'function' || (Array.isArray(node) && node[0]?.__v_isVNode) ? node : _createTextVNode(_toDisplayString(node));` ,
199+ `const _resolveJSXExpression = (node) => node?.__v_isVNode || typeof node === 'function' || (Array.isArray(node) && node[0]?.__v_isVNode) ? node : _createTextVNode(_toDisplayString(node));` ,
188200 )
189201 }
190202
0 commit comments