diff --git a/hook.js b/hook.js index a9f7cc4..bff6c1f 100644 --- a/hook.js +++ b/hook.js @@ -198,12 +198,13 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve, const exportNames = await getExports(srcUrl, context, parentGetSource) const starExports = new Set() const setters = new Map() + const starExportSources = new Map() - const addSetter = (name, setter, isStarExport = false) => { + const addSetter = (name, source, setter, isStarExport = false) => { if (setters.has(name)) { if (isStarExport) { - // If there's already a matching star export, delete it - if (starExports.has(name)) { + // If there's already a matching star export and it comes from a different source, delete it + if (starExports.has(name) && !starExportSources.get(name).has(source)) { setters.delete(name) } // and return so this is excluded @@ -220,6 +221,9 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve, // named exports if (isStarExport) { starExports.add(name) + const sources = starExportSources.get(name) || new Set() + sources.add(source) + starExportSources.set(name, sources) } setters.set(name, setter) @@ -249,10 +253,10 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve, }) for (const [name, setter] of subSetters.entries()) { - addSetter(name, setter, true) + addSetter(name, srcUrl, setter, true) } } else { - addSetter(n, ` + addSetter(n, srcUrl, ` let $${n} try { $${n} = _.${n} = namespace.${n} diff --git a/test/fixtures/duplicate-d.mjs b/test/fixtures/duplicate-d.mjs new file mode 100644 index 0000000..301ad51 --- /dev/null +++ b/test/fixtures/duplicate-d.mjs @@ -0,0 +1 @@ +export * from './duplicate-c.mjs' diff --git a/test/fixtures/duplicate-same-source.mjs b/test/fixtures/duplicate-same-source.mjs new file mode 100644 index 0000000..0cebc90 --- /dev/null +++ b/test/fixtures/duplicate-same-source.mjs @@ -0,0 +1,2 @@ +export * from './duplicate-c.mjs' +export * from './duplicate-d.mjs' diff --git a/test/hook/duplicate-exports-same-source.mjs b/test/hook/duplicate-exports-same-source.mjs new file mode 100644 index 0000000..7c09e1b --- /dev/null +++ b/test/hook/duplicate-exports-same-source.mjs @@ -0,0 +1,15 @@ +import * as lib from '../fixtures/duplicate-same-source.mjs' +import { notEqual, strictEqual } from 'assert' +import Hook from '../../index.js' + +Hook((exports, name) => { + if (name.match(/duplicate-same-source\.mjs/)) { + // foo should be exported because it comes from the same source + strictEqual('foo' in exports, true) + } +}) + +notEqual(lib, undefined) + +// foo should be exported because it comes from the same source +strictEqual('foo' in lib, true)