mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 20:31:36 +00:00

Instead of calling into C++ each time we need to check the value of a command line option, cache the option map in a new `internal/options` module for faster access to the values in JS land. PR-URL: https://github.com/nodejs/node/pull/24091 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Refael Ackermann <refack@gmail.com>
65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
setImportModuleDynamicallyCallback,
|
|
setInitializeImportMetaObjectCallback,
|
|
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;
|
|
|
|
function initializeImportMetaObject(wrap, meta) {
|
|
if (callbackMap.has(wrap)) {
|
|
const { initializeImportMeta } = callbackMap.get(wrap);
|
|
if (initializeImportMeta !== undefined) {
|
|
initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function importModuleDynamicallyCallback(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();
|
|
}
|
|
|
|
setInitializeImportMetaObjectCallback(initializeImportMetaObject);
|
|
setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
|
|
|
|
let loaderResolve;
|
|
exports.loaderPromise = new Promise((resolve, reject) => {
|
|
loaderResolve = resolve;
|
|
});
|
|
|
|
exports.ESMLoader = undefined;
|
|
|
|
exports.setup = function() {
|
|
let ESMLoader = new Loader();
|
|
const loaderPromise = (async () => {
|
|
const userLoader = require('internal/options').getOptionValue('--loader');
|
|
if (userLoader) {
|
|
const hooks = await ESMLoader.import(
|
|
userLoader, pathToFileURL(`${process.cwd()}/`).href);
|
|
ESMLoader = new Loader();
|
|
ESMLoader.hook(hooks);
|
|
exports.ESMLoader = ESMLoader;
|
|
}
|
|
return ESMLoader;
|
|
})();
|
|
loaderResolve(loaderPromise);
|
|
|
|
exports.ESMLoader = ESMLoader;
|
|
};
|