Skip to content

Commit

Permalink
fix(ssr): named export should overwrite export all (#19534)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Mar 2, 2025
1 parent cb9165c commit 2fd2fc1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ function exportAll(exports: any, sourceModule: any) {
return

for (const key in sourceModule) {
if (key !== 'default' && key !== '__esModule') {
if (key !== 'default' && key !== '__esModule' && !(key in exports)) {
try {
Object.defineProperty(exports, key, {
enumerable: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const a = 'dep1-a'
export const b = 'dep1-b'
export const c = 'dep1-c'
export const d = 'dep1-d'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const d = 'dep2-d'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const a = 'main-a'
export * from './dep1.js'
export const c = 'main-c'
export * from './dep2.js'
23 changes: 23 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,26 @@ test('plugin error', async () => {
Plugin: test-plugin"
`)
})

test('named exports overwrite export all', async () => {
const server = await createDevServer()
const mod = await server.ssrLoadModule(
'./fixtures/named-overwrite-all/main.js',
)

// ESM spec doesn't allow conflicting `export *` and such duplicate exports are removed (in this case "d"),
// but this is likely not possible to support due to Vite dev SSR's lazy nature.
// [Node]
// $ node -e 'import("./packages/vite/src/node/ssr/__tests__/fixtures/named-overwrite-all/main.js").then(console.log)'
// [Module: null prototype] { a: 'main-a', b: 'dep1-b', c: 'main-c' }
// [Rollup]
// Conflicting namespaces: "main.js" re-exports "d" from one of the modules "dep1.js" and "dep2.js" (will be ignored).
expect(mod).toMatchInlineSnapshot(`
{
"a": "main-a",
"b": "dep1-b",
"c": "main-c",
"d": "dep1-d",
}
`)
})

0 comments on commit 2fd2fc1

Please sign in to comment.