Skip to content

Commit 7c1bb9b

Browse files
committed
esm: module.exports on CJS modules
1 parent abd73d8 commit 7c1bb9b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

lib/internal/modules/esm/translators.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,13 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
188188
const { exportNames, module } = cjsPreparseModuleExports(filename, source, format);
189189
cjsCache.set(url, module);
190190

191-
const wrapperNames = [...exportNames, 'module.exports'];
191+
const wrapperNames = [...exportNames];
192192
if (!exportNames.has('default')) {
193193
ArrayPrototypePush(wrapperNames, 'default');
194194
}
195+
if (!exportNames.has('module.exports')) {
196+
ArrayPrototypePush(wrapperNames, 'module.exports');
197+
}
195198

196199
if (isMain) {
197200
setOwnProperty(process, 'mainModule', module);
@@ -212,7 +215,8 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul
212215
({ exports } = module);
213216
}
214217
for (const exportName of exportNames) {
215-
if (!ObjectPrototypeHasOwnProperty(exports, exportName) || exportName === 'default') {
218+
if (exportName === 'default' || exportName === 'module.exports' ||
219+
!ObjectPrototypeHasOwnProperty(exports, exportName)) {
216220
continue;
217221
}
218222
// We might trigger a getter -> dont fail.
+10-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
if (global.maybe)
2-
module.exports = require('../is-object');
3-
exports['invalid identifier'] = 'yes';
4-
module.exports['?invalid'] = 'yes';
5-
module.exports['π'] = 'yes';
6-
exports['\u{D83C}'] = 'no';
7-
exports['\u{D83C}\u{DF10}'] = 'yes';
8-
exports.package = 10; // reserved word
9-
Object.defineProperty(exports, 'z', { value: 'yes' });
1+
if (global.maybe)
2+
module.exports = require('../is-object');
3+
exports['invalid identifier'] = 'yes';
4+
module.exports['?invalid'] = 'yes';
5+
module.exports['π'] = 'yes';
6+
exports['\u{D83C}'] = 'no';
7+
exports['\u{D83C}\u{DF10}'] = 'yes';
8+
exports.package = 10; // reserved word
9+
Object.defineProperty(exports, 'z', { value: 'yes' });
10+
exports['module.exports'] = 5;

0 commit comments

Comments
 (0)