fix(compiler-ssr): avoid duplicated asset imports merged from component slot client branch

fix vitejs/vite#2034
This commit is contained in:
Evan You 2021-02-15 12:12:45 -05:00
parent a238da1082
commit c69f4ea857
4 changed files with 19 additions and 19 deletions

View File

@ -89,7 +89,7 @@ export interface TransformContext
components: Set<string> components: Set<string>
directives: Set<string> directives: Set<string>
hoists: (JSChildNode | null)[] hoists: (JSChildNode | null)[]
imports: Set<ImportItem> imports: ImportItem[]
temps: number temps: number
cached: number cached: number
identifiers: { [name: string]: number | undefined } identifiers: { [name: string]: number | undefined }
@ -163,7 +163,7 @@ export function createTransformContext(
components: new Set(), components: new Set(),
directives: new Set(), directives: new Set(),
hoists: [], hoists: [],
imports: new Set(), imports: [],
constantCache: new Map(), constantCache: new Map(),
temps: 0, temps: 0,
cached: 0, cached: 0,
@ -293,7 +293,7 @@ export function transform(root: RootNode, options: TransformOptions) {
root.helpers = [...context.helpers] root.helpers = [...context.helpers]
root.components = [...context.components] root.components = [...context.components]
root.directives = [...context.directives] root.directives = [...context.directives]
root.imports = [...context.imports] root.imports = context.imports
root.hoists = context.hoists root.hoists = context.hoists
root.temps = context.temps root.temps = context.temps
root.cached = context.cached root.cached = context.cached

View File

@ -153,19 +153,18 @@ function getImportsExpressionExp(
context: TransformContext context: TransformContext
): ExpressionNode { ): ExpressionNode {
if (path) { if (path) {
const importsArray = Array.from(context.imports) const existing = context.imports.find(i => i.path === path)
const existing = importsArray.find(i => i.path === path)
if (existing) { if (existing) {
return existing.exp as ExpressionNode return existing.exp as ExpressionNode
} }
const name = `_imports_${importsArray.length}` const name = `_imports_${context.imports.length}`
const exp = createSimpleExpression( const exp = createSimpleExpression(
name, name,
false, false,
loc, loc,
ConstantTypes.CAN_HOIST ConstantTypes.CAN_HOIST
) )
context.imports.add({ exp, path }) context.imports.push({ exp, path })
if (hash && path) { if (hash && path) {
return context.hoist( return context.hoist(
createSimpleExpression( createSimpleExpression(

View File

@ -99,8 +99,7 @@ export const transformSrcset: NodeTransform = (
const { path } = parseUrl(url) const { path } = parseUrl(url)
let exp: SimpleExpressionNode let exp: SimpleExpressionNode
if (path) { if (path) {
const importsArray = Array.from(context.imports) const existingImportsIndex = context.imports.findIndex(
const existingImportsIndex = importsArray.findIndex(
i => i.path === path i => i.path === path
) )
if (existingImportsIndex > -1) { if (existingImportsIndex > -1) {
@ -112,12 +111,12 @@ export const transformSrcset: NodeTransform = (
) )
} else { } else {
exp = createSimpleExpression( exp = createSimpleExpression(
`_imports_${importsArray.length}`, `_imports_${context.imports.length}`,
false, false,
attr.loc, attr.loc,
ConstantTypes.CAN_HOIST ConstantTypes.CAN_HOIST
) )
context.imports.add({ exp, path }) context.imports.push({ exp, path })
} }
compoundExpression.children.push(exp) compoundExpression.children.push(exp)
} }

View File

@ -289,14 +289,16 @@ function subTransform(
childContext.identifiers = { ...parentContext.identifiers } childContext.identifiers = { ...parentContext.identifiers }
// traverse // traverse
traverseNode(childRoot, childContext) traverseNode(childRoot, childContext)
// merge helpers/components/directives/imports into parent context // merge helpers/components/directives into parent context
;(['helpers', 'components', 'directives', 'imports'] as const).forEach( ;(['helpers', 'components', 'directives'] as const).forEach(key => {
key => {
childContext[key].forEach((value: any) => { childContext[key].forEach((value: any) => {
;(parentContext[key] as any).add(value) ;(parentContext[key] as any).add(value)
}) })
} })
) // imports/hoists are not merged because:
// - imports are only used for asset urls and should be consistent between
// node/client branches
// - hoists are not enabled for the client branch here
} }
function clone(v: any): any { function clone(v: any): any {