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

View File

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

View File

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

View File

@ -289,14 +289,16 @@ function subTransform(
childContext.identifiers = { ...parentContext.identifiers }
// traverse
traverseNode(childRoot, childContext)
// merge helpers/components/directives/imports into parent context
;(['helpers', 'components', 'directives', 'imports'] as const).forEach(
key => {
childContext[key].forEach((value: any) => {
;(parentContext[key] as any).add(value)
})
}
)
// merge helpers/components/directives into parent context
;(['helpers', 'components', 'directives'] as const).forEach(key => {
childContext[key].forEach((value: any) => {
;(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 {