Skip to content

Commit

Permalink
esm: module.exports on CJS modules
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Mar 7, 2025
1 parent abd73d8 commit 7c1bb9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
8 changes: 6 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
const { exportNames, module } = cjsPreparseModuleExports(filename, source, format);
cjsCache.set(url, module);

const wrapperNames = [...exportNames, 'module.exports'];
const wrapperNames = [...exportNames];
if (!exportNames.has('default')) {
ArrayPrototypePush(wrapperNames, 'default');
}
if (!exportNames.has('module.exports')) {
ArrayPrototypePush(wrapperNames, 'module.exports');
}

if (isMain) {
setOwnProperty(process, 'mainModule', module);
Expand All @@ -212,7 +215,8 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
({ exports } = module);
}
for (const exportName of exportNames) {
if (!ObjectPrototypeHasOwnProperty(exports, exportName) || exportName === 'default') {
if (exportName === 'default' || exportName === 'module.exports' ||
!ObjectPrototypeHasOwnProperty(exports, exportName)) {
continue;
}
// We might trigger a getter -> dont fail.
Expand Down
19 changes: 10 additions & 9 deletions test/fixtures/es-modules/exports-cases.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
if (global.maybe)
module.exports = require('../is-object');
exports['invalid identifier'] = 'yes';
module.exports['?invalid'] = 'yes';
module.exports['π'] = 'yes';
exports['\u{D83C}'] = 'no';
exports['\u{D83C}\u{DF10}'] = 'yes';
exports.package = 10; // reserved word
Object.defineProperty(exports, 'z', { value: 'yes' });
if (global.maybe)
module.exports = require('../is-object');
exports['invalid identifier'] = 'yes';
module.exports['?invalid'] = 'yes';
module.exports['π'] = 'yes';
exports['\u{D83C}'] = 'no';
exports['\u{D83C}\u{DF10}'] = 'yes';
exports.package = 10; // reserved word
Object.defineProperty(exports, 'z', { value: 'yes' });
exports['module.exports'] = 5;

0 comments on commit 7c1bb9b

Please sign in to comment.