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: Explicitly named exports should be exported over export * #103

Merged
merged 2 commits into from
Jun 14, 2024
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
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 All @@ -164,7 +181,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)
Loading