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

Function packageImportsResolve

lib/internal/modules/esm/resolve.js:696–753  ·  view source on GitHub ↗

* Resolves the given import name for a package. * @param {string} name - The name of the import to resolve. * @param {string | URL | undefined} base - The base URL to resolve the import from. * @param {Set } conditions - An object containing the import conditions. * @throws {ERR_INVALID_M

(name, base, conditions)

Source from the content-addressed store, hash-verified

694 * @returns {URL} The resolved import URL.
695 */
696function packageImportsResolve(name, base, conditions) {
697 if (name === '#' || StringPrototypeEndsWith(name, '/')) {
698 const reason = 'is not a valid internal imports specifier name';
699 throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
700 }
701 let packageJSONUrl;
702 const packageConfig = packageJsonReader.getPackageScopeConfig(base);
703 if (packageConfig.exists) {
704 packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
705 const imports = packageConfig.imports;
706 if (imports) {
707 if (ObjectPrototypeHasOwnProperty(imports, name) &&
708 !StringPrototypeIncludes(name, '*')) {
709 const resolveResult = resolvePackageTarget(
710 packageJSONUrl, imports[name], '', name, base, false, true, false,
711 conditions,
712 );
713 if (resolveResult != null) {
714 return resolveResult;
715 }
716 } else {
717 let bestMatch = '';
718 let bestMatchSubpath;
719 const keys = ObjectGetOwnPropertyNames(imports);
720 for (let i = 0; i < keys.length; i++) {
721 const key = keys[i];
722 const patternIndex = StringPrototypeIndexOf(key, '*');
723 if (patternIndex !== -1 &&
724 StringPrototypeStartsWith(name,
725 StringPrototypeSlice(key, 0,
726 patternIndex))) {
727 const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
728 if (name.length >= key.length &&
729 StringPrototypeEndsWith(name, patternTrailer) &&
730 patternKeyCompare(bestMatch, key) === 1 &&
731 StringPrototypeLastIndexOf(key, '*') === patternIndex) {
732 bestMatch = key;
733 bestMatchSubpath = StringPrototypeSlice(
734 name, patternIndex, name.length - patternTrailer.length);
735 }
736 }
737 }
738
739 if (bestMatch) {
740 const target = imports[bestMatch];
741 const resolveResult = resolvePackageTarget(packageJSONUrl, target,
742 bestMatchSubpath,
743 bestMatch, base, true,
744 true, false, conditions);
745 if (resolveResult != null) {
746 return resolveResult;
747 }
748 }
749 }
750 }
751 }
752 throw importNotDefined(name, packageJSONUrl, base);
753}

Callers 2

moduleResolveFunction · 0.85
loader.jsFile · 0.85

Calls 6

fileURLToPathFunction · 0.85
resolvePackageTargetFunction · 0.85
patternKeyCompareFunction · 0.85
importNotDefinedFunction · 0.85
getPackageScopeConfigMethod · 0.80
pathToFileURLFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…