Skip to content

Commit

Permalink
fix(ssrTransform): handle arbitrary module namespace identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
gtm-nayan committed Jun 12, 2024
1 parent ec287f8 commit 4f50e69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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.only('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

0 comments on commit 4f50e69

Please sign in to comment.