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
|
|
|
StringPrototypeEndsWith,
|
|
|
|
} = primordials;
|
2019-11-10 16:59:16 +08:00
|
|
|
const CJSLoader = require('internal/modules/cjs/loader');
|
|
|
|
const { Module, toRealPath, readPackageScope } = CJSLoader;
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
function resolveMainPath(main) {
|
|
|
|
// Note extension resolution for the main entry point can be deprecated in a
|
|
|
|
// future major.
|
|
|
|
// Module._findPath is monkey-patchable here.
|
|
|
|
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 shouldUseESMLoader(mainPath) {
|
2022-05-04 17:51:12 +02:00
|
|
|
/**
|
|
|
|
* @type {string[]} userLoaders A list of custom loaders registered by the user
|
|
|
|
* (or an empty list when none have been registered).
|
|
|
|
*/
|
|
|
|
const userLoaders = getOptionValue('--experimental-loader');
|
2022-07-31 06:08:02 +03:00
|
|
|
/**
|
|
|
|
* @type {string[]} userImports A list of preloaded modules registered by the user
|
|
|
|
* (or an empty list when none have been registered).
|
|
|
|
*/
|
|
|
|
const userImports = getOptionValue('--import');
|
|
|
|
if (userLoaders.length > 0 || userImports.length > 0)
|
2019-11-10 16:59:16 +08:00
|
|
|
return true;
|
|
|
|
// Determine the module format of the main
|
2020-11-07 11:08:09 +01:00
|
|
|
if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs'))
|
2019-11-10 16:59:16 +08:00
|
|
|
return true;
|
2020-11-07 11:08:09 +01:00
|
|
|
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cjs'))
|
2019-11-10 16:59:16 +08:00
|
|
|
return false;
|
|
|
|
const pkg = readPackageScope(mainPath);
|
|
|
|
return pkg && pkg.data.type === 'module';
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
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
|
|
|
};
|