Skip to content

Commit

Permalink
handle arbitrary specifier imports
Browse files Browse the repository at this point in the history
  • Loading branch information
gtm-nayan committed Jun 12, 2024
1 parent 5e8734c commit f8a689c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 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 @@ -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
24 changes: 19 additions & 5 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

0 comments on commit f8a689c

Please sign in to comment.