Skip to content

Commit 0a76652

Browse files
authored
fix(ssrTransform): handle arbitrary module namespace identifiers (#17446)
1 parent f16fae5 commit 0a76652

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ test('named import', async () => {
2727
`)
2828
})
2929

30+
test('named import: arbitrary module namespace specifier', async () => {
31+
expect(
32+
await ssrTransformSimpleCode(
33+
`import { "some thing" as ref } from 'vue';function foo() { return ref(0) }`,
34+
),
35+
).toMatchInlineSnapshot(`
36+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["some thing"]});
37+
function foo() { return __vite_ssr_import_0__["some thing"](0) }"
38+
`)
39+
})
40+
3041
test('namespace import', async () => {
3142
expect(
3243
await ssrTransformSimpleCode(
@@ -120,6 +131,17 @@ test('export * as from', async () => {
120131
`)
121132
})
122133

134+
test('export as arbitrary module namespace identifier', async () => {
135+
expect(
136+
await ssrTransformSimpleCode(
137+
`const something = "Something";export { something as "arbitrary string" };`,
138+
),
139+
).toMatchInlineSnapshot(`
140+
"const something = "Something";
141+
Object.defineProperty(__vite_ssr_exports__, "arbitrary string", { enumerable: true, configurable: true, get(){ return something }});"
142+
`)
143+
})
144+
123145
test('export default', async () => {
124146
expect(
125147
await ssrTransformSimpleCode(`export default {}`),

packages/vite/src/node/ssr/ssrTransform.ts

+34-7
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,32 @@ async function ssrTransformScript(
139139
const importId = defineImport(hoistIndex, node.source.value as string, {
140140
importedNames: node.specifiers
141141
.map((s) => {
142-
if (s.type === 'ImportSpecifier') return s.imported.name
142+
if (s.type === 'ImportSpecifier')
143+
return s.imported.type === 'Identifier'
144+
? s.imported.name
145+
: // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
146+
s.imported.value
143147
else if (s.type === 'ImportDefaultSpecifier') return 'default'
144148
})
145149
.filter(isDefined),
146150
})
147151
s.remove(node.start, node.end)
148152
for (const spec of node.specifiers) {
149153
if (spec.type === 'ImportSpecifier') {
150-
idToImportMap.set(
151-
spec.local.name,
152-
`${importId}.${spec.imported.name}`,
153-
)
154+
if (spec.imported.type === 'Identifier') {
155+
idToImportMap.set(
156+
spec.local.name,
157+
`${importId}.${spec.imported.name}`,
158+
)
159+
} else {
160+
idToImportMap.set(
161+
spec.local.name,
162+
`${importId}[${
163+
// @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
164+
JSON.stringify(spec.imported.value)
165+
}]`,
166+
)
167+
}
154168
} else if (spec.type === 'ImportDefaultSpecifier') {
155169
idToImportMap.set(spec.local.name, `${importId}.default`)
156170
} else {
@@ -194,9 +208,15 @@ async function ssrTransformScript(
194208
},
195209
)
196210
for (const spec of node.specifiers) {
211+
const exportedAs =
212+
spec.exported.type === 'Identifier'
213+
? spec.exported.name
214+
: // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
215+
spec.exported.value
216+
197217
defineExport(
198218
node.start,
199-
spec.exported.name,
219+
exportedAs,
200220
`${importId}.${spec.local.name}`,
201221
)
202222
}
@@ -205,7 +225,14 @@ async function ssrTransformScript(
205225
for (const spec of node.specifiers) {
206226
const local = spec.local.name
207227
const binding = idToImportMap.get(local)
208-
defineExport(node.end, spec.exported.name, binding || local)
228+
229+
const exportedAs =
230+
spec.exported.type === 'Identifier'
231+
? spec.exported.name
232+
: // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
233+
spec.exported.value
234+
235+
defineExport(node.end, exportedAs, binding || local)
209236
}
210237
}
211238
}

0 commit comments

Comments
 (0)