Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): set single root correctly in DEV mode to avoid redundant patches #2173

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")

return (_openBlock(), _createBlock(\\"div\\", null, [
_createVNode(\\"div\\", _hoisted_1, [
(_openBlock(), _createBlock(\\"div\\", _hoisted_1, [
_createVNode(_component_Comp)
])
]))
]))
}
}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,24 @@ describe('compiler: element transform', () => {
isBlock: true
})
})

// #2169
test('elements with components child should be forced into blocks', () => {
const ast = parse(`<div><section><h1><Comp/></h1></section></div>`)
transform(ast, {
nodeTransforms: [transformElement]
})
expect((ast as any).children[0].children[0].codegenNode).toMatchObject({
type: NodeTypes.VNODE_CALL,
tag: `"section"`,
isBlock: false
})
expect(
(ast as any).children[0].children[0].children[0].codegenNode
).toMatchObject({
type: NodeTypes.VNODE_CALL,
tag: `"h1"`,
isBlock: true
})
})
})
27 changes: 25 additions & 2 deletions packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
VNodeCall,
TemplateTextChildNode,
DirectiveArguments,
createVNodeCall
createVNodeCall,
PlainElementNode
} from '../ast'
import {
PatchFlags,
Expand Down Expand Up @@ -101,7 +102,9 @@ export const transformElement: NodeTransform = (node, context) => {
(tag === 'svg' ||
tag === 'foreignObject' ||
// #938: elements with dynamic keys should be forced into blocks
findProp(node, 'key', true)))
findProp(node, 'key', true) ||
// #2169: elements with components child should be forced into blocks
hasComponentChild(node as PlainElementNode)))

// props
if (props.length > 0) {
Expand Down Expand Up @@ -590,3 +593,23 @@ function stringifyDynamicPropNames(props: string[]): string {
}
return propsNamesString + `]`
}

function hasComponentChild(node: PlainElementNode): boolean {
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i]
if (
child.type === NodeTypes.ELEMENT &&
child.codegenNode &&
child.codegenNode.type === NodeTypes.VNODE_CALL &&
!child.codegenNode.isBlock
) {
switch (child.tagType) {
case ElementTypes.ELEMENT:
return hasComponentChild(child)
case ElementTypes.COMPONENT:
return true
}
}
}
return false
}
4 changes: 4 additions & 0 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ const getChildRoot = (
if (dynamicIndex > -1) {
dynamicChildren[dynamicIndex] = updatedRoot
} else if (dynamicChildren && updatedRoot.patchFlag > 0) {
// fix: #2169
// since we can guarantee that it is a single root,
// we can directly use it to replace the dynamicChildren to avoid redundant patches.
dynamicChildren.length = 0
dynamicChildren.push(updatedRoot)
}
}
Expand Down