Skip to content

Commit

Permalink
fix: Explicitly named exports should be exported over export * (#103)
Browse files Browse the repository at this point in the history
Closes #102
  • Loading branch information
timfish authored Jun 14, 2024
1 parent 29f51f6 commit 1c6f7b0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
31 changes: 24 additions & 7 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,32 @@ function isBareSpecifier (specifier) {
*/
async function processModule ({ srcUrl, context, parentGetSource, parentResolve, excludeDefault }) {
const exportNames = await getExports(srcUrl, context, parentGetSource)
const duplicates = new Set()
const starExports = new Set()
const setters = new Map()

const addSetter = (name, setter) => {
// When doing an `import *` duplicates become undefined, so do the same
const addSetter = (name, setter, isStarExport = false) => {
if (setters.has(name)) {
duplicates.add(name)
setters.delete(name)
} else if (!duplicates.has(name)) {
if (isStarExport) {
// If there's already a matching star export, delete it
if (starExports.has(name)) {
setters.delete(name)
}
// and return so this is excluded
return
}

// if we already have this export but it is from a * export, overwrite it
if (starExports.has(name)) {
starExports.delete(name)
setters.set(name, setter)
}
} else {
// Store export * exports so we know they can be overridden by explicit
// named exports
if (isStarExport) {
starExports.add(name)
}

setters.set(name, setter)
}
}
Expand Down Expand Up @@ -165,7 +182,7 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
excludeDefault: true
})
for (const [name, setter] of setters.entries()) {
addSetter(name, setter)
addSetter(name, setter, true)
}
} else {
addSetter(n, `
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/duplicate-b.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const foo = 'a'
export const foo = 'b'
1 change: 1 addition & 0 deletions test/fixtures/duplicate-c.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'c'
5 changes: 5 additions & 0 deletions test/fixtures/duplicate-explicit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './duplicate-a.mjs'
export * from './duplicate-b.mjs'
export { foo } from './duplicate-c.mjs'
export * from './duplicate-a.mjs'
export * from './duplicate-b.mjs'
13 changes: 13 additions & 0 deletions test/hook/duplicate-exports-explicit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as lib from '../fixtures/duplicate-explicit.mjs'
import { strictEqual } from 'assert'
import Hook from '../../index.js'

Hook((exports, name) => {
if (name.endsWith('duplicate-explicit.mjs')) {
strictEqual(exports.foo, 'c')
exports.foo += '-wrapped'
}
})

// foo should not be exported because there are duplicate exports
strictEqual(lib.foo, 'c-wrapped')
6 changes: 3 additions & 3 deletions test/hook/duplicate-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Hook from '../../index.js'

Hook((exports, name) => {
if (name.match(/duplicate\.mjs/)) {
// foo should not be exported because there are duplicate exports
strictEqual(exports.foo, undefined)
// foo should not be exported because there are duplicate * exports
strictEqual('foo' in exports, false)
// default should be exported
strictEqual(exports.default, 'foo')
}
Expand All @@ -14,6 +14,6 @@ Hook((exports, name) => {
notEqual(lib, undefined)

// foo should not be exported because there are duplicate exports
strictEqual(lib.foo, undefined)
strictEqual('foo' in lib, false)
// default should be exported
strictEqual(lib.default, 'foo')
6 changes: 6 additions & 0 deletions test/hook/v18.19-openai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ Hook((exports, name) => {
})

console.assert(OpenAI)

const openAI = new OpenAI({
apiKey: 'doesnt-matter'
})

console.assert(openAI)

0 comments on commit 1c6f7b0

Please sign in to comment.