Skip to content

Commit 6169843

Browse files
committed
chore: improve code
1 parent 82b1184 commit 6169843

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

packages/runtime-core/__tests__/rendererOptimizedMode.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,10 @@ describe('renderer: optimized mode', () => {
767767

768768
show.value = false
769769
await nextTick()
770-
expect(block!.dynamicChildren).toBe(null)
771770
expect(inner(root)).toBe('<div>foo</div>')
772771

773772
show.value = true
774773
await nextTick()
775-
expect(block!.dynamicChildren).toBe(null)
776774
expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
777775
})
778776

packages/runtime-core/src/helpers/renderSlot.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
openBlock,
1212
createBlock,
1313
Fragment,
14-
VNode,
15-
setBlockTracking
14+
VNode
1615
} from '../vnode'
1716
import { PatchFlags, SlotFlags } from '@vue/shared'
1817
import { warn } from '../warning'
@@ -39,7 +38,14 @@ export function renderSlot(
3938
currentRenderingInstance!.parent.isCE)
4039
) {
4140
if (name !== 'default') props.name = name
42-
return createVNode('slot', props, fallback && getFallbackVNode(fallback))
41+
let renderFallback = false
42+
const node = createVNode(
43+
'slot',
44+
props,
45+
fallback && ((renderFallback = true), fallback())
46+
)
47+
if (renderFallback) node.patchFlag = PatchFlags.BAIL
48+
return node
4349
}
4450

4551
let slot = slots[name]
@@ -60,6 +66,7 @@ export function renderSlot(
6066
if (slot && (slot as ContextualRenderFn)._c) {
6167
;(slot as ContextualRenderFn)._d = false
6268
}
69+
let renderFallback = false
6370
openBlock()
6471
const validSlotContent = slot && ensureValidVNode(slot(props))
6572
const rendered = createBlock(
@@ -72,7 +79,7 @@ export function renderSlot(
7279
(validSlotContent && (validSlotContent as any).key) ||
7380
`_${name}`
7481
},
75-
validSlotContent || (fallback ? getFallbackVNode(fallback) : []),
82+
validSlotContent || (fallback ? ((renderFallback = true), fallback()) : []),
7683
validSlotContent && (slots as RawSlots)._ === SlotFlags.STABLE
7784
? PatchFlags.STABLE_FRAGMENT
7885
: PatchFlags.BAIL
@@ -83,16 +90,10 @@ export function renderSlot(
8390
if (slot && (slot as ContextualRenderFn)._c) {
8491
;(slot as ContextualRenderFn)._d = true
8592
}
93+
if (renderFallback) rendered.patchFlag = PatchFlags.BAIL
8694
return rendered
8795
}
8896

89-
function getFallbackVNode(fallback: () => VNodeArrayChildren) {
90-
setBlockTracking(-1)
91-
const vnode = fallback()
92-
setBlockTracking(1)
93-
return vnode
94-
}
95-
9697
function ensureValidVNode(vnodes: VNodeArrayChildren) {
9798
return vnodes.some(child => {
9899
if (!isVNode(child)) return true

packages/runtime-core/src/renderer.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ function baseCreateRenderer(
825825
}
826826

827827
const areChildrenSVG = isSVG && n2.type !== 'foreignObject'
828-
if (dynamicChildren) {
828+
if (optimized && dynamicChildren) {
829829
patchBlockChildren(
830830
n1.dynamicChildren!,
831831
dynamicChildren,
@@ -1058,9 +1058,10 @@ function baseCreateRenderer(
10581058
let { patchFlag, dynamicChildren, slotScopeIds: fragmentSlotScopeIds } = n2
10591059

10601060
if (
1061-
__DEV__ &&
1062-
// #5523 dev root fragment may inherit directives
1063-
(isHmrUpdating || patchFlag & PatchFlags.DEV_ROOT_FRAGMENT)
1061+
(__DEV__ &&
1062+
// #5523 dev root fragment may inherit directives
1063+
(isHmrUpdating || patchFlag & PatchFlags.DEV_ROOT_FRAGMENT)) ||
1064+
(n1 || n2).patchFlag === PatchFlags.BAIL
10641065
) {
10651066
// HMR updated / Dev root fragment (w/ comments), force full diff
10661067
patchFlag = 0
@@ -1093,6 +1094,7 @@ function baseCreateRenderer(
10931094
)
10941095
} else {
10951096
if (
1097+
optimized &&
10961098
patchFlag > 0 &&
10971099
patchFlag & PatchFlags.STABLE_FRAGMENT &&
10981100
dynamicChildren &&

0 commit comments

Comments
 (0)