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

Function packageExportsResolve

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

* Resolves the exports of a package. * @param {URL} packageJSONUrl - The URL of the package.json file. * @param {string} packageSubpath - The subpath of the package to resolve. * @param {import('internal/modules/esm/package_config.js').PackageConfig} packageConfig - The package metadata. * @para

(
  packageJSONUrl, packageSubpath, packageConfig, base, conditions)

Source from the content-addressed store, hash-verified

587 * @returns {URL} - The resolved package target.
588 */
589function packageExportsResolve(
590 packageJSONUrl, packageSubpath, packageConfig, base, conditions) {
591 let { exports } = packageConfig;
592 if (isConditionalExportsMainSugar(exports, packageJSONUrl, base)) {
593 exports = { '.': exports };
594 }
595
596 if (ObjectPrototypeHasOwnProperty(exports, packageSubpath) &&
597 !StringPrototypeIncludes(packageSubpath, '*') &&
598 !StringPrototypeEndsWith(packageSubpath, '/')) {
599 const target = exports[packageSubpath];
600 const resolveResult = resolvePackageTarget(
601 packageJSONUrl, target, '', packageSubpath, base, false, false, false,
602 conditions,
603 );
604
605 if (resolveResult == null) {
606 throw exportsNotFound(packageSubpath, packageJSONUrl, base);
607 }
608
609 return resolveResult;
610 }
611
612 let bestMatch = '';
613 let bestMatchSubpath;
614 const keys = ObjectGetOwnPropertyNames(exports);
615 for (let i = 0; i < keys.length; i++) {
616 const key = keys[i];
617 const patternIndex = StringPrototypeIndexOf(key, '*');
618 if (patternIndex !== -1 &&
619 StringPrototypeStartsWith(packageSubpath,
620 StringPrototypeSlice(key, 0, patternIndex))) {
621 // When this reaches EOL, this can throw at the top of the whole function:
622 //
623 // if (StringPrototypeEndsWith(packageSubpath, '/'))
624 // throwInvalidSubpath(packageSubpath)
625 //
626 // To match "imports" and the spec.
627 if (StringPrototypeEndsWith(packageSubpath, '/')) {
628 emitTrailingSlashPatternDeprecation(packageSubpath, packageJSONUrl,
629 base);
630 }
631 const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
632 if (packageSubpath.length >= key.length &&
633 StringPrototypeEndsWith(packageSubpath, patternTrailer) &&
634 patternKeyCompare(bestMatch, key) === 1 &&
635 StringPrototypeLastIndexOf(key, '*') === patternIndex) {
636 bestMatch = key;
637 bestMatchSubpath = StringPrototypeSlice(
638 packageSubpath, patternIndex,
639 packageSubpath.length - patternTrailer.length);
640 }
641 }
642 }
643
644 if (bestMatch) {
645 const target = exports[bestMatch];
646 const resolveResult = resolvePackageTarget(

Callers 3

packageResolveFunction · 0.85
resolveExpansionFunction · 0.85
resolveExportsFunction · 0.85

Calls 5

resolvePackageTargetFunction · 0.85
exportsNotFoundFunction · 0.85
patternKeyCompareFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…