node/lib/internal/process/esm_loader.js
João Lenon c2cd74453e
util: add getCwdSafe internal util fn
This function was first implemented in #46826, but at some point
of the PR implementation this fn was no longer related to the PR.

Refs: https://github.com/nodejs/node/pull/46826#discussion_r1225431178
PR-URL: https://github.com/nodejs/node/pull/48434
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-09-22 08:37:42 +00:00

47 lines
1.2 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 { kEmptyObject, getCWDURL } = 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) {
const parentURL = getCWDURL().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 */
);
}
},
};