Skip to content
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

esm: ensure require.main for CJS top-level loads #21150

Closed
wants to merge 1 commit into from
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
7 changes: 1 addition & 6 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ class Loader {
loaderInstance = translators.get(format);
}

let inspectBrk = false;
if (process._breakFirstLine) {
delete process._breakFirstLine;
inspectBrk = true;
}
job = new ModuleJob(this, url, loaderInstance, inspectBrk);
job = new ModuleJob(this, url, loaderInstance, parentURL === undefined);
this.moduleMap.set(url, job);
return job;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const resolvedPromise = SafePromise.resolve();
class ModuleJob {
// `loader` is the Loader instance used for loading dependencies.
// `moduleProvider` is a function
constructor(loader, url, moduleProvider, inspectBrk) {
constructor(loader, url, moduleProvider, isMain) {
this.loader = loader;
this.error = null;
this.hadError = false;
this.inspectBrk = inspectBrk;
this.isMain = isMain;

// This is a Promise<{ module, reflect }>, whose fields will be copied
// onto `this` by `link()` below once it has been resolved.
this.modulePromise = moduleProvider(url);
this.modulePromise = moduleProvider(url, isMain);
Copy link
Member

Choose a reason for hiding this comment

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

is incorrect arity still a slow path

Copy link
Member

Choose a reason for hiding this comment

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

I believe it is, yeah - not that it likely means anything in this case.

this.module = undefined;
this.reflect = undefined;

Expand Down Expand Up @@ -82,7 +82,8 @@ class ModuleJob {
throw e;
}
try {
if (this.inspectBrk) {
if (this.isMain && process._breakFirstLine) {
delete process._breakFirstLine;
const initWrapper = process.binding('inspector').callAndPauseOnStart;
initWrapper(this.module.instantiate, this.module);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ translators.set('esm', async (url) => {
// Strategy for loading a node-style CommonJS module
const isWindows = process.platform === 'win32';
const winSepRegEx = /\//g;
translators.set('cjs', async (url) => {
translators.set('cjs', async (url, isMain) => {
debug(`Translating CJSModule ${url}`);
const pathname = internalURLModule.getPathFromURL(new URL(url));
const module = CJSModule._cache[
Expand All @@ -51,7 +51,7 @@ translators.set('cjs', async (url) => {
// we don't care about the return val of _load here because Module#load
// will handle it for us by checking the loader registry and filling the
// exports like above
CJSModule._load(pathname);
CJSModule._load(pathname, undefined, isMain);
});
});

Expand Down
6 changes: 6 additions & 0 deletions test/es-module/test-esm-cjs-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Flags: --experimental-modules
'use strict';
require('../common');
const assert = require('assert');
exports.asdf = 'asdf';
assert.strictEqual(require.main.exports.asdf, 'asdf');