Skip to content
Closed
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
module: do not warn when accessing __esModule of unfinished exports
Since this property access is performed by generated code, and not
used for accessing the actual exports of a module (and because
transpilers generally define it as the first key of `module.exports`
when it *is* present), it should be okay to allow it.

Refs: #29935
Fixes: #33046
  • Loading branch information
addaleax committed Apr 24, 2020
commit 0b9106977efc6500919802aa291c7c63fb4cb7b0
7 changes: 5 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,16 @@ function emitCircularRequireWarning(prop) {
// warns when non-existend properties are accessed.
const CircularRequirePrototypeWarningProxy = new Proxy({}, {
get(target, prop) {
if (prop in target) return target[prop];
// Allow __esModule access in any case because it is used in the output
// of transpiled code to determine whether something comes from an
// ES module, and is not used as a regular key of `module.exports`.
if (prop in target || prop === '__esModule') return target[prop];
emitCircularRequireWarning(prop);
return undefined;
},

getOwnPropertyDescriptor(target, prop) {
if (ObjectPrototypeHasOwnProperty(target, prop))
if (ObjectPrototypeHasOwnProperty(target, prop) || prop === '__esModule')
return ObjectGetOwnPropertyDescriptor(target, prop);
emitCircularRequireWarning(prop);
return undefined;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cycles/warning-esm-half-transpiled-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./warning-esm-half-transpiled-b.js');
2 changes: 2 additions & 0 deletions test/fixtures/cycles/warning-esm-half-transpiled-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = require('./warning-esm-half-transpiled-a.js');
a.__esModule;
7 changes: 7 additions & 0 deletions test/parallel/test-module-circular-dependency-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ assert.strictEqual(Object.getPrototypeOf(classExport).name, 'Parent');
const esmTranspiledExport =
require(fixtures.path('cycles', 'warning-esm-transpiled-a.js'));
assert.strictEqual(esmTranspiledExport.__esModule, true);

// If module.exports.__esModule is being accessed but is not present, e.g.
// because only the one of the files is a transpiled ES module, no warning
// should be emitted.
const halfTranspiledExport =
require(fixtures.path('cycles', 'warning-esm-half-transpiled-a.js'));
assert.strictEqual(halfTranspiledExport.__esModule, undefined);