2019-11-10 16:59:16 +08:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-07 11:08:09 +01:00
|
|
|
const {
|
2021-08-28 01:47:49 +02:00
|
|
|
ObjectCreate,
|
2020-11-07 11:08:09 +01:00
|
|
|
} = primordials;
|
2019-11-10 16:59:16 +08:00
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const path = require('path');
|
2022-12-19 19:35:57 +02:00
|
|
|
const { shouldUseESMLoader } = require('internal/modules/utils');
|
2019-11-10 16:59:16 +08:00
|
|
|
|
|
|
|
function resolveMainPath(main) {
|
|
|
|
// Note extension resolution for the main entry point can be deprecated in a
|
|
|
|
// future major.
|
|
|
|
// Module._findPath is monkey-patchable here.
|
2022-12-13 22:35:42 +01:00
|
|
|
const { Module, toRealPath } = require('internal/modules/cjs/loader');
|
2019-11-10 16:59:16 +08:00
|
|
|
let mainPath = Module._findPath(path.resolve(main), null, true);
|
|
|
|
if (!mainPath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
|
|
|
|
if (!preserveSymlinksMain)
|
|
|
|
mainPath = toRealPath(mainPath);
|
|
|
|
|
|
|
|
return mainPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
function runMainESM(mainPath) {
|
2021-08-25 22:55:14 +02:00
|
|
|
const { loadESM } = require('internal/process/esm_loader');
|
2019-11-10 16:59:16 +08:00
|
|
|
const { pathToFileURL } = require('internal/url');
|
2021-08-25 22:55:14 +02:00
|
|
|
|
|
|
|
handleMainPromise(loadESM((esmLoader) => {
|
2019-11-10 16:59:16 +08:00
|
|
|
const main = path.isAbsolute(mainPath) ?
|
2021-08-28 01:47:49 +02:00
|
|
|
pathToFileURL(mainPath).href : mainPath;
|
|
|
|
return esmLoader.import(main, undefined, ObjectCreate(null));
|
2020-08-06 02:56:17 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:16:43 +02:00
|
|
|
async function handleMainPromise(promise) {
|
2022-12-09 23:37:35 +01:00
|
|
|
const {
|
|
|
|
handleProcessExit,
|
|
|
|
} = require('internal/modules/esm/handle_process_exit');
|
2022-01-14 08:37:41 +08:00
|
|
|
process.on('exit', handleProcessExit);
|
2021-05-12 12:16:43 +02:00
|
|
|
try {
|
|
|
|
return await promise;
|
|
|
|
} finally {
|
2022-01-14 08:37:41 +08:00
|
|
|
process.off('exit', handleProcessExit);
|
2021-05-12 12:16:43 +02:00
|
|
|
}
|
2019-11-10 16:59:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For backwards compatibility, we have to run a bunch of
|
|
|
|
// monkey-patchable code that belongs to the CJS loader (exposed by
|
|
|
|
// `require('module')`) even when the entry point is ESM.
|
|
|
|
function executeUserEntryPoint(main = process.argv[1]) {
|
|
|
|
const resolvedMain = resolveMainPath(main);
|
|
|
|
const useESMLoader = shouldUseESMLoader(resolvedMain);
|
|
|
|
if (useESMLoader) {
|
|
|
|
runMainESM(resolvedMain || main);
|
|
|
|
} else {
|
|
|
|
// Module._load is the monkey-patchable CJS module loader.
|
2022-12-13 22:35:42 +01:00
|
|
|
const { Module } = require('internal/modules/cjs/loader');
|
2019-11-10 16:59:16 +08:00
|
|
|
Module._load(main, null, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-08-06 02:56:17 +02:00
|
|
|
executeUserEntryPoint,
|
|
|
|
handleMainPromise,
|
2019-11-10 16:59:16 +08:00
|
|
|
};
|