mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
PR-URL: https://github.com/nodejs/node/pull/48779 Fixes: https://github.com/nodejs/node/issues/48778 Fixes: https://github.com/nodejs/node/issues/48516 Refs: https://github.com/nodejs/node/pull/46402 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me>
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
SafePromiseAllReturnVoid,
|
|
} = primordials;
|
|
|
|
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 SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
|
|
specifier,
|
|
parentURL,
|
|
kEmptyObject,
|
|
));
|
|
} else {
|
|
esmLoader.forceLoadHooks();
|
|
}
|
|
await callback(esmLoader);
|
|
} catch (err) {
|
|
if (hasUncaughtExceptionCaptureCallback()) {
|
|
process._fatalException(err);
|
|
return;
|
|
}
|
|
internalBinding('errors').triggerUncaughtException(
|
|
err,
|
|
true, /* fromPromise */
|
|
);
|
|
}
|
|
},
|
|
};
|