diff options
Diffstat (limited to 'lib/internal/bootstrap/pre_execution.js')
-rw-r--r-- | lib/internal/bootstrap/pre_execution.js | 67 |
1 files changed, 9 insertions, 58 deletions
diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 793ee18142..8edec86a3e 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -5,7 +5,7 @@ const { Object, SafeWeakMap } = primordials; const { getOptionValue } = require('internal/options'); const { Buffer } = require('buffer'); const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes; -const path = require('path'); +const assert = require('internal/assert'); function prepareMainThreadExecution(expandArgv1 = false) { // Patch the process object with legacy properties and normalizations @@ -60,6 +60,9 @@ function prepareMainThreadExecution(expandArgv1 = false) { initializeDeprecations(); initializeCJSLoader(); initializeESMLoader(); + + const CJSLoader = require('internal/modules/cjs/loader'); + assert(!CJSLoader.hasLoadedAnyUserCJSModule); loadPreloadModules(); initializeFrozenIntrinsics(); } @@ -394,7 +397,11 @@ function initializePolicy() { } function initializeCJSLoader() { - require('internal/modules/cjs/loader').Module._initPaths(); + const CJSLoader = require('internal/modules/cjs/loader'); + CJSLoader.Module._initPaths(); + // TODO(joyeecheung): deprecate this in favor of a proper hook? + CJSLoader.Module.runMain = + require('internal/modules/run_main').executeUserEntryPoint; } function initializeESMLoader() { @@ -433,67 +440,11 @@ function loadPreloadModules() { } } -function resolveMainPath(main) { - const { toRealPath, Module: CJSModule } = - require('internal/modules/cjs/loader'); - - // Note extension resolution for the main entry point can be deprecated in a - // future major. - let mainPath = CJSModule._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 { readPackageScope } = require('internal/modules/cjs/loader'); - 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 */ - ); - }); -} - - module.exports = { patchProcessObject, - resolveMainPath, - runMainESM, setupCoverageHooks, setupWarningHandler, setupDebugEnv, - shouldUseESMLoader, prepareMainThreadExecution, initializeDeprecations, initializeESMLoader, |