MCPcopy Index your code
hub / github.com/nodejs/node / createImportMetaResolve

Function createImportMetaResolve

lib/internal/modules/esm/loader.js:910–940  ·  view source on GitHub ↗

* This is only called from the native ImportMetaObjectInitialize function to set up import.meta.resolve * when import.meta.resolve is accessed for the first time in a module. * @param {ModuleLoader} loader The cascaded loader to use. Bound when this function gets passed to native land. * @param {

(loader, moduleURL)

Source from the content-addressed store, hash-verified

908 * @returns {function(string, URL['href']=): string} The import.meta.resolve function
909 */
910function createImportMetaResolve(loader, moduleURL) {
911 /**
912 * @param {string} specifier The module specifier to resolve.
913 * @param {URL['href']} [parentURL] Optional parent URL to resolve against. Ignored unless
914 * `--experimental-import-meta-resolve` is enabled.
915 * @returns {string}
916 */
917 return function resolve(specifier, parentURL) {
918 // The second argument is ignored unless --experimental-import-meta-resolve is enabled.
919 // Even then, if it's not provided, parentURL defaults to the url of the module accessing
920 // import.meta.resolve.
921 allowImportMetaResolveParentURL ??= getOptionValue('--experimental-import-meta-resolve');
922 parentURL = allowImportMetaResolveParentURL ? (parentURL ?? moduleURL) : moduleURL;
923
924 let url;
925 try {
926 ({ url } = loader.resolveSync(parentURL, { specifier, __proto__: null }));
927 return url;
928 } catch (error) {
929 switch (error?.code) {
930 case 'ERR_UNSUPPORTED_DIR_IMPORT':
931 case 'ERR_MODULE_NOT_FOUND':
932 ({ url } = error);
933 if (url) {
934 return url;
935 }
936 }
937 throw error;
938 }
939 };
940}
941
942let cascadedLoader;
943/**

Callers

nothing calls this directly

Calls 2

getOptionValueFunction · 0.85
resolveSyncMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…