Skip to content

fix: Account for module.exports export #197

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 16 additions & 7 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
}

for (const n of exportNames) {
if (n === 'default' && excludeDefault) continue
// Skip default exports
if (n === 'default' && excludeDefault) {
continue
}

if (isStarExportLine(n) === true) {
const [, modFile] = n.split('* from ')
Expand All @@ -254,19 +257,25 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
addSetter(name, setter, true)
}
} else {
// Handle `module.exports` specially since it contains a dot which is invalid in variable names
// `module.exports` used by packages like `yargs-parser` to shim CJS support in ESM.
const variableName = n === 'module.exports' ? '$moduleExports' : `$${n}`
const objectKey = n === 'module.exports' ? JSON.stringify(n) : n
const objectKeyWithAccess = n === 'module.exports' ? `[${objectKey}]` : `.${objectKey}`
Comment on lines +263 to +264
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the object[bracket] syntax for all exports?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured we had done the dot syntax for some reason, so I elected to not change it, but now looking at it further, we probably can. Let me test something else out.


addSetter(n, `
let $${n}
let ${variableName}
try {
$${n} = _.${n} = namespace.${n}
${variableName} = _${objectKeyWithAccess} = namespace${objectKeyWithAccess}
} catch (err) {
if (!(err instanceof ReferenceError)) throw err
}
export { $${n} as ${n} }
set.${n} = (v) => {
$${n} = v
export { ${variableName} as ${objectKey} }
set${objectKeyWithAccess} = (v) => {
${variableName} = v
return true
}
get.${n} = () => $${n}
get${objectKeyWithAccess} = () => ${variableName}
`)
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/module-exports-cjs-shim.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function foo () {
// do nothing
}

export { foo as 'module.exports' }
4 changes: 4 additions & 0 deletions test/low-level/v16-fake-cjs-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ui from '../fixtures/module-exports-cjs-shim.mjs'
import { notStrictEqual } from 'assert'

notStrictEqual(ui, undefined)