Skip to content

Commit c750488

Browse files
committed
move main functions into pre_execution
1 parent fe0c923 commit c750488

File tree

5 files changed

+61
-64
lines changed

5 files changed

+61
-64
lines changed

β€Žlib/internal/bootstrap/pre_execution.jsβ€Ž

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { Object, SafeWeakMap } = primordials;
55
const { getOptionValue } = require('internal/options');
66
const { Buffer } = require('buffer');
77
const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
8+
const path = require('path');
89

910
function prepareMainThreadExecution(expandArgv1 = false) {
1011
// Patch the process object with legacy properties and normalizations
@@ -437,11 +438,70 @@ function loadPreloadModules() {
437438
}
438439
}
439440

441+
function resolveMainPath(main) {
442+
const { toRealPath, Module: CJSModule } =
443+
require('internal/modules/cjs/loader');
444+
445+
// Note extension resolution for the main entry point can be deprecated in a
446+
// future major.
447+
let mainPath = CJSModule._findPath(path.resolve(main), null, true);
448+
if (!mainPath)
449+
return;
450+
451+
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
452+
if (!preserveSymlinksMain)
453+
mainPath = toRealPath(mainPath);
454+
455+
return mainPath;
456+
}
457+
458+
function shouldUseESMLoader(mainPath) {
459+
const experimentalModules = getOptionValue('--experimental-modules');
460+
if (!experimentalModules)
461+
return false;
462+
const userLoader = getOptionValue('--experimental-loader');
463+
if (userLoader)
464+
return true;
465+
// Determine the module format of the main
466+
if (mainPath && mainPath.endsWith('.mjs'))
467+
return true;
468+
if (!mainPath || mainPath.endsWith('.cjs'))
469+
return false;
470+
const { readPackageScope } = require('internal/modules/cjs/loader');
471+
const pkg = readPackageScope(mainPath);
472+
return pkg && pkg.data.type === 'module';
473+
}
474+
475+
function runMainESM(mainPath) {
476+
const esmLoader = require('internal/process/esm_loader');
477+
const { pathToFileURL } = require('internal/url');
478+
const { hasUncaughtExceptionCaptureCallback } =
479+
require('internal/process/execution');
480+
return (esmLoader.initializeLoader() || Promise.resolve()).then(() => {
481+
const main = path.isAbsolute(mainPath) ?
482+
pathToFileURL(mainPath).href : mainPath;
483+
return esmLoader.ESMLoader.import(main).catch((e) => {
484+
if (hasUncaughtExceptionCaptureCallback()) {
485+
process._fatalException(e);
486+
return;
487+
}
488+
internalBinding('errors').triggerUncaughtException(
489+
e,
490+
true /* fromPromise */
491+
);
492+
});
493+
});
494+
}
495+
496+
440497
module.exports = {
441498
patchProcessObject,
499+
resolveMainPath,
500+
runMainESM,
442501
setupCoverageHooks,
443502
setupWarningHandler,
444503
setupDebugEnv,
504+
shouldUseESMLoader,
445505
prepareMainThreadExecution,
446506
initializeDeprecations,
447507
initializeESMLoader,

β€Žlib/internal/modules/cjs/loader.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const {
7373
resolveMainPath,
7474
shouldUseESMLoader,
7575
runMainESM
76-
} = require('internal/process/main');
76+
} = require('internal/bootstrap/pre_execution');
7777
const pendingDeprecation = getOptionValue('--pending-deprecation');
7878

7979
module.exports = { wrapSafe, Module, toRealPath, readPackageScope };

β€Žlib/internal/process/main.jsβ€Ž

Lines changed: 0 additions & 61 deletions
This file was deleted.

β€Žnode.gypβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@
160160
'lib/internal/priority_queue.js',
161161
'lib/internal/process/esm_loader.js',
162162
'lib/internal/process/execution.js',
163-
'lib/internal/process/main.js',
164163
'lib/internal/process/main_thread_only.js',
165164
'lib/internal/process/per_thread.js',
166165
'lib/internal/process/policy.js',

β€Žtest/parallel/test-bootstrap-modules.jsβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const expectedModules = new Set([
5151
'NativeModule internal/options',
5252
'NativeModule internal/priority_queue',
5353
'NativeModule internal/process/execution',
54-
'NativeModule internal/process/main',
5554
'NativeModule internal/process/per_thread',
5655
'NativeModule internal/process/promises',
5756
'NativeModule internal/process/task_queues',

0 commit comments

Comments
Β (0)