Skip to content

Commit 175f8aa

Browse files
committed
feat(compiler): mark hoisted trees with patchFlag
1 parent d866d38 commit 175f8aa

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = `
55
const _withId = _withScopeId(\\"test\\")
66
77
_pushScopeId(\\"test\\")
8-
const _hoisted_1 = _createVNode(\\"div\\", null, \\"hello\\")
9-
const _hoisted_2 = _createVNode(\\"div\\", null, \\"world\\")
8+
const _hoisted_1 = _createVNode(\\"div\\", null, \\"hello\\", -1)
9+
const _hoisted_2 = _createVNode(\\"div\\", null, \\"world\\", -1)
1010
_popScopeId()
1111
1212
export const render = _withId(function render(_ctx, _cache) {

packages/compiler-core/__tests__/scopeId.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ describe('scopeId compiler support', () => {
8181
expect(code).toMatch(
8282
[
8383
`_pushScopeId("test")`,
84-
`const _hoisted_1 = _createVNode("div", null, "hello")`,
85-
`const _hoisted_2 = _createVNode("div", null, "world")`,
84+
`const _hoisted_1 = _createVNode("div", null, "hello", -1)`,
85+
`const _hoisted_2 = _createVNode("div", null, "world", -1)`,
8686
`_popScopeId()`
8787
].join('\n')
8888
)

packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`compiler: hoistStatic transform hoist element with static key 1`] = `
44
"const _Vue = Vue
55
const { createVNode: _createVNode } = _Vue
66
7-
const _hoisted_1 = _createVNode(\\"div\\", { key: \\"foo\\" })
7+
const _hoisted_1 = _createVNode(\\"div\\", { key: \\"foo\\" }, null, -1)
88
99
return function render(_ctx, _cache) {
1010
with (this) {
@@ -24,7 +24,7 @@ const { createVNode: _createVNode } = _Vue
2424
const _hoisted_1 = _createVNode(\\"p\\", null, [
2525
_createVNode(\\"span\\"),
2626
_createVNode(\\"span\\")
27-
])
27+
], -1)
2828
2929
return function render(_ctx, _cache) {
3030
with (this) {
@@ -43,7 +43,7 @@ const { createVNode: _createVNode, createCommentVNode: _createCommentVNode } = _
4343
4444
const _hoisted_1 = _createVNode(\\"div\\", null, [
4545
_createCommentVNode(\\"comment\\")
46-
])
46+
], -1)
4747
4848
return function render(_ctx, _cache) {
4949
with (this) {
@@ -60,8 +60,8 @@ exports[`compiler: hoistStatic transform hoist siblings with common non-hoistabl
6060
"const _Vue = Vue
6161
const { createVNode: _createVNode } = _Vue
6262
63-
const _hoisted_1 = _createVNode(\\"span\\")
64-
const _hoisted_2 = _createVNode(\\"div\\")
63+
const _hoisted_1 = _createVNode(\\"span\\", null, null, -1)
64+
const _hoisted_2 = _createVNode(\\"div\\", null, null, -1)
6565
6666
return function render(_ctx, _cache) {
6767
with (this) {
@@ -79,7 +79,7 @@ exports[`compiler: hoistStatic transform hoist simple element 1`] = `
7979
"const _Vue = Vue
8080
const { createVNode: _createVNode } = _Vue
8181
82-
const _hoisted_1 = _createVNode(\\"span\\", { class: \\"inline\\" }, \\"hello\\")
82+
const _hoisted_1 = _createVNode(\\"span\\", { class: \\"inline\\" }, \\"hello\\", -1)
8383
8484
return function render(_ctx, _cache) {
8585
with (this) {
@@ -172,7 +172,7 @@ exports[`compiler: hoistStatic transform prefixIdentifiers hoist nested static t
172172
"const _Vue = Vue
173173
const { createVNode: _createVNode } = _Vue
174174
175-
const _hoisted_1 = _createVNode(\\"span\\", null, \\"foo \\" + _toDisplayString(1) + \\" \\" + _toDisplayString(true))
175+
const _hoisted_1 = _createVNode(\\"span\\", null, \\"foo \\" + _toDisplayString(1) + \\" \\" + _toDisplayString(true), -1)
176176
177177
return function render(_ctx, _cache) {
178178
with (this) {
@@ -189,7 +189,7 @@ exports[`compiler: hoistStatic transform prefixIdentifiers hoist nested static t
189189
"const _Vue = Vue
190190
const { createVNode: _createVNode } = _Vue
191191
192-
const _hoisted_1 = _createVNode(\\"span\\", { foo: 0 }, _toDisplayString(1))
192+
const _hoisted_1 = _createVNode(\\"span\\", { foo: 0 }, _toDisplayString(1), -1)
193193
194194
return function render(_ctx, _cache) {
195195
with (this) {
@@ -346,7 +346,7 @@ exports[`compiler: hoistStatic transform should hoist v-for children if static 1
346346
const { createVNode: _createVNode } = _Vue
347347
348348
const _hoisted_1 = { id: \\"foo\\" }
349-
const _hoisted_2 = _createVNode(\\"span\\")
349+
const _hoisted_2 = _createVNode(\\"span\\", null, null, -1)
350350
351351
return function render(_ctx, _cache) {
352352
with (this) {
@@ -371,7 +371,7 @@ const _hoisted_1 = {
371371
key: 0,
372372
id: \\"foo\\"
373373
}
374-
const _hoisted_2 = _createVNode(\\"span\\")
374+
const _hoisted_2 = _createVNode(\\"span\\", null, null, -1)
375375
376376
return function render(_ctx, _cache) {
377377
with (this) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function walk(
5252
) {
5353
if (!doNotHoistNode && isStaticNode(child, resultCache)) {
5454
// whole tree is static
55+
;(child.codegenNode as VNodeCall).patchFlag = PatchFlags.HOISTED + ``
5556
const hoisted = context.transformHoist
5657
? context.transformHoist(child, context)
5758
: child.codegenNode!

packages/runtime-core/src/renderer.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,12 @@ export function createRenderer<
411411
optimized: boolean
412412
) {
413413
let el: HostElement
414-
const { type, props, shapeFlag, transition, scopeId } = vnode
415-
if (vnode.el != null && hostCloneNode !== undefined) {
414+
const { type, props, shapeFlag, transition, scopeId, patchFlag } = vnode
415+
if (
416+
vnode.el !== null &&
417+
hostCloneNode !== undefined &&
418+
patchFlag === PatchFlags.HOISTED
419+
) {
416420
// If a vnode has non-null el, it means it's being reused.
417421
// Only static vnodes can be reused, so its mounted DOM nodes should be
418422
// exactly the same, and we can simply do a clone here.

packages/shared/src/patchFlags.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ export const enum PatchFlags {
6161
// Components with this flag are always force updated.
6262
DYNAMIC_SLOTS = 1 << 9,
6363

64+
// A special flag that indicates a hoisted, static vnode.
65+
HOISTED = -1,
66+
6467
// A special flag that indicates that the diffing algorithm should bail out
6568
// of optimized mode. This is only on block fragments created by renderSlot()
6669
// when encountering non-compiler generated slots (i.e. manually written
6770
// render functions, which should always be fully diffed)
68-
BAIL = -1
71+
BAIL = -2
6972
}
7073

7174
// runtime object for public consumption

0 commit comments

Comments
 (0)