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

PR-URL: https://github.com/nodejs/node/pull/46629 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
42 lines
981 B
JavaScript
42 lines
981 B
JavaScript
'use strict';
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
const experimentalImportMetaResolve =
|
|
getOptionValue('--experimental-import-meta-resolve');
|
|
const {
|
|
PromisePrototypeThen,
|
|
PromiseReject,
|
|
} = primordials;
|
|
const asyncESM = require('internal/process/esm_loader');
|
|
|
|
function createImportMetaResolve(defaultParentUrl) {
|
|
return async function resolve(specifier, parentUrl = defaultParentUrl) {
|
|
return PromisePrototypeThen(
|
|
asyncESM.esmLoader.resolve(specifier, parentUrl),
|
|
({ url }) => url,
|
|
(error) => (
|
|
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
|
|
error.url : PromiseReject(error)),
|
|
);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {object} meta
|
|
* @param {{url: string}} context
|
|
*/
|
|
function initializeImportMeta(meta, context) {
|
|
const { url } = context;
|
|
|
|
// Alphabetical
|
|
if (experimentalImportMetaResolve) {
|
|
meta.resolve = createImportMetaResolve(url);
|
|
}
|
|
|
|
meta.url = url;
|
|
}
|
|
|
|
module.exports = {
|
|
initializeImportMeta
|
|
};
|