mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 03:56:02 +00:00

PR-URL: https://github.com/nodejs/node/pull/44710 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com> Co-authored-by: Michaël Zasso <targos@protonmail.com>
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const { createModuleLoader } = require('internal/modules/esm/loader');
|
|
const { getOptionValue } = require('internal/options');
|
|
const {
|
|
hasUncaughtExceptionCaptureCallback,
|
|
} = require('internal/process/execution');
|
|
const { pathToFileURL } = require('internal/url');
|
|
const { kEmptyObject } = require('internal/util');
|
|
|
|
let esmLoader;
|
|
|
|
module.exports = {
|
|
get esmLoader() {
|
|
return esmLoader ??= createModuleLoader(true);
|
|
},
|
|
async loadESM(callback) {
|
|
esmLoader ??= createModuleLoader(true);
|
|
try {
|
|
const userImports = getOptionValue('--import');
|
|
if (userImports.length > 0) {
|
|
let cwd;
|
|
try {
|
|
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
|
|
cwd = process.cwd() + '/';
|
|
} catch {
|
|
cwd = '/';
|
|
}
|
|
const parentURL = pathToFileURL(cwd).href;
|
|
await esmLoader.import(
|
|
userImports,
|
|
parentURL,
|
|
kEmptyObject,
|
|
);
|
|
}
|
|
await callback(esmLoader);
|
|
} catch (err) {
|
|
if (hasUncaughtExceptionCaptureCallback()) {
|
|
process._fatalException(err);
|
|
return;
|
|
}
|
|
internalBinding('errors').triggerUncaughtException(
|
|
err,
|
|
true, /* fromPromise */
|
|
);
|
|
}
|
|
},
|
|
};
|