Skip to content

Commit

Permalink
Revert "esm: ensure require.main for CJS top-level loads"
Browse files Browse the repository at this point in the history
This reverts commit 8ee604c
due to a regression when loading a CJS main script in ESM mode.

Minor merge conflict cleanup was necessary for
`lib/internal/modules/esm/module_job.js`.

Refs: nodejs#21150
  • Loading branch information
addaleax committed Jun 20, 2018
1 parent 99e6ecb commit 39d40c8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
7 changes: 6 additions & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ class Loader {
loaderInstance = translators.get(format);
}

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

// 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, isMain);
this.modulePromise = moduleProvider(url);
this.module = undefined;
this.reflect = undefined;

Expand Down Expand Up @@ -72,8 +72,7 @@ class ModuleJob {
};
await addJobsToDependencyGraph(this);
try {
if (this.isMain && process._breakFirstLine) {
delete process._breakFirstLine;
if (this.inspectBrk) {
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, isMain) => {
translators.set('cjs', async (url) => {
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, isMain) => {
// 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, undefined, isMain);
CJSModule._load(pathname);
});
});

Expand Down
6 changes: 0 additions & 6 deletions test/es-module/test-esm-cjs-main.js

This file was deleted.

0 comments on commit 39d40c8

Please sign in to comment.