Skip to content

Commit e8f5b58

Browse files
committed
fix: duplicate exports from the same source file are excluded
fix #171
1 parent 53a33a9 commit e8f5b58

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

hook.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
198198
const exportNames = await getExports(srcUrl, context, parentGetSource)
199199
const starExports = new Set()
200200
const setters = new Map()
201+
const starExportSources = new Map()
201202

202-
const addSetter = (name, setter, isStarExport = false) => {
203+
const addSetter = (name, source, setter, isStarExport = false) => {
203204
if (setters.has(name)) {
204205
if (isStarExport) {
205-
// If there's already a matching star export, delete it
206-
if (starExports.has(name)) {
206+
// If there's already a matching star export and it comes from a different source, delete it
207+
if (starExports.has(name) && !starExportSources.get('name').has('source')) {
207208
setters.delete(name)
208209
}
209210
// and return so this is excluded
@@ -220,6 +221,9 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
220221
// named exports
221222
if (isStarExport) {
222223
starExports.add(name)
224+
const sources = starExportSources.get(name) || new Set()
225+
sources.add(source)
226+
starExportSources.set(name, sources)
223227
}
224228

225229
setters.set(name, setter)
@@ -249,10 +253,10 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
249253
})
250254

251255
for (const [name, setter] of subSetters.entries()) {
252-
addSetter(name, setter, true)
256+
addSetter(name, srcUrl, setter, true)
253257
}
254258
} else {
255-
addSetter(n, `
259+
addSetter(n, srcUrl, `
256260
let $${n}
257261
try {
258262
$${n} = _.${n} = namespace.${n}

test/fixtures/duplicate-d.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './duplicate-a.mjs'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './duplicate-a.mjs'
2+
export * from './duplicate-d.mjs'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as lib from '../fixtures/duplicate-same-source.mjs'
2+
import { notEqual, strictEqual } from 'assert'
3+
import Hook from '../../index.js'
4+
5+
Hook((exports, name) => {
6+
if (name.match(/duplicate-same-source\.mjs/)) {
7+
console.log(JSON.stringify(exports, null, 2));
8+
// foo should be exported<D-s><D-s>because it comes from the same source
9+
strictEqual('foo' in exports, true)
10+
}
11+
})
12+
13+
notEqual(lib, undefined)
14+
15+
// foo should be exported because it comes from the same source
16+
strictEqual('foo' in lib, true)

0 commit comments

Comments
 (0)