Skip to content

Commit

Permalink
fix(hmr): deep clone reused hoisted trees during dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 15, 2021
1 parent c69f4ea commit 5a7a1b8
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export function cloneVNode<T, U>(
): VNode<T, U> {
// This is intentionally NOT using spread or extend to avoid the runtime
// key enumeration cost.
const { props, ref, patchFlag } = vnode
const { props, ref, patchFlag, children } = vnode
const mergedProps = extraProps ? mergeProps(props || {}, extraProps) : props
return {
__v_isVNode: true,
Expand All @@ -479,7 +479,10 @@ export function cloneVNode<T, U>(
: normalizeRef(extraProps)
: ref,
scopeId: vnode.scopeId,
children: vnode.children,
children:
__DEV__ && patchFlag === PatchFlags.HOISTED && isArray(children)
? (children as VNode[]).map(deepCloneVNode)
: children,
target: vnode.target,
targetAnchor: vnode.targetAnchor,
staticCount: vnode.staticCount,
Expand Down Expand Up @@ -513,6 +516,18 @@ export function cloneVNode<T, U>(
}
}

/**
* Dev only, for HMR of hoisted vnodes reused in v-for
* https://github.com/vitejs/vite/issues/2022
*/
function deepCloneVNode(vnode: VNode): VNode {
const cloned = cloneVNode(vnode)
if (isArray(vnode.children)) {
cloned.children = (vnode.children as VNode[]).map(deepCloneVNode)
}
return cloned
}

/**
* @private
*/
Expand Down

0 comments on commit 5a7a1b8

Please sign in to comment.