1'use strict'; 2 3const { getOptionValue } = require('internal/options'); 4const experimentalImportMetaResolve = 5 getOptionValue('--experimental-import-meta-resolve'); 6const { 7 PromisePrototypeThen, 8 PromiseReject, 9} = primordials; 10const asyncESM = require('internal/process/esm_loader'); 11 12function createImportMetaResolve(defaultParentUrl) { 13 return async function resolve(specifier, parentUrl = defaultParentUrl) { 14 return PromisePrototypeThen( 15 asyncESM.esmLoader.resolve(specifier, parentUrl), 16 ({ url }) => url, 17 (error) => ( 18 error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ? 19 error.url : PromiseReject(error)), 20 ); 21 }; 22} 23 24/** 25 * @param {object} meta 26 * @param {{url: string}} context 27 */ 28function initializeImportMeta(meta, context) { 29 const { url } = context; 30 31 // Alphabetical 32 if (experimentalImportMetaResolve) { 33 meta.resolve = createImportMetaResolve(url); 34 } 35 36 meta.url = url; 37} 38 39module.exports = { 40 initializeImportMeta, 41}; 42