Skip to content

Commit

Permalink
fix(ssrTransform): handle arbitrary module namespace identifiers (#17446
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gtm-nayan authored Jun 13, 2024
1 parent f16fae5 commit 0a76652
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
22 changes: 22 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ test('named import', async () => {
`)
})

test('named import: arbitrary module namespace specifier', async () => {
expect(
await ssrTransformSimpleCode(
`import { "some thing" as ref } from 'vue';function foo() { return ref(0) }`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["some thing"]});
function foo() { return __vite_ssr_import_0__["some thing"](0) }"
`)
})

test('namespace import', async () => {
expect(
await ssrTransformSimpleCode(
Expand Down Expand Up @@ -120,6 +131,17 @@ test('export * as from', async () => {
`)
})

test('export as arbitrary module namespace identifier', async () => {
expect(
await ssrTransformSimpleCode(
`const something = "Something";export { something as "arbitrary string" };`,
),
).toMatchInlineSnapshot(`
"const something = "Something";
Object.defineProperty(__vite_ssr_exports__, "arbitrary string", { enumerable: true, configurable: true, get(){ return something }});"
`)
})

test('export default', async () => {
expect(
await ssrTransformSimpleCode(`export default {}`),
Expand Down
41 changes: 34 additions & 7 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,32 @@ async function ssrTransformScript(
const importId = defineImport(hoistIndex, node.source.value as string, {
importedNames: node.specifiers
.map((s) => {
if (s.type === 'ImportSpecifier') return s.imported.name
if (s.type === 'ImportSpecifier')
return s.imported.type === 'Identifier'
? s.imported.name
: // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
s.imported.value
else if (s.type === 'ImportDefaultSpecifier') return 'default'
})
.filter(isDefined),
})
s.remove(node.start, node.end)
for (const spec of node.specifiers) {
if (spec.type === 'ImportSpecifier') {
idToImportMap.set(
spec.local.name,
`${importId}.${spec.imported.name}`,
)
if (spec.imported.type === 'Identifier') {
idToImportMap.set(
spec.local.name,
`${importId}.${spec.imported.name}`,
)
} else {
idToImportMap.set(
spec.local.name,
`${importId}[${
// @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
JSON.stringify(spec.imported.value)
}]`,
)
}
} else if (spec.type === 'ImportDefaultSpecifier') {
idToImportMap.set(spec.local.name, `${importId}.default`)
} else {
Expand Down Expand Up @@ -194,9 +208,15 @@ async function ssrTransformScript(
},
)
for (const spec of node.specifiers) {
const exportedAs =
spec.exported.type === 'Identifier'
? spec.exported.name
: // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
spec.exported.value

defineExport(
node.start,
spec.exported.name,
exportedAs,
`${importId}.${spec.local.name}`,
)
}
Expand All @@ -205,7 +225,14 @@ async function ssrTransformScript(
for (const spec of node.specifiers) {
const local = spec.local.name
const binding = idToImportMap.get(local)
defineExport(node.end, spec.exported.name, binding || local)

const exportedAs =
spec.exported.type === 'Identifier'
? spec.exported.name
: // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
spec.exported.value

defineExport(node.end, exportedAs, binding || local)
}
}
}
Expand Down

0 comments on commit 0a76652

Please sign in to comment.