Skip to content

Commit 23ee154

Browse files
committed
fix: v-slot & v-for co-usage
1 parent a9b487d commit 23ee154

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

playground/virtual-dom/for.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ defineRender(
1919
<div>
2020
{count.value}
2121
</div>
22-
{
23-
Array.from({ length: 3 }).map((_, index) => (
24-
<span>
25-
{index}
26-
</span>
27-
))
28-
}
29-
{' '}
22+
<Comp>
23+
{
24+
Array.from({ length: 3 }).map((_, index) => (
25+
<span>
26+
{index}
27+
</span>
28+
))
29+
}
30+
</Comp>
3031
{
3132
Array.from({ length: 3 }).map((_, index) => {
3233
const idx = index + 1

src/core/transform.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function transformVueJsxVapor(
2424
) {
2525
const s = new MagicString(code)
2626
let hasTextNode = false
27+
let hasSlots = false
2728
const rootNodes: RootNodes = []
2829
let postCallbacks: ((() => void) | undefined)[] = []
2930
walkAST<Node>(babelParse(code, getLang(id)), {
@@ -124,12 +125,15 @@ export function transformVueJsxVapor(
124125
else if (parent?.type === 'JSXElement'
125126
&& isComponent(parent.openingElement)
126127
&& parent.children.filter(child => s.sliceNode(child).trim()).length === 1
128+
&& !(isMapCallExpression(node.expression)
129+
|| isConditionalExpression(node)
130+
|| isLogicalExpression(node))
127131
) {
128132
rootNodes.unshift({
129133
node: node.expression,
130134
isAttributeValue: true,
131135
})
132-
s.prepend(`const _toSlots = s => (Object.prototype.toString.call(s) === '[object Object]' && !s?.__v_isVNode) ? s : { default: typeof s === 'function' ? s: () => s };`)
136+
hasSlots = true
133137
s.overwrite(node.start!, node.expression.start!, `<template v-for="(slot, slotName) in _toSlots(`)
134138
s.overwrite(node.expression.end!, node.end!, `)" v-slot:[slotName]="scope" :key="slotName"><component :is="slot" v-bind="scope" /></template>`)
135139
}
@@ -197,6 +201,10 @@ export function transformVueJsxVapor(
197201
)
198202
}
199203

204+
if (hasSlots) {
205+
s.prepend(`const _toSlots = s => (Object.prototype.toString.call(s) === '[object Object]' && !s?.__v_isVNode) ? s : { default: typeof s === 'function' ? s: () => s };`)
206+
}
207+
200208
s.prepend(
201209
`import { ${Array.from(importSet).join(', ')} } from ${runtime};`,
202210
)

0 commit comments

Comments
 (0)