(exports: unknown, subpath: string)
| 670 | } |
| 671 | |
| 672 | function getPackageExportMatch(exports: unknown, subpath: string): PackageExportMatch | undefined { |
| 673 | if (!isRecord(exports)) { |
| 674 | return subpath === "." ? { key: ".", value: exports, star: null, specificity: 1, prefixLength: 1 } : undefined |
| 675 | } |
| 676 | if (Object.hasOwn(exports, subpath)) { |
| 677 | return { |
| 678 | key: subpath, |
| 679 | value: exports[subpath], |
| 680 | star: null, |
| 681 | specificity: Number.MAX_SAFE_INTEGER, |
| 682 | prefixLength: subpath.length |
| 683 | } |
| 684 | } |
| 685 | const matches: Array<PackageExportMatch> = [] |
| 686 | for (const [key, value] of Object.entries(exports)) { |
| 687 | const starIndex = key.indexOf("*") |
| 688 | if (!key.startsWith(".") || starIndex === -1) { |
| 689 | continue |
| 690 | } |
| 691 | const prefix = key.slice(0, starIndex) |
| 692 | const suffix = key.slice(starIndex + 1) |
| 693 | if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix)) { |
| 694 | continue |
| 695 | } |
| 696 | matches.push({ |
| 697 | key, |
| 698 | value, |
| 699 | star: subpath.slice(prefix.length, subpath.length - suffix.length), |
| 700 | specificity: prefix.length + suffix.length, |
| 701 | prefixLength: prefix.length |
| 702 | }) |
| 703 | } |
| 704 | return matches.sort((a, b) => |
| 705 | b.specificity - a.specificity || b.prefixLength - a.prefixLength || b.key.length - a.key.length |
| 706 | )[0] |
| 707 | } |
| 708 | |
| 709 | function packageExportTargetMatches(value: unknown, star: string | null, expectedTarget: string): boolean { |
| 710 | if (typeof value === "string") { |
no test coverage detected