Skip to content

Commit 4d3178e

Browse files
committed
chore : work with filtered nodes instead
node children should not be overwritten to keep comments in the build output
1 parent 0d04f3c commit 4d3178e

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

packages/compiler-core/src/transforms/transformElement.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {
1919
TemplateTextChildNode,
2020
DirectiveArguments,
2121
createVNodeCall,
22-
ConstantTypes,
23-
PlainElementNode
22+
ConstantTypes
2423
} from '../ast'
2524
import {
2625
PatchFlags,
@@ -146,7 +145,8 @@ export const transformElement: NodeTransform = (node, context) => {
146145
}
147146

148147
// children
149-
if (node.children.length > 0) {
148+
if (node.children.length > 0) {
149+
let filterdChildren = [...node.children]
150150
if (vnodeTag === KEEP_ALIVE) {
151151
// Although a built-in component, we compile KeepAlive with raw children
152152
// instead of slot functions so that it can be used inside Transition
@@ -157,12 +157,14 @@ export const transformElement: NodeTransform = (node, context) => {
157157
shouldUseBlock = true
158158
// 2. Force keep-alive to always be updated, since it uses raw children.
159159
patchFlag |= PatchFlags.DYNAMIC_SLOTS
160+
//filter out potential comment nodes
161+
filterdChildren = filterdChildren.filter(c => c.type !== NodeTypes.COMMENT)
160162
// warn if <KeepAlive> has multiple children
161-
if (__DEV__ && hasMultipleChildren(node)) {
163+
if (__DEV__ && filterdChildren.length > 1) {
162164
context.onError(
163165
createCompilerError(ErrorCodes.X_KEEP_ALIVE_INVALID_CHILDREN, {
164-
start: node.children[0].loc.start,
165-
end: node.children[node.children.length - 1].loc.end,
166+
start: filterdChildren[0].loc.start,
167+
end: filterdChildren[filterdChildren.length - 1].loc.end,
166168
source: ''
167169
})
168170
)
@@ -182,8 +184,8 @@ export const transformElement: NodeTransform = (node, context) => {
182184
if (hasDynamicSlots) {
183185
patchFlag |= PatchFlags.DYNAMIC_SLOTS
184186
}
185-
} else if (node.children.length === 1 && vnodeTag !== TELEPORT) {
186-
const child = node.children[0]
187+
} else if (filterdChildren.length === 1 && vnodeTag !== TELEPORT) {
188+
const child = filterdChildren[0]
187189
const type = child.type
188190
// check for dynamic text children
189191
const hasDynamicTextChild =
@@ -929,12 +931,3 @@ function stringifyDynamicPropNames(props: string[]): string {
929931
function isComponentTag(tag: string) {
930932
return tag === 'component' || tag === 'Component'
931933
}
932-
933-
function hasMultipleChildren(node: ComponentNode | PlainElementNode): boolean {
934-
// filter out potential comment nodes.
935-
const children = (node.children = node.children.filter(
936-
c => c.type !== NodeTypes.COMMENT
937-
))
938-
939-
return children.length !== 1
940-
}

packages/runtime-core/src/components/KeepAlive.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
cloneVNode,
1414
isVNode,
1515
VNodeProps,
16-
invokeVNodeHook
16+
invokeVNodeHook,
17+
Comment
1718
} from '../vnode'
18-
import { warn } from '../warning'
1919
import {
2020
onBeforeUnmount,
2121
injectHook,
@@ -249,13 +249,21 @@ const KeepAliveImpl: ComponentOptions = {
249249
}
250250

251251
const children = slots.default()
252-
const rawVNode = children[0]
252+
let rawVNode = children[0]
253253
if (children.length > 1) {
254-
if (__DEV__) {
255-
warn(`KeepAlive should contain exactly one component child.`)
254+
let hasFound = false
255+
// locate first non-comment child
256+
for (const c of children) {
257+
if (c.type !== Comment) {
258+
rawVNode = c
259+
hasFound = true
260+
break
261+
}
262+
}
263+
if(!hasFound){
264+
current = null
265+
return children
256266
}
257-
current = null
258-
return children
259267
} else if (
260268
!isVNode(rawVNode) ||
261269
(!(rawVNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) &&

0 commit comments

Comments
 (0)