Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a75e44d

Browse filesBrowse files
guybedfordtargos
authored andcommitted
esm: ensure require.main for CJS top-level loads
PR-URL: #21150 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
1 parent ffc29c1 commit a75e44d
Copy full SHA for a75e44d

File tree

Expand file treeCollapse file tree

4 files changed

+14
-12
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+14
-12
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ class Loader {
110110
loaderInstance = translators.get(format);
111111
}
112112

113-
let inspectBrk = false;
114-
if (process._breakFirstLine) {
115-
delete process._breakFirstLine;
116-
inspectBrk = true;
117-
}
118-
job = new ModuleJob(this, url, loaderInstance, inspectBrk);
113+
job = new ModuleJob(this, url, loaderInstance, parentURL === undefined);
119114
this.moduleMap.set(url, job);
120115
return job;
121116
}
Collapse file

‎lib/internal/modules/esm/module_job.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const resolvedPromise = SafePromise.resolve();
1212
class ModuleJob {
1313
// `loader` is the Loader instance used for loading dependencies.
1414
// `moduleProvider` is a function
15-
constructor(loader, url, moduleProvider, inspectBrk) {
15+
constructor(loader, url, moduleProvider, isMain) {
1616
this.loader = loader;
1717
this.error = null;
1818
this.hadError = false;
19-
this.inspectBrk = inspectBrk;
19+
this.isMain = isMain;
2020

2121
// This is a Promise<{ module, reflect }>, whose fields will be copied
2222
// onto `this` by `link()` below once it has been resolved.
23-
this.modulePromise = moduleProvider(url);
23+
this.modulePromise = moduleProvider(url, isMain);
2424
this.module = undefined;
2525
this.reflect = undefined;
2626

@@ -82,7 +82,8 @@ class ModuleJob {
8282
throw e;
8383
}
8484
try {
85-
if (this.inspectBrk) {
85+
if (this.isMain && process._breakFirstLine) {
86+
delete process._breakFirstLine;
8687
const initWrapper = process.binding('inspector').callAndPauseOnStart;
8788
initWrapper(this.module.instantiate, this.module);
8889
} else {
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ translators.set('esm', async (url) => {
3636
// Strategy for loading a node-style CommonJS module
3737
const isWindows = process.platform === 'win32';
3838
const winSepRegEx = /\//g;
39-
translators.set('cjs', async (url) => {
39+
translators.set('cjs', async (url, isMain) => {
4040
debug(`Translating CJSModule ${url}`);
4141
const pathname = internalURLModule.getPathFromURL(new URL(url));
4242
const module = CJSModule._cache[
@@ -51,7 +51,7 @@ translators.set('cjs', async (url) => {
5151
// we don't care about the return val of _load here because Module#load
5252
// will handle it for us by checking the loader registry and filling the
5353
// exports like above
54-
CJSModule._load(pathname);
54+
CJSModule._load(pathname, undefined, isMain);
5555
});
5656
});
5757

Collapse file
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Flags: --experimental-modules
2+
'use strict';
3+
require('../common');
4+
const assert = require('assert');
5+
exports.asdf = 'asdf';
6+
assert.strictEqual(require.main.exports.asdf, 'asdf');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.