fix(compiler-sfc): make asset url imports stringifiable

This commit is contained in:
Evan You
2021-12-06 01:19:06 +08:00
parent 3e5ed6c1fc
commit 87c73e99d6
7 changed files with 136 additions and 16 deletions

View File

@@ -38,6 +38,12 @@ export const enum StringifyThresholds {
type StringifiableNode = PlainElementNode | TextCallNode
/**
* Regex for replacing placeholders for embedded constant variables
* (e.g. import URL string constants generated by compiler-sfc)
*/
const expReplaceRE = /__VUE_EXP_START__(.*?)__VUE_EXP_END__/g
/**
* Turn eligible hoisted static trees into stringified static nodes, e.g.
*
@@ -80,7 +86,7 @@ export const stringifyStatic: HoistTransform = (children, context, parent) => {
const staticCall = createCallExpression(context.helper(CREATE_STATIC), [
JSON.stringify(
currentChunk.map(node => stringifyNode(node, context)).join('')
),
).replace(expReplaceRE, `" + $1 + "`),
// the 2nd argument indicates the number of DOM nodes this static vnode
// will insert / hydrate
String(currentChunk.length)
@@ -273,8 +279,17 @@ function stringifyElement(
res += `="${escapeHtml(p.value.content)}"`
}
} else if (p.type === NodeTypes.DIRECTIVE && p.name === 'bind') {
const exp = p.exp as SimpleExpressionNode
if (exp.content[0] === '_') {
// internally generated string constant references
// e.g. imported URL strings via compiler-sfc transformAssetUrl plugin
res += ` ${(p.arg as SimpleExpressionNode).content}="__VUE_EXP_START__${
exp.content
}__VUE_EXP_END__"`
continue
}
// constant v-bind, e.g. :foo="1"
let evaluated = evaluateConstant(p.exp as SimpleExpressionNode)
let evaluated = evaluateConstant(exp)
if (evaluated != null) {
const arg = p.arg && (p.arg as SimpleExpressionNode).content
if (arg === 'class') {