Skip to content

Commit cad25d9

Browse files
committed
fix(compiler-core): hoisted vnode calls and scoped id calls should be marked pure
Otherwise they cannot be tree-shaken
1 parent 6c60ce1 commit cad25d9

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = `
44
"import { createVNode as _createVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\"
5-
const _withId = _withScopeId(\\"test\\")
5+
const _withId = /*#__PURE__*/ _withScopeId(\\"test\\")
66
77
_pushScopeId(\\"test\\")
8-
const _hoisted_1 = _createVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */)
9-
const _hoisted_2 = _createVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */)
8+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */)
9+
const _hoisted_2 = /*#__PURE__*/ _createVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */)
1010
_popScopeId()
1111
12-
export const render = _withId(function render(_ctx, _cache) {
12+
export const render = /*#__PURE__*/ _withId(function render(_ctx, _cache) {
1313
return (_openBlock(), _createBlock(\\"div\\", null, [
1414
_hoisted_1,
1515
_createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */),
@@ -20,9 +20,9 @@ export const render = _withId(function render(_ctx, _cache) {
2020

2121
exports[`scopeId compiler support should wrap default slot 1`] = `
2222
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
23-
const _withId = _withScopeId(\\"test\\")
23+
const _withId = /*#__PURE__*/ _withScopeId(\\"test\\")
2424
25-
export const render = _withId(function render(_ctx, _cache) {
25+
export const render = /*#__PURE__*/ _withId(function render(_ctx, _cache) {
2626
const _component_Child = _resolveComponent(\\"Child\\")
2727
2828
return (_openBlock(), _createBlock(_component_Child, null, {
@@ -36,9 +36,9 @@ export const render = _withId(function render(_ctx, _cache) {
3636

3737
exports[`scopeId compiler support should wrap dynamic slots 1`] = `
3838
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, renderList as _renderList, createSlots as _createSlots, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
39-
const _withId = _withScopeId(\\"test\\")
39+
const _withId = /*#__PURE__*/ _withScopeId(\\"test\\")
4040
41-
export const render = _withId(function render(_ctx, _cache) {
41+
export const render = /*#__PURE__*/ _withId(function render(_ctx, _cache) {
4242
const _component_Child = _resolveComponent(\\"Child\\")
4343
4444
return (_openBlock(), _createBlock(_component_Child, null, _createSlots({ _: 1 }, [
@@ -64,9 +64,9 @@ export const render = _withId(function render(_ctx, _cache) {
6464

6565
exports[`scopeId compiler support should wrap named slots 1`] = `
6666
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createVNode as _createVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
67-
const _withId = _withScopeId(\\"test\\")
67+
const _withId = /*#__PURE__*/ _withScopeId(\\"test\\")
6868
69-
export const render = _withId(function render(_ctx, _cache) {
69+
export const render = /*#__PURE__*/ _withId(function render(_ctx, _cache) {
7070
const _component_Child = _resolveComponent(\\"Child\\")
7171
7272
return (_openBlock(), _createBlock(_component_Child, null, {
@@ -83,9 +83,9 @@ export const render = _withId(function render(_ctx, _cache) {
8383

8484
exports[`scopeId compiler support should wrap render function 1`] = `
8585
"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
86-
const _withId = _withScopeId(\\"test\\")
86+
const _withId = /*#__PURE__*/ _withScopeId(\\"test\\")
8787
88-
export const render = _withId(function render(_ctx, _cache) {
88+
export const render = /*#__PURE__*/ _withId(function render(_ctx, _cache) {
8989
return (_openBlock(), _createBlock(\\"div\\"))
9090
})"
9191
`;

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ describe('scopeId compiler support', () => {
2020
scopeId: 'test'
2121
})
2222
expect(ast.helpers).toContain(WITH_SCOPE_ID)
23-
expect(code).toMatch(`const _withId = _withScopeId("test")`)
24-
expect(code).toMatch(`export const render = _withId(function render(`)
23+
expect(code).toMatch(`const _withId = /*#__PURE__*/ _withScopeId("test")`)
24+
expect(code).toMatch(
25+
`export const render = /*#__PURE__*/ _withId(function render(`
26+
)
2527
expect(code).toMatchSnapshot()
2628
})
2729

@@ -83,10 +85,10 @@ describe('scopeId compiler support', () => {
8385
expect(code).toMatch(
8486
[
8587
`_pushScopeId("test")`,
86-
`const _hoisted_1 = _createVNode("div", null, "hello", ${genFlagText(
88+
`const _hoisted_1 = /*#__PURE__*/ _createVNode("div", null, "hello", ${genFlagText(
8789
PatchFlags.HOISTED
8890
)})`,
89-
`const _hoisted_2 = _createVNode("div", null, "world", ${genFlagText(
91+
`const _hoisted_2 = /*#__PURE__*/ _createVNode("div", null, "world", ${genFlagText(
9092
PatchFlags.HOISTED
9193
)})`,
9294
`_popScopeId()`

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\\" }, null, -1 /* HOISTED */)
7+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"div\\", { key: \\"foo\\" }, null, -1 /* HOISTED */)
88
99
return function render(_ctx, _cache) {
1010
with (_ctx) {
@@ -21,7 +21,7 @@ exports[`compiler: hoistStatic transform hoist nested static tree 1`] = `
2121
"const _Vue = Vue
2222
const { createVNode: _createVNode } = _Vue
2323
24-
const _hoisted_1 = _createVNode(\\"p\\", null, [
24+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"p\\", null, [
2525
_createVNode(\\"span\\"),
2626
_createVNode(\\"span\\")
2727
], -1 /* HOISTED */)
@@ -41,7 +41,7 @@ exports[`compiler: hoistStatic transform hoist nested static tree with comments
4141
"const _Vue = Vue
4242
const { createVNode: _createVNode, createCommentVNode: _createCommentVNode } = _Vue
4343
44-
const _hoisted_1 = _createVNode(\\"div\\", null, [
44+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"div\\", null, [
4545
_createCommentVNode(\\"comment\\")
4646
], -1 /* HOISTED */)
4747
@@ -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\\", null, null, -1 /* HOISTED */)
64-
const _hoisted_2 = _createVNode(\\"div\\", null, null, -1 /* HOISTED */)
63+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"span\\", null, null, -1 /* HOISTED */)
64+
const _hoisted_2 = /*#__PURE__*/ _createVNode(\\"div\\", null, null, -1 /* HOISTED */)
6565
6666
return function render(_ctx, _cache) {
6767
with (_ctx) {
@@ -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\\", -1 /* HOISTED */)
82+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"span\\", { class: \\"inline\\" }, \\"hello\\", -1 /* HOISTED */)
8383
8484
return function render(_ctx, _cache) {
8585
with (_ctx) {
@@ -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), -1 /* HOISTED */)
175+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"span\\", null, \\"foo \\" + _toDisplayString(1) + \\" \\" + _toDisplayString(true), -1 /* HOISTED */)
176176
177177
return function render(_ctx, _cache) {
178178
with (_ctx) {
@@ -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), -1 /* HOISTED */)
192+
const _hoisted_1 = /*#__PURE__*/ _createVNode(\\"span\\", { foo: 0 }, _toDisplayString(1), -1 /* HOISTED */)
193193
194194
return function render(_ctx, _cache) {
195195
with (_ctx) {
@@ -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\\", null, null, -1 /* HOISTED */)
349+
const _hoisted_2 = /*#__PURE__*/ _createVNode(\\"span\\", null, null, -1 /* HOISTED */)
350350
351351
return function render(_ctx, _cache) {
352352
with (_ctx) {
@@ -371,7 +371,7 @@ const _hoisted_1 = {
371371
key: 0,
372372
id: \\"foo\\"
373373
}
374-
const _hoisted_2 = _createVNode(\\"span\\", null, null, -1 /* HOISTED */)
374+
const _hoisted_2 = /*#__PURE__*/ _createVNode(\\"span\\", null, null, -1 /* HOISTED */)
375375
376376
return function render(_ctx, _cache) {
377377
with (_ctx) {

packages/compiler-core/src/codegen.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function generate(
201201

202202
// enter render function
203203
if (genScopeId && !ssr) {
204-
push(`const render = _withId(`)
204+
push(`const render = /*#__PURE__*/ _withId(`)
205205
}
206206
if (!ssr) {
207207
push(`function render(_ctx, _cache) {`)
@@ -400,7 +400,7 @@ function genModulePreamble(
400400
}
401401

402402
if (genScopeId) {
403-
push(`const _withId = ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
403+
push(`const _withId = /*#__PURE__*/ ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
404404
newline()
405405
}
406406

@@ -445,6 +445,13 @@ function genHoists(hoists: JSChildNode[], context: CodegenContext) {
445445

446446
hoists.forEach((exp, i) => {
447447
push(`const _hoisted_${i + 1} = `)
448+
// make hosit function calls tree-shakable
449+
if (
450+
exp.type === NodeTypes.VNODE_CALL ||
451+
exp.type === NodeTypes.JS_CALL_EXPRESSION
452+
) {
453+
push(`/*#__PURE__*/ `)
454+
}
448455
genNode(exp, context)
449456
newline()
450457
})

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function walk(
4545
) {
4646
for (let i = 0; i < children.length; i++) {
4747
const child = children[i]
48-
// only plain elements are eligible for hoisting.
48+
// only plain elements & text calls are eligible for hoisting.
4949
if (
5050
child.type === NodeTypes.ELEMENT &&
5151
child.tagType === ElementTypes.ELEMENT
@@ -79,7 +79,14 @@ function walk(
7979
}
8080
}
8181
}
82+
} else if (
83+
child.type === NodeTypes.TEXT_CALL &&
84+
isStaticNode(child.content, resultCache)
85+
) {
86+
child.codegenNode = context.hoist(child.codegenNode)
8287
}
88+
89+
// walk further
8390
if (child.type === NodeTypes.ELEMENT) {
8491
walk(child.children, context, resultCache)
8592
} else if (child.type === NodeTypes.FOR) {
@@ -91,11 +98,6 @@ function walk(
9198
// Do not hoist v-if single child because it has to be a block
9299
walk(branchChildren, context, resultCache, branchChildren.length === 1)
93100
}
94-
} else if (
95-
child.type === NodeTypes.TEXT_CALL &&
96-
isStaticNode(child.content, resultCache)
97-
) {
98-
child.codegenNode = context.hoist(child.codegenNode)
99101
}
100102
}
101103
}

0 commit comments

Comments
 (0)