Skip to content

Commit cb2d7c0

Browse files
committed
fix(compiler-core): ensure hoisted scopeId code can be treeshaken
1 parent 54db1eb commit cb2d7c0

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = `
44
"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\"
55
6-
_pushScopeId(\\"test\\")
7-
const _hoisted_1 = /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */)
8-
const _hoisted_2 = /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */)
9-
_popScopeId()
6+
const _withScopeId = n => (_pushScopeId(\\"test\\"),n=n(),_popScopeId(),n)
7+
const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */))
8+
const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */))
109
1110
export function render(_ctx, _cache) {
1211
return (_openBlock(), _createElementBlock(\\"div\\", null, [

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

+9-12
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,15 @@ describe('scopeId compiler support', () => {
7070
expect(ast.helpers).toContain(PUSH_SCOPE_ID)
7171
expect(ast.helpers).toContain(POP_SCOPE_ID)
7272
expect(ast.hoists.length).toBe(2)
73-
expect(code).toMatch(
74-
[
75-
`_pushScopeId("test")`,
76-
`const _hoisted_1 = /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText(
77-
PatchFlags.HOISTED
78-
)})`,
79-
`const _hoisted_2 = /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText(
80-
PatchFlags.HOISTED
81-
)})`,
82-
`_popScopeId()`
83-
].join('\n')
84-
)
73+
;[
74+
`const _withScopeId = n => (_pushScopeId("test"),n=n(),_popScopeId(),n)`,
75+
`const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText(
76+
PatchFlags.HOISTED
77+
)}))`,
78+
`const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText(
79+
PatchFlags.HOISTED
80+
)}))`
81+
].forEach(c => expect(code).toMatch(c))
8582
expect(code).toMatchSnapshot()
8683
})
8784
})

packages/compiler-core/src/codegen.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -470,25 +470,33 @@ function genHoists(hoists: (JSChildNode | null)[], context: CodegenContext) {
470470
const genScopeId = !__BROWSER__ && scopeId != null && mode !== 'function'
471471
newline()
472472

473-
// push scope Id before initializing hoisted vnodes so that these vnodes
474-
// get the proper scopeId as well.
473+
// generate inlined withScopeId helper
475474
if (genScopeId) {
476-
push(`${helper(PUSH_SCOPE_ID)}("${scopeId}")`)
475+
push(
476+
`const _withScopeId = n => (${helper(
477+
PUSH_SCOPE_ID
478+
)}("${scopeId}"),n=n(),${helper(POP_SCOPE_ID)}(),n)`
479+
)
477480
newline()
478481
}
479482

480-
hoists.forEach((exp, i) => {
483+
for (let i = 0; i < hoists.length; i++) {
484+
const exp = hoists[i]
481485
if (exp) {
482-
push(`const _hoisted_${i + 1} = `)
486+
const needScopeIdWrapper = genScopeId && exp.type === NodeTypes.VNODE_CALL
487+
push(
488+
`const _hoisted_${i + 1} = ${
489+
needScopeIdWrapper ? `${PURE_ANNOTATION} _withScopeId(() => ` : ``
490+
}`
491+
)
483492
genNode(exp, context)
493+
if (needScopeIdWrapper) {
494+
push(`)`)
495+
}
484496
newline()
485497
}
486-
})
487-
488-
if (genScopeId) {
489-
push(`${helper(POP_SCOPE_ID)}()`)
490-
newline()
491498
}
499+
492500
context.pure = false
493501
}
494502

0 commit comments

Comments
 (0)