fix(compiler-core): hoisted vnode calls and scoped id calls should be marked pure

Otherwise they cannot be tree-shaken
This commit is contained in:
Evan You
2020-05-01 17:34:11 -04:00
parent 6c60ce13e0
commit cad25d95a3
5 changed files with 45 additions and 34 deletions

View File

@@ -201,7 +201,7 @@ export function generate(
// enter render function
if (genScopeId && !ssr) {
push(`const render = _withId(`)
push(`const render = /*#__PURE__*/ _withId(`)
}
if (!ssr) {
push(`function render(_ctx, _cache) {`)
@@ -400,7 +400,7 @@ function genModulePreamble(
}
if (genScopeId) {
push(`const _withId = ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
push(`const _withId = /*#__PURE__*/ ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
newline()
}
@@ -445,6 +445,13 @@ function genHoists(hoists: JSChildNode[], context: CodegenContext) {
hoists.forEach((exp, i) => {
push(`const _hoisted_${i + 1} = `)
// make hosit function calls tree-shakable
if (
exp.type === NodeTypes.VNODE_CALL ||
exp.type === NodeTypes.JS_CALL_EXPRESSION
) {
push(`/*#__PURE__*/ `)
}
genNode(exp, context)
newline()
})

View File

@@ -45,7 +45,7 @@ function walk(
) {
for (let i = 0; i < children.length; i++) {
const child = children[i]
// only plain elements are eligible for hoisting.
// only plain elements & text calls are eligible for hoisting.
if (
child.type === NodeTypes.ELEMENT &&
child.tagType === ElementTypes.ELEMENT
@@ -79,7 +79,14 @@ function walk(
}
}
}
} else if (
child.type === NodeTypes.TEXT_CALL &&
isStaticNode(child.content, resultCache)
) {
child.codegenNode = context.hoist(child.codegenNode)
}
// walk further
if (child.type === NodeTypes.ELEMENT) {
walk(child.children, context, resultCache)
} else if (child.type === NodeTypes.FOR) {
@@ -91,11 +98,6 @@ function walk(
// Do not hoist v-if single child because it has to be a block
walk(branchChildren, context, resultCache, branchChildren.length === 1)
}
} else if (
child.type === NodeTypes.TEXT_CALL &&
isStaticNode(child.content, resultCache)
) {
child.codegenNode = context.hoist(child.codegenNode)
}
}
}