GeezerVison is showing github.com — esm: ensure require.main for CJS top-level loads · nodejs/node@a75e44d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js +1-6Lines changed: 1 addition & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -110,12 +110,7 @@ class Loader { | 110 | 110 | loaderInstance = translators.get(format); | 111 | 111 | } | 112 | 112 |
| 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); | 119 | 114 | this.moduleMap.set(url, job); | 120 | 115 | return job; | 121 | 116 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,15 +12,15 @@ const resolvedPromise = SafePromise.resolve(); | 12 | 12 | class ModuleJob { | 13 | 13 | // `loader` is the Loader instance used for loading dependencies. | 14 | 14 | // `moduleProvider` is a function | 15 | | - constructor(loader, url, moduleProvider, inspectBrk) { | | 15 | + constructor(loader, url, moduleProvider, isMain) { | 16 | 16 | this.loader = loader; | 17 | 17 | this.error = null; | 18 | 18 | this.hadError = false; | 19 | | - this.inspectBrk = inspectBrk; | | 19 | + this.isMain = isMain; | 20 | 20 |
| 21 | 21 | // This is a Promise<{ module, reflect }>, whose fields will be copied | 22 | 22 | // onto `this` by `link()` below once it has been resolved. | 23 | | - this.modulePromise = moduleProvider(url); | | 23 | + this.modulePromise = moduleProvider(url, isMain); | 24 | 24 | this.module = undefined; | 25 | 25 | this.reflect = undefined; | 26 | 26 |
| @@ -82,7 +82,8 @@ class ModuleJob { | 82 | 82 | throw e; | 83 | 83 | } | 84 | 84 | try { | 85 | | - if (this.inspectBrk) { | | 85 | + if (this.isMain && process._breakFirstLine) { | | 86 | + delete process._breakFirstLine; | 86 | 87 | const initWrapper = process.binding('inspector').callAndPauseOnStart; | 87 | 88 | initWrapper(this.module.instantiate, this.module); | 88 | 89 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,7 @@ translators.set('esm', async (url) => { | 36 | 36 | // Strategy for loading a node-style CommonJS module | 37 | 37 | const isWindows = process.platform === 'win32'; | 38 | 38 | const winSepRegEx = /\//g; | 39 | | -translators.set('cjs', async (url) => { | | 39 | +translators.set('cjs', async (url, isMain) => { | 40 | 40 | debug(`Translating CJSModule ${url}`); | 41 | 41 | const pathname = internalURLModule.getPathFromURL(new URL(url)); | 42 | 42 | const module = CJSModule._cache[ | @@ -51,7 +51,7 @@ translators.set('cjs', async (url) => { | 51 | 51 | // we don't care about the return val of _load here because Module#load | 52 | 52 | // will handle it for us by checking the loader registry and filling the | 53 | 53 | // exports like above | 54 | | - CJSModule._load(pathname); | | 54 | + CJSModule._load(pathname, undefined, isMain); | 55 | 55 | }); | 56 | 56 | }); | 57 | 57 |
| |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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'); |
|