Skip to content

Commit c69f4ea

Browse files
committed
fix(compiler-ssr): avoid duplicated asset imports merged from component slot client branch
fix vitejs/vite#2034
1 parent a238da1 commit c69f4ea

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

packages/compiler-core/src/transform.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export interface TransformContext
8989
components: Set<string>
9090
directives: Set<string>
9191
hoists: (JSChildNode | null)[]
92-
imports: Set<ImportItem>
92+
imports: ImportItem[]
9393
temps: number
9494
cached: number
9595
identifiers: { [name: string]: number | undefined }
@@ -163,7 +163,7 @@ export function createTransformContext(
163163
components: new Set(),
164164
directives: new Set(),
165165
hoists: [],
166-
imports: new Set(),
166+
imports: [],
167167
constantCache: new Map(),
168168
temps: 0,
169169
cached: 0,
@@ -293,7 +293,7 @@ export function transform(root: RootNode, options: TransformOptions) {
293293
root.helpers = [...context.helpers]
294294
root.components = [...context.components]
295295
root.directives = [...context.directives]
296-
root.imports = [...context.imports]
296+
root.imports = context.imports
297297
root.hoists = context.hoists
298298
root.temps = context.temps
299299
root.cached = context.cached

packages/compiler-sfc/src/templateTransformAssetUrl.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,18 @@ function getImportsExpressionExp(
153153
context: TransformContext
154154
): ExpressionNode {
155155
if (path) {
156-
const importsArray = Array.from(context.imports)
157-
const existing = importsArray.find(i => i.path === path)
156+
const existing = context.imports.find(i => i.path === path)
158157
if (existing) {
159158
return existing.exp as ExpressionNode
160159
}
161-
const name = `_imports_${importsArray.length}`
160+
const name = `_imports_${context.imports.length}`
162161
const exp = createSimpleExpression(
163162
name,
164163
false,
165164
loc,
166165
ConstantTypes.CAN_HOIST
167166
)
168-
context.imports.add({ exp, path })
167+
context.imports.push({ exp, path })
169168
if (hash && path) {
170169
return context.hoist(
171170
createSimpleExpression(

packages/compiler-sfc/src/templateTransformSrcset.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ export const transformSrcset: NodeTransform = (
9999
const { path } = parseUrl(url)
100100
let exp: SimpleExpressionNode
101101
if (path) {
102-
const importsArray = Array.from(context.imports)
103-
const existingImportsIndex = importsArray.findIndex(
102+
const existingImportsIndex = context.imports.findIndex(
104103
i => i.path === path
105104
)
106105
if (existingImportsIndex > -1) {
@@ -112,12 +111,12 @@ export const transformSrcset: NodeTransform = (
112111
)
113112
} else {
114113
exp = createSimpleExpression(
115-
`_imports_${importsArray.length}`,
114+
`_imports_${context.imports.length}`,
116115
false,
117116
attr.loc,
118117
ConstantTypes.CAN_HOIST
119118
)
120-
context.imports.add({ exp, path })
119+
context.imports.push({ exp, path })
121120
}
122121
compoundExpression.children.push(exp)
123122
}

packages/compiler-ssr/src/transforms/ssrTransformComponent.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,16 @@ function subTransform(
289289
childContext.identifiers = { ...parentContext.identifiers }
290290
// traverse
291291
traverseNode(childRoot, childContext)
292-
// merge helpers/components/directives/imports into parent context
293-
;(['helpers', 'components', 'directives', 'imports'] as const).forEach(
294-
key => {
295-
childContext[key].forEach((value: any) => {
296-
;(parentContext[key] as any).add(value)
297-
})
298-
}
299-
)
292+
// merge helpers/components/directives into parent context
293+
;(['helpers', 'components', 'directives'] as const).forEach(key => {
294+
childContext[key].forEach((value: any) => {
295+
;(parentContext[key] as any).add(value)
296+
})
297+
})
298+
// imports/hoists are not merged because:
299+
// - imports are only used for asset urls and should be consistent between
300+
// node/client branches
301+
// - hoists are not enabled for the client branch here
300302
}
301303

302304
function clone(v: any): any {

0 commit comments

Comments
 (0)