From 4f50e691d60c82ca27627e7937cbe872db18869e Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Wed, 12 Jun 2024 10:52:44 +0545 Subject: [PATCH 1/3] fix(ssrTransform): handle arbitrary module namespace identifiers --- .../src/node/ssr/__tests__/ssrTransform.spec.ts | 11 +++++++++++ packages/vite/src/node/ssr/ssrTransform.ts | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 9d1e5403c67845..87a4e904a4f004 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -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 {}`), diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 61849d0e3ca4f2..7fb745e7436da1 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -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}`, ) } @@ -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) } } } From 5e8734cd4e2fe26359d9d2f17d57ded818db2afd Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Wed, 12 Jun 2024 10:54:29 +0545 Subject: [PATCH 2/3] remove test.only --- packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 87a4e904a4f004..7a9c71fa5ffeca 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -120,7 +120,7 @@ test('export * as from', async () => { `) }) -test.only('export as arbitrary module namespace identifier', async () => { +test('export as arbitrary module namespace identifier', async () => { expect( await ssrTransformSimpleCode( `const something = "Something";export { something as "arbitrary string" };`, From f8a689c6930d4de9f66b9085f336fcd523cd6eb8 Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Wed, 12 Jun 2024 12:42:59 +0545 Subject: [PATCH 3/3] handle arbitrary specifier imports --- .../node/ssr/__tests__/ssrTransform.spec.ts | 11 +++++++++ packages/vite/src/node/ssr/ssrTransform.ts | 24 +++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 7a9c71fa5ffeca..1c268835117deb 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -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( diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 7fb745e7436da1..bd99493c1b86a3 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -139,7 +139,11 @@ 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), @@ -147,10 +151,20 @@ async function ssrTransformScript( 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 {