From a75e44d135ffa3b19b4159f4ab7577f9f7ac60eb Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 5 Jun 2018 16:05:49 +0200 Subject: [PATCH] esm: ensure require.main for CJS top-level loads PR-URL: https://github.com/nodejs/node/pull/21150 Reviewed-By: Bradley Farias Reviewed-By: Benjamin Gruenbaum Reviewed-By: Gus Caplan Reviewed-By: Tiancheng "Timothy" Gu --- lib/internal/modules/esm/loader.js | 7 +------ lib/internal/modules/esm/module_job.js | 9 +++++---- lib/internal/modules/esm/translators.js | 4 ++-- test/es-module/test-esm-cjs-main.js | 6 ++++++ 4 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 test/es-module/test-esm-cjs-main.js diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 18b0827eee0f68..b3023fcd4f8e36 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -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; } diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 8943ca8809f802..6f0bb869314404 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -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); this.module = undefined; this.reflect = undefined; @@ -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 { diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 4ed6b2c3b023a9..476a420b8a9620 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -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[ @@ -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); }); }); diff --git a/test/es-module/test-esm-cjs-main.js b/test/es-module/test-esm-cjs-main.js new file mode 100644 index 00000000000000..ed6730aec1d2a2 --- /dev/null +++ b/test/es-module/test-esm-cjs-main.js @@ -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');