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

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Guy Bedford 2018-06-05 16:05:49 +02:00
parent eb2d3a3b9c
commit 8ee604ca71
4 changed files with 14 additions and 12 deletions

View File

@ -110,12 +110,7 @@ class Loader {
loaderInstance = translators.get(format); loaderInstance = translators.get(format);
} }
let inspectBrk = false; job = new ModuleJob(this, url, loaderInstance, parentURL === undefined);
if (process._breakFirstLine) {
delete process._breakFirstLine;
inspectBrk = true;
}
job = new ModuleJob(this, url, loaderInstance, inspectBrk);
this.moduleMap.set(url, job); this.moduleMap.set(url, job);
return job; return job;
} }

View File

@ -12,15 +12,15 @@ const resolvedPromise = SafePromise.resolve();
class ModuleJob { class ModuleJob {
// `loader` is the Loader instance used for loading dependencies. // `loader` is the Loader instance used for loading dependencies.
// `moduleProvider` is a function // `moduleProvider` is a function
constructor(loader, url, moduleProvider, inspectBrk) { constructor(loader, url, moduleProvider, isMain) {
this.loader = loader; this.loader = loader;
this.error = null; this.error = null;
this.hadError = false; this.hadError = false;
this.inspectBrk = inspectBrk; this.isMain = isMain;
// This is a Promise<{ module, reflect }>, whose fields will be copied // This is a Promise<{ module, reflect }>, whose fields will be copied
// onto `this` by `link()` below once it has been resolved. // onto `this` by `link()` below once it has been resolved.
this.modulePromise = moduleProvider(url); this.modulePromise = moduleProvider(url, isMain);
this.module = undefined; this.module = undefined;
this.reflect = undefined; this.reflect = undefined;
@ -82,7 +82,8 @@ class ModuleJob {
throw e; throw e;
} }
try { try {
if (this.inspectBrk) { if (this.isMain && process._breakFirstLine) {
delete process._breakFirstLine;
const initWrapper = process.binding('inspector').callAndPauseOnStart; const initWrapper = process.binding('inspector').callAndPauseOnStart;
initWrapper(this.module.instantiate, this.module); initWrapper(this.module.instantiate, this.module);
} else { } else {

View File

@ -36,7 +36,7 @@ translators.set('esm', async (url) => {
// Strategy for loading a node-style CommonJS module // Strategy for loading a node-style CommonJS module
const isWindows = process.platform === 'win32'; const isWindows = process.platform === 'win32';
const winSepRegEx = /\//g; const winSepRegEx = /\//g;
translators.set('cjs', async (url) => { translators.set('cjs', async (url, isMain) => {
debug(`Translating CJSModule ${url}`); debug(`Translating CJSModule ${url}`);
const pathname = internalURLModule.getPathFromURL(new URL(url)); const pathname = internalURLModule.getPathFromURL(new URL(url));
const module = CJSModule._cache[ const module = CJSModule._cache[
@ -51,7 +51,7 @@ translators.set('cjs', async (url) => {
// we don't care about the return val of _load here because Module#load // 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 // will handle it for us by checking the loader registry and filling the
// exports like above // exports like above
CJSModule._load(pathname); CJSModule._load(pathname, undefined, isMain);
}); });
}); });

View File

@ -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');