Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): fix transform error due to export all id scope #19331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'`),
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down