Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Duplicate declarations #79

Closed
wants to merge 6 commits into from
Closed
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
27 changes: 23 additions & 4 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ function isBareSpecifier (specifier) {
}
}

function mapExcludingDuplicates () {
const map = new Map()
const duplicates = new Set()
return {
set (k, v, isDefault = false) {
if (map.has(k)) {
if (!isDefault) {
duplicates.add(k)
map.delete(k)
}
} else {
map.set(k, v)
}
},
values: () => map.values(),
entries: () => map.entries()
}
}

/**
* @typedef {object} ProcessedModule
* @property {string[]} imports A set of ESM import lines to be added to the
Expand Down Expand Up @@ -174,7 +193,7 @@ async function processModule ({
// exports will result in the "foo" export being defined twice in our shim.
// The map allows us to avoid this situation at the cost of losing the
// named export in favor of the default export.
const setters = new Map()
const setters = mapExcludingDuplicates()

for (const n of exportNames) {
if (isStarExportLine(n) === true) {
Expand Down Expand Up @@ -215,18 +234,18 @@ async function processModule ({
// needs to utilize that new name while being initialized from the
// corresponding origin namespace.
const renamedExport = matches[2]
setters.set(`$${renamedExport}${ns}`, `
setters.set(renamedExport, `
let $${renamedExport} = ${ns}.default
export { $${renamedExport} as ${renamedExport} }
set.${renamedExport} = (v) => {
$${renamedExport} = v
return true
}
`)
`, true)
continue
}

setters.set(`$${n}` + ns, `
setters.set(n, `
let $${n} = ${ns}.${n}
export { $${n} as ${n} }
set.${n} = (v) => {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/duplicate-a.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'a'
1 change: 1 addition & 0 deletions test/fixtures/duplicate-b.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'b'
2 changes: 2 additions & 0 deletions test/fixtures/duplicate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './duplicate-a.mjs'
export * from './duplicate-b.mjs'
6 changes: 6 additions & 0 deletions test/hook/duplicate-exports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as lib from '../fixtures/duplicate.mjs'
import { notEqual, strictEqual } from 'assert'

notEqual(lib, undefined)
// foo should not be exported because there are duplicate exports
strictEqual(lib.foo, undefined)