|
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 | | -const path = require('path'); |
| 12 | +const assert = require('internal/assert'); |
13 | 13 |
|
14 | 14 | function prepareMainThreadExecution(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 | + const CJSLoader = 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 | function initializeCJSLoader() { |
401 | | - require('internal/modules/cjs/loader').Module._initPaths(); |
| 404 | + const CJSLoader = 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 | function initializeESMLoader() { |
@@ -446,74 +453,11 @@ function loadPreloadModules() { |
446 | 453 | } |
447 | 454 | } |
448 | 455 |
|
449 | | -function resolveMainPath(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 | | - let mainPath = CJSModule._findPath(path.resolve(main), null, true); |
456 | | - if (!mainPath) |
457 | | - return; |
458 | | - |
459 | | - const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); |
460 | | - if (!preserveSymlinksMain) |
461 | | - mainPath = toRealPath(mainPath); |
462 | | - |
463 | | - return mainPath; |
464 | | -} |
465 | | - |
466 | | -function shouldUseESMLoader(mainPath) { |
467 | | - const experimentalModules = getOptionValue('--experimental-modules'); |
468 | | - if (!experimentalModules) |
469 | | - return false; |
470 | | - const userLoader = getOptionValue('--experimental-loader'); |
471 | | - if (userLoader) |
472 | | - return true; |
473 | | - const experimentalSpecifierResolution = |
474 | | - getOptionValue('--experimental-specifier-resolution'); |
475 | | - if (experimentalSpecifierResolution === 'node') |
476 | | - return true; |
477 | | - // Determine the module format of the main |
478 | | - if (mainPath && mainPath.endsWith('.mjs')) |
479 | | - return true; |
480 | | - if (!mainPath || mainPath.endsWith('.cjs')) |
481 | | - return false; |
482 | | - const { readPackageScope } = require('internal/modules/cjs/loader'); |
483 | | - const pkg = readPackageScope(mainPath); |
484 | | - return pkg && pkg.data.type === 'module'; |
485 | | -} |
486 | | - |
487 | | -function runMainESM(mainPath) { |
488 | | - const esmLoader = require('internal/process/esm_loader'); |
489 | | - const { pathToFileURL } = require('internal/url'); |
490 | | - const { hasUncaughtExceptionCaptureCallback } = |
491 | | - require('internal/process/execution'); |
492 | | - return esmLoader.initializeLoader().then(() => { |
493 | | - const main = path.isAbsolute(mainPath) ? |
494 | | - pathToFileURL(mainPath).href : mainPath; |
495 | | - return esmLoader.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