Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssrTransform): handle arbitrary module namespace identifiers #17446

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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
17 changes: 15 additions & 2 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,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 +211,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
Loading