fix(compiler-core): ensure hoisted scopeId code can be treeshaken
This commit is contained in:
parent
54db1eb372
commit
cb2d7c0e3c
@ -3,10 +3,9 @@
|
|||||||
exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = `
|
exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = `
|
||||||
"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\"
|
"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\"
|
||||||
|
|
||||||
_pushScopeId(\\"test\\")
|
const _withScopeId = n => (_pushScopeId(\\"test\\"),n=n(),_popScopeId(),n)
|
||||||
const _hoisted_1 = /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */)
|
const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */))
|
||||||
const _hoisted_2 = /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */)
|
const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */))
|
||||||
_popScopeId()
|
|
||||||
|
|
||||||
export function render(_ctx, _cache) {
|
export function render(_ctx, _cache) {
|
||||||
return (_openBlock(), _createElementBlock(\\"div\\", null, [
|
return (_openBlock(), _createElementBlock(\\"div\\", null, [
|
||||||
|
@ -70,18 +70,15 @@ describe('scopeId compiler support', () => {
|
|||||||
expect(ast.helpers).toContain(PUSH_SCOPE_ID)
|
expect(ast.helpers).toContain(PUSH_SCOPE_ID)
|
||||||
expect(ast.helpers).toContain(POP_SCOPE_ID)
|
expect(ast.helpers).toContain(POP_SCOPE_ID)
|
||||||
expect(ast.hoists.length).toBe(2)
|
expect(ast.hoists.length).toBe(2)
|
||||||
expect(code).toMatch(
|
;[
|
||||||
[
|
`const _withScopeId = n => (_pushScopeId("test"),n=n(),_popScopeId(),n)`,
|
||||||
`_pushScopeId("test")`,
|
`const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText(
|
||||||
`const _hoisted_1 = /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText(
|
PatchFlags.HOISTED
|
||||||
PatchFlags.HOISTED
|
)}))`,
|
||||||
)})`,
|
`const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText(
|
||||||
`const _hoisted_2 = /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText(
|
PatchFlags.HOISTED
|
||||||
PatchFlags.HOISTED
|
)}))`
|
||||||
)})`,
|
].forEach(c => expect(code).toMatch(c))
|
||||||
`_popScopeId()`
|
|
||||||
].join('\n')
|
|
||||||
)
|
|
||||||
expect(code).toMatchSnapshot()
|
expect(code).toMatchSnapshot()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -470,25 +470,33 @@ function genHoists(hoists: (JSChildNode | null)[], context: CodegenContext) {
|
|||||||
const genScopeId = !__BROWSER__ && scopeId != null && mode !== 'function'
|
const genScopeId = !__BROWSER__ && scopeId != null && mode !== 'function'
|
||||||
newline()
|
newline()
|
||||||
|
|
||||||
// push scope Id before initializing hoisted vnodes so that these vnodes
|
// generate inlined withScopeId helper
|
||||||
// get the proper scopeId as well.
|
|
||||||
if (genScopeId) {
|
if (genScopeId) {
|
||||||
push(`${helper(PUSH_SCOPE_ID)}("${scopeId}")`)
|
push(
|
||||||
|
`const _withScopeId = n => (${helper(
|
||||||
|
PUSH_SCOPE_ID
|
||||||
|
)}("${scopeId}"),n=n(),${helper(POP_SCOPE_ID)}(),n)`
|
||||||
|
)
|
||||||
newline()
|
newline()
|
||||||
}
|
}
|
||||||
|
|
||||||
hoists.forEach((exp, i) => {
|
for (let i = 0; i < hoists.length; i++) {
|
||||||
|
const exp = hoists[i]
|
||||||
if (exp) {
|
if (exp) {
|
||||||
push(`const _hoisted_${i + 1} = `)
|
const needScopeIdWrapper = genScopeId && exp.type === NodeTypes.VNODE_CALL
|
||||||
|
push(
|
||||||
|
`const _hoisted_${i + 1} = ${
|
||||||
|
needScopeIdWrapper ? `${PURE_ANNOTATION} _withScopeId(() => ` : ``
|
||||||
|
}`
|
||||||
|
)
|
||||||
genNode(exp, context)
|
genNode(exp, context)
|
||||||
|
if (needScopeIdWrapper) {
|
||||||
|
push(`)`)
|
||||||
|
}
|
||||||
newline()
|
newline()
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
if (genScopeId) {
|
|
||||||
push(`${helper(POP_SCOPE_ID)}()`)
|
|
||||||
newline()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context.pure = false
|
context.pure = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user