Skip to content
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
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",
}
`)
Comment on lines +302 to +316
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just learned that ESM spec says duplicate names from export * from ... should be removed entirely 😮 (Didn't actually read the spec but it's on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export#specifications:~:text=If%20there%20are%20two%20wildcard%20exports%20statements%20that%20implicitly%20re%2Dexport%20the%20same%20name%2C%20neither%20one%20is%20re%2Dexported.)

I think this case is hard to handle on Vite SSR model, so the behavior here is that first export * wins.

})