refactor(compiler): better constant hoist/stringify checks

This commit is contained in:
Evan You
2020-11-20 19:26:07 -05:00
parent acba86ef45
commit 90bdf59f4c
22 changed files with 291 additions and 204 deletions

View File

@@ -1,5 +1,6 @@
import path from 'path'
import {
ConstantTypes,
createSimpleExpression,
ExpressionNode,
NodeTransform,
@@ -159,19 +160,26 @@ function getImportsExpressionExp(
return existing.exp as ExpressionNode
}
const name = `_imports_${importsArray.length}`
const exp = createSimpleExpression(name, false, loc, true)
exp.isRuntimeConstant = true
const exp = createSimpleExpression(
name,
false,
loc,
ConstantTypes.CAN_HOIST
)
context.imports.add({ exp, path })
if (hash && path) {
const ret = context.hoist(
createSimpleExpression(`${name} + '${hash}'`, false, loc, true)
return context.hoist(
createSimpleExpression(
`${name} + '${hash}'`,
false,
loc,
ConstantTypes.CAN_HOIST
)
)
ret.isRuntimeConstant = true
return ret
} else {
return exp
}
} else {
return createSimpleExpression(`''`, false, loc, true)
return createSimpleExpression(`''`, false, loc, ConstantTypes.CAN_HOIST)
}
}

View File

@@ -1,5 +1,6 @@
import path from 'path'
import {
ConstantTypes,
createCompoundExpression,
createSimpleExpression,
NodeTransform,
@@ -107,14 +108,14 @@ export const transformSrcset: NodeTransform = (
`_imports_${existingImportsIndex}`,
false,
attr.loc,
true
ConstantTypes.CAN_HOIST
)
} else {
exp = createSimpleExpression(
`_imports_${importsArray.length}`,
false,
attr.loc,
true
ConstantTypes.CAN_HOIST
)
context.imports.add({ exp, path })
}
@@ -125,7 +126,7 @@ export const transformSrcset: NodeTransform = (
`"${url}"`,
false,
attr.loc,
true
ConstantTypes.CAN_HOIST
)
compoundExpression.children.push(exp)
}
@@ -140,7 +141,7 @@ export const transformSrcset: NodeTransform = (
})
const hoisted = context.hoist(compoundExpression)
hoisted.isRuntimeConstant = true
hoisted.constType = ConstantTypes.CAN_HOIST
node.props[index] = {
type: NodeTypes.DIRECTIVE,