@@ -5,7 +5,7 @@ const { Object, SafeWeakMap } = primordials;
55const { getOptionValue } = require ( 'internal/options' ) ;
66const { Buffer } = require ( 'buffer' ) ;
77const { ERR_MANIFEST_ASSERT_INTEGRITY } = require ( 'internal/errors' ) . codes ;
8- const path = require ( 'path ' ) ;
8+ const assert = require ( 'internal/assert ' ) ;
99
1010function prepareMainThreadExecution ( expandArgv1 = false ) {
1111 // Patch the process object with legacy properties and normalizations
@@ -60,6 +60,9 @@ function prepareMainThreadExecution(expandArgv1 = false) {
6060 initializeDeprecations ( ) ;
6161 initializeCJSLoader ( ) ;
6262 initializeESMLoader ( ) ;
63+
64+ const CJSLoader = require ( 'internal/modules/cjs/loader' ) ;
65+ assert ( ! CJSLoader . hasLoadedAnyUserCJSModule ) ;
6366 loadPreloadModules ( ) ;
6467 initializeFrozenIntrinsics ( ) ;
6568}
@@ -394,7 +397,11 @@ function initializePolicy() {
394397}
395398
396399function initializeCJSLoader ( ) {
397- require ( 'internal/modules/cjs/loader' ) . Module . _initPaths ( ) ;
400+ const CJSLoader = require ( 'internal/modules/cjs/loader' ) ;
401+ CJSLoader . Module . _initPaths ( ) ;
402+ // TODO(joyeecheung): deprecate this in favor of a proper hook?
403+ CJSLoader . Module . runMain =
404+ require ( 'internal/modules/run_main' ) . executeUserEntryPoint ;
398405}
399406
400407function initializeESMLoader ( ) {
@@ -433,67 +440,11 @@ function loadPreloadModules() {
433440 }
434441}
435442
436- function resolveMainPath ( main ) {
437- const { toRealPath, Module : CJSModule } =
438- require ( 'internal/modules/cjs/loader' ) ;
439-
440- // Note extension resolution for the main entry point can be deprecated in a
441- // future major.
442- let mainPath = CJSModule . _findPath ( path . resolve ( main ) , null , true ) ;
443- if ( ! mainPath )
444- return ;
445-
446- const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
447- if ( ! preserveSymlinksMain )
448- mainPath = toRealPath ( mainPath ) ;
449-
450- return mainPath ;
451- }
452-
453- function shouldUseESMLoader ( mainPath ) {
454- const userLoader = getOptionValue ( '--experimental-loader' ) ;
455- if ( userLoader )
456- return true ;
457- // Determine the module format of the main
458- if ( mainPath && mainPath . endsWith ( '.mjs' ) )
459- return true ;
460- if ( ! mainPath || mainPath . endsWith ( '.cjs' ) )
461- return false ;
462- const { readPackageScope } = require ( 'internal/modules/cjs/loader' ) ;
463- const pkg = readPackageScope ( mainPath ) ;
464- return pkg && pkg . data . type === 'module' ;
465- }
466-
467- function runMainESM ( mainPath ) {
468- const esmLoader = require ( 'internal/process/esm_loader' ) ;
469- const { pathToFileURL } = require ( 'internal/url' ) ;
470- const { hasUncaughtExceptionCaptureCallback } =
471- require ( 'internal/process/execution' ) ;
472- return esmLoader . initializeLoader ( ) . then ( ( ) => {
473- const main = path . isAbsolute ( mainPath ) ?
474- pathToFileURL ( mainPath ) . href : mainPath ;
475- return esmLoader . ESMLoader . import ( main ) ;
476- } ) . catch ( ( e ) => {
477- if ( hasUncaughtExceptionCaptureCallback ( ) ) {
478- process . _fatalException ( e ) ;
479- return ;
480- }
481- internalBinding ( 'errors' ) . triggerUncaughtException (
482- e ,
483- true /* fromPromise */
484- ) ;
485- } ) ;
486- }
487-
488-
489443module . exports = {
490444 patchProcessObject,
491- resolveMainPath,
492- runMainESM,
493445 setupCoverageHooks,
494446 setupWarningHandler,
495447 setupDebugEnv,
496- shouldUseESMLoader,
497448 prepareMainThreadExecution,
498449 initializeDeprecations,
499450 initializeESMLoader,
0 commit comments