diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 226e0ef176278e..88ab290c391904 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -126,6 +126,44 @@ test('export * as from', async () => { `) }) +test('re-export by imported name', async () => { + expect( + await ssrTransformSimpleCode(`\ +import * as foo from 'foo' +export * as foo from 'foo' +`), + ).toMatchInlineSnapshot(` + "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo"); + const __vite_ssr_import_1__ = await __vite_ssr_import__("foo"); + Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__ }}); + " + `) + + expect( + await ssrTransformSimpleCode(`\ +import { foo } from 'foo' +export { foo } from 'foo' +`), + ).toMatchInlineSnapshot(` + "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]}); + const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]}); + Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__.foo }}); + " + `) + + expect( + await ssrTransformSimpleCode(`\ +import { foo } from 'foo' +export { foo as foo } from 'foo' +`), + ).toMatchInlineSnapshot(` + "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]}); + const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]}); + Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__.foo }}); + " + `) +}) + test('export * as from arbitrary module namespace identifier', async () => { expect( await ssrTransformSimpleCode(`export * as "arbitrary string" from 'vue'`), diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 8c44859bc49129..cd1cc22c513d7a 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -714,7 +714,12 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) { return false } - if (parent.type === 'ExportSpecifier') { + // export { id } from "lib" + // export * as id from "lib" + if ( + parent.type === 'ExportSpecifier' || + parent.type === 'ExportAllDeclaration' + ) { return false }