Skip to content

Commit e28bce2

Browse files
authored
fix(ssr): fix transform error due to export all id scope (#19331)
1 parent f19ffbc commit e28bce2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,44 @@ test('export * as from', async () => {
127127
`)
128128
})
129129

130+
test('re-export by imported name', async () => {
131+
expect(
132+
await ssrTransformSimpleCode(`\
133+
import * as foo from 'foo'
134+
export * as foo from 'foo'
135+
`),
136+
).toMatchInlineSnapshot(`
137+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo");
138+
const __vite_ssr_import_1__ = await __vite_ssr_import__("foo");
139+
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__ }});
140+
"
141+
`)
142+
143+
expect(
144+
await ssrTransformSimpleCode(`\
145+
import { foo } from 'foo'
146+
export { foo } from 'foo'
147+
`),
148+
).toMatchInlineSnapshot(`
149+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]});
150+
const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]});
151+
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__.foo }});
152+
"
153+
`)
154+
155+
expect(
156+
await ssrTransformSimpleCode(`\
157+
import { foo } from 'foo'
158+
export { foo as foo } from 'foo'
159+
`),
160+
).toMatchInlineSnapshot(`
161+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]});
162+
const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]});
163+
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__.foo }});
164+
"
165+
`)
166+
})
167+
130168
test('export * as from arbitrary module namespace identifier', async () => {
131169
expect(
132170
await ssrTransformSimpleCode(`export * as "arbitrary string" from 'vue'`),

packages/vite/src/node/ssr/ssrTransform.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,12 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {
719719
return false
720720
}
721721

722-
if (parent.type === 'ExportSpecifier') {
722+
// export { id } from "lib"
723+
// export * as id from "lib"
724+
if (
725+
parent.type === 'ExportSpecifier' ||
726+
parent.type === 'ExportAllDeclaration'
727+
) {
723728
return false
724729
}
725730

0 commit comments

Comments
 (0)