mirror of
https://github.com/nodejs/node.git
synced 2025-05-22 06:32:13 +00:00

At the last TC39 meeting, a new type of Module Records backed by JavaScript source called Dynamic Module Records was discussed, and it is now at Stage 1. Regardless of whether that proposal makes it all the way into the spec, SourceTextModule is indeed a more descriptive and accurate name for what this class represents. PR-URL: https://github.com/nodejs/node/pull/22007 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const { internalBinding } = require('internal/bootstrap/loaders');
|
|
const {
|
|
setImportModuleDynamicallyCallback,
|
|
setInitializeImportMetaObjectCallback
|
|
} = internalBinding('module_wrap');
|
|
|
|
const { getURLFromFilePath } = require('internal/url');
|
|
const Loader = require('internal/modules/esm/loader');
|
|
const path = require('path');
|
|
const { URL } = require('url');
|
|
const {
|
|
initImportMetaMap,
|
|
wrapToModuleMap
|
|
} = require('internal/vm/source_text_module');
|
|
|
|
function normalizeReferrerURL(referrer) {
|
|
if (typeof referrer === 'string' && path.isAbsolute(referrer)) {
|
|
return getURLFromFilePath(referrer).href;
|
|
}
|
|
return new URL(referrer).href;
|
|
}
|
|
|
|
function initializeImportMetaObject(wrap, meta) {
|
|
const vmModule = wrapToModuleMap.get(wrap);
|
|
if (vmModule === undefined) {
|
|
// This ModuleWrap belongs to the Loader.
|
|
meta.url = wrap.url;
|
|
} else {
|
|
const initializeImportMeta = initImportMetaMap.get(vmModule);
|
|
if (initializeImportMeta !== undefined) {
|
|
// This ModuleWrap belongs to vm.SourceTextModule,
|
|
// initializer callback was provided.
|
|
initializeImportMeta(meta, vmModule);
|
|
}
|
|
}
|
|
}
|
|
|
|
let loaderResolve;
|
|
exports.loaderPromise = new Promise((resolve, reject) => {
|
|
loaderResolve = resolve;
|
|
});
|
|
|
|
exports.ESMLoader = undefined;
|
|
|
|
exports.setup = function() {
|
|
setInitializeImportMetaObjectCallback(initializeImportMetaObject);
|
|
|
|
let ESMLoader = new Loader();
|
|
const loaderPromise = (async () => {
|
|
const userLoader = process.binding('config').userLoader;
|
|
if (userLoader) {
|
|
const hooks = await ESMLoader.import(
|
|
userLoader, getURLFromFilePath(`${process.cwd()}/`).href);
|
|
ESMLoader = new Loader();
|
|
ESMLoader.hook(hooks);
|
|
exports.ESMLoader = ESMLoader;
|
|
}
|
|
return ESMLoader;
|
|
})();
|
|
loaderResolve(loaderPromise);
|
|
|
|
setImportModuleDynamicallyCallback(async (referrer, specifier) => {
|
|
const loader = await loaderPromise;
|
|
return loader.import(specifier, normalizeReferrerURL(referrer));
|
|
});
|
|
|
|
exports.ESMLoader = ESMLoader;
|
|
};
|