mirror of
https://github.com/nodejs/node.git
synced 2025-05-08 09:31:52 +00:00

PR-URL: https://github.com/nodejs/node/pull/49038 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve');
|
|
|
|
/**
|
|
* Generate a function to be used as import.meta.resolve for a particular module.
|
|
* @param {string} defaultParentURL The default base to use for resolution
|
|
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
|
|
* @returns {(specifier: string, parentURL?: string) => string} Function to assign to import.meta.resolve
|
|
*/
|
|
function createImportMetaResolve(defaultParentURL, loader) {
|
|
return function resolve(specifier, parentURL = defaultParentURL) {
|
|
let url;
|
|
try {
|
|
({ url } = loader.resolveSync(specifier, parentURL));
|
|
return url;
|
|
} catch (error) {
|
|
switch (error?.code) {
|
|
case 'ERR_UNSUPPORTED_DIR_IMPORT':
|
|
case 'ERR_MODULE_NOT_FOUND':
|
|
({ url } = error);
|
|
if (url) {
|
|
return url;
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create the `import.meta` object for a module.
|
|
* @param {object} meta
|
|
* @param {{url: string}} context
|
|
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
|
|
* @returns {{url: string, resolve?: Function}}
|
|
*/
|
|
function initializeImportMeta(meta, context, loader) {
|
|
const { url } = context;
|
|
|
|
// Alphabetical
|
|
if (experimentalImportMetaResolve && loader.allowImportMetaResolve) {
|
|
meta.resolve = createImportMetaResolve(url, loader);
|
|
}
|
|
|
|
meta.url = url;
|
|
|
|
return meta;
|
|
}
|
|
|
|
module.exports = {
|
|
initializeImportMeta,
|
|
};
|