Skip to content

Commit 2fd2fc1

Browse files
authored
fix(ssr): named export should overwrite export all (#19534)
1 parent cb9165c commit 2fd2fc1

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

packages/vite/src/module-runner/runner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ function exportAll(exports: any, sourceModule: any) {
429429
return
430430

431431
for (const key in sourceModule) {
432-
if (key !== 'default' && key !== '__esModule') {
432+
if (key !== 'default' && key !== '__esModule' && !(key in exports)) {
433433
try {
434434
Object.defineProperty(exports, key, {
435435
enumerable: true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const a = 'dep1-a'
2+
export const b = 'dep1-b'
3+
export const c = 'dep1-c'
4+
export const d = 'dep1-d'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const d = 'dep2-d'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const a = 'main-a'
2+
export * from './dep1.js'
3+
export const c = 'main-c'
4+
export * from './dep2.js'

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

+23
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,26 @@ test('plugin error', async () => {
292292
Plugin: test-plugin"
293293
`)
294294
})
295+
296+
test('named exports overwrite export all', async () => {
297+
const server = await createDevServer()
298+
const mod = await server.ssrLoadModule(
299+
'./fixtures/named-overwrite-all/main.js',
300+
)
301+
302+
// ESM spec doesn't allow conflicting `export *` and such duplicate exports are removed (in this case "d"),
303+
// but this is likely not possible to support due to Vite dev SSR's lazy nature.
304+
// [Node]
305+
// $ node -e 'import("./packages/vite/src/node/ssr/__tests__/fixtures/named-overwrite-all/main.js").then(console.log)'
306+
// [Module: null prototype] { a: 'main-a', b: 'dep1-b', c: 'main-c' }
307+
// [Rollup]
308+
// Conflicting namespaces: "main.js" re-exports "d" from one of the modules "dep1.js" and "dep2.js" (will be ignored).
309+
expect(mod).toMatchInlineSnapshot(`
310+
{
311+
"a": "main-a",
312+
"b": "dep1-b",
313+
"c": "main-c",
314+
"d": "dep1-d",
315+
}
316+
`)
317+
})

0 commit comments

Comments
 (0)