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

This patch: - Clarifies the dependency of the ESM loader initialization (`process.cwd()` and the value of `--loader`) in `node.js`. - Moves the initialization of the per-isolate `importModuleDynamically` and `initializeImportMetaObject` callbacks into `node.js` - Moves the initialization of the ESM loader into `prepareUserCodeExecution()` since it potentially involves execution of user code (similar to `--require` for CJS modules). PR-URL: https://github.com/nodejs/node/pull/25530 Reviewed-By: Gus Caplan <me@gus.host>
59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
callbackMap,
|
|
} = internalBinding('module_wrap');
|
|
|
|
const { pathToFileURL } = require('internal/url');
|
|
const Loader = require('internal/modules/esm/loader');
|
|
const {
|
|
wrapToModuleMap,
|
|
} = require('internal/vm/source_text_module');
|
|
const {
|
|
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
|
|
} = require('internal/errors').codes;
|
|
|
|
exports.initializeImportMetaObject = function(wrap, meta) {
|
|
if (callbackMap.has(wrap)) {
|
|
const { initializeImportMeta } = callbackMap.get(wrap);
|
|
if (initializeImportMeta !== undefined) {
|
|
initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap);
|
|
}
|
|
}
|
|
};
|
|
|
|
exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
|
|
if (callbackMap.has(wrap)) {
|
|
const { importModuleDynamically } = callbackMap.get(wrap);
|
|
if (importModuleDynamically !== undefined) {
|
|
return importModuleDynamically(
|
|
specifier, wrapToModuleMap.get(wrap) || wrap);
|
|
}
|
|
}
|
|
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
|
|
};
|
|
|
|
let loaderResolve;
|
|
exports.loaderPromise = new Promise((resolve, reject) => {
|
|
loaderResolve = resolve;
|
|
});
|
|
|
|
exports.ESMLoader = undefined;
|
|
|
|
exports.initializeLoader = function(cwd, userLoader) {
|
|
let ESMLoader = new Loader();
|
|
const loaderPromise = (async () => {
|
|
if (userLoader) {
|
|
const hooks = await ESMLoader.import(
|
|
userLoader, pathToFileURL(`${cwd}/`).href);
|
|
ESMLoader = new Loader();
|
|
ESMLoader.hook(hooks);
|
|
exports.ESMLoader = ESMLoader;
|
|
}
|
|
return ESMLoader;
|
|
})();
|
|
loaderResolve(loaderPromise);
|
|
|
|
exports.ESMLoader = ESMLoader;
|
|
};
|