-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: reduce circular dependency of internal/modules/cjs/loader
Previously `internal/bootstrap/pre_execution.js` requires `internal/modules/cjs/loader.js` which in turn requires `internal/bootstrap/pre_execution.js`. This patch moves the entry point execution logic out of `pre_execution.js` and puts it into `internal/modules/run_main.js`. It also tests that `Module.runMain` can be monkey-patched before further deprecation/refactoring can be done. Also added an internal assertion `hasLoadedAnyUserCJSModule` for documentation purposes. PR-URL: #30349 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
66e1adf
commit c7c5660
Showing
25 changed files
with
152 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const CJSLoader = require('internal/modules/cjs/loader'); | ||
const { Module, toRealPath, readPackageScope } = CJSLoader; | ||
const { getOptionValue } = require('internal/options'); | ||
const path = require('path'); | ||
|
||
function resolveMainPath(main) { | ||
// Note extension resolution for the main entry point can be deprecated in a | ||
// future major. | ||
// Module._findPath is monkey-patchable here. | ||
let mainPath = Module._findPath(path.resolve(main), null, true); | ||
if (!mainPath) | ||
return; | ||
|
||
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); | ||
if (!preserveSymlinksMain) | ||
mainPath = toRealPath(mainPath); | ||
|
||
return mainPath; | ||
} | ||
|
||
function shouldUseESMLoader(mainPath) { | ||
const userLoader = getOptionValue('--experimental-loader'); | ||
if (userLoader) | ||
return true; | ||
// Determine the module format of the main | ||
if (mainPath && mainPath.endsWith('.mjs')) | ||
return true; | ||
if (!mainPath || mainPath.endsWith('.cjs')) | ||
return false; | ||
const pkg = readPackageScope(mainPath); | ||
return pkg && pkg.data.type === 'module'; | ||
} | ||
|
||
function runMainESM(mainPath) { | ||
const esmLoader = require('internal/process/esm_loader'); | ||
const { pathToFileURL } = require('internal/url'); | ||
const { hasUncaughtExceptionCaptureCallback } = | ||
require('internal/process/execution'); | ||
return esmLoader.initializeLoader().then(() => { | ||
const main = path.isAbsolute(mainPath) ? | ||
pathToFileURL(mainPath).href : mainPath; | ||
return esmLoader.ESMLoader.import(main); | ||
}).catch((e) => { | ||
if (hasUncaughtExceptionCaptureCallback()) { | ||
process._fatalException(e); | ||
return; | ||
} | ||
internalBinding('errors').triggerUncaughtException( | ||
e, | ||
true /* fromPromise */ | ||
); | ||
}); | ||
} | ||
|
||
// For backwards compatibility, we have to run a bunch of | ||
// monkey-patchable code that belongs to the CJS loader (exposed by | ||
// `require('module')`) even when the entry point is ESM. | ||
function executeUserEntryPoint(main = process.argv[1]) { | ||
const resolvedMain = resolveMainPath(main); | ||
const useESMLoader = shouldUseESMLoader(resolvedMain); | ||
if (useESMLoader) { | ||
runMainESM(resolvedMain || main); | ||
} else { | ||
// Module._load is the monkey-patchable CJS module loader. | ||
Module._load(main, null, true); | ||
} | ||
} | ||
|
||
module.exports = { | ||
executeUserEntryPoint | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
const oldRunMain = require('module').runMain; | ||
|
||
require('module').runMain = function(...args) { | ||
console.log('runMain is monkey patched!'); | ||
oldRunMain(...args); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.