|
9 | 9 | const{ getOptionValue }=require('internal/options'); |
10 | 10 | const{ Buffer }=require('buffer'); |
11 | 11 | const{ERR_MANIFEST_ASSERT_INTEGRITY}=require('internal/errors').codes; |
12 | | -constpath=require('path'); |
| 12 | +constassert=require('internal/assert'); |
13 | 13 |
|
14 | 14 | functionprepareMainThreadExecution(expandArgv1=false){ |
15 | 15 | // Patch the process object with legacy properties and normalizations |
@@ -64,6 +64,9 @@ function prepareMainThreadExecution(expandArgv1 = false) { |
64 | 64 | initializeDeprecations(); |
65 | 65 | initializeCJSLoader(); |
66 | 66 | initializeESMLoader(); |
| 67 | + |
| 68 | +constCJSLoader=require('internal/modules/cjs/loader'); |
| 69 | +assert(!CJSLoader.hasLoadedAnyUserCJSModule); |
67 | 70 | loadPreloadModules(); |
68 | 71 | initializeFrozenIntrinsics(); |
69 | 72 | } |
@@ -398,7 +401,11 @@ function initializePolicy() { |
398 | 401 | } |
399 | 402 |
|
400 | 403 | functioninitializeCJSLoader(){ |
401 | | -require('internal/modules/cjs/loader').Module._initPaths(); |
| 404 | +constCJSLoader=require('internal/modules/cjs/loader'); |
| 405 | +CJSLoader.Module._initPaths(); |
| 406 | +// TODO(joyeecheung): deprecate this in favor of a proper hook? |
| 407 | +CJSLoader.Module.runMain= |
| 408 | +require('internal/modules/run_main').executeUserEntryPoint; |
402 | 409 | } |
403 | 410 |
|
404 | 411 | functioninitializeESMLoader(){ |
@@ -446,74 +453,11 @@ function loadPreloadModules() { |
446 | 453 | } |
447 | 454 | } |
448 | 455 |
|
449 | | -functionresolveMainPath(main){ |
450 | | -const{ toRealPath,Module: CJSModule}= |
451 | | -require('internal/modules/cjs/loader'); |
452 | | - |
453 | | -// Note extension resolution for the main entry point can be deprecated in a |
454 | | -// future major. |
455 | | -letmainPath=CJSModule._findPath(path.resolve(main),null,true); |
456 | | -if(!mainPath) |
457 | | -return; |
458 | | - |
459 | | -constpreserveSymlinksMain=getOptionValue('--preserve-symlinks-main'); |
460 | | -if(!preserveSymlinksMain) |
461 | | -mainPath=toRealPath(mainPath); |
462 | | - |
463 | | -returnmainPath; |
464 | | -} |
465 | | - |
466 | | -functionshouldUseESMLoader(mainPath){ |
467 | | -constexperimentalModules=getOptionValue('--experimental-modules'); |
468 | | -if(!experimentalModules) |
469 | | -returnfalse; |
470 | | -constuserLoader=getOptionValue('--experimental-loader'); |
471 | | -if(userLoader) |
472 | | -returntrue; |
473 | | -constexperimentalSpecifierResolution= |
474 | | -getOptionValue('--experimental-specifier-resolution'); |
475 | | -if(experimentalSpecifierResolution==='node') |
476 | | -returntrue; |
477 | | -// Determine the module format of the main |
478 | | -if(mainPath&&mainPath.endsWith('.mjs')) |
479 | | -returntrue; |
480 | | -if(!mainPath||mainPath.endsWith('.cjs')) |
481 | | -returnfalse; |
482 | | -const{ readPackageScope }=require('internal/modules/cjs/loader'); |
483 | | -constpkg=readPackageScope(mainPath); |
484 | | -returnpkg&&pkg.data.type==='module'; |
485 | | -} |
486 | | - |
487 | | -functionrunMainESM(mainPath){ |
488 | | -constesmLoader=require('internal/process/esm_loader'); |
489 | | -const{ pathToFileURL }=require('internal/url'); |
490 | | -const{ hasUncaughtExceptionCaptureCallback }= |
491 | | -require('internal/process/execution'); |
492 | | -returnesmLoader.initializeLoader().then(()=>{ |
493 | | -constmain=path.isAbsolute(mainPath) ? |
494 | | -pathToFileURL(mainPath).href : mainPath; |
495 | | -returnesmLoader.ESMLoader.import(main); |
496 | | -}).catch((e)=>{ |
497 | | -if(hasUncaughtExceptionCaptureCallback()){ |
498 | | -process._fatalException(e); |
499 | | -return; |
500 | | -} |
501 | | -internalBinding('errors').triggerUncaughtException( |
502 | | -e, |
503 | | -true/* fromPromise */ |
504 | | -); |
505 | | -}); |
506 | | -} |
507 | | - |
508 | | - |
509 | 456 | module.exports={ |
510 | 457 | patchProcessObject, |
511 | | - resolveMainPath, |
512 | | - runMainESM, |
513 | 458 | setupCoverageHooks, |
514 | 459 | setupWarningHandler, |
515 | 460 | setupDebugEnv, |
516 | | - shouldUseESMLoader, |
517 | 461 | prepareMainThreadExecution, |
518 | 462 | initializeDeprecations, |
519 | 463 | initializeESMLoader, |
|
0 commit comments