Skip to content

Commit

Permalink
fix(compiler-ssr): avoid duplicated asset imports merged from compone…
Browse files Browse the repository at this point in the history
…nt slot client branch

fix vitejs/vite#2034
  • Loading branch information
yyx990803 committed Feb 15, 2021
1 parent a238da1 commit c69f4ea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/compiler-core/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions packages/compiler-sfc/src/templateTransformAssetUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions packages/compiler-sfc/src/templateTransformSrcset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
18 changes: 10 additions & 8 deletions packages/compiler-ssr/src/transforms/ssrTransformComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c69f4ea

Please sign in to comment.