* given a relative path in pkg dir, * return a relative path in pkg dir, * mapped with the "map" object * * - Returning `undefined` means there is no browser mapping for this id * - Returning `false` means this id is explicitly externalized for browser
( relativePathInPkgDir: string, map: Record<string, string | false>, )
| 1121 | * - Returning `false` means this id is explicitly externalized for browser |
| 1122 | */ |
| 1123 | function mapWithBrowserField( |
| 1124 | relativePathInPkgDir: string, |
| 1125 | map: Record<string, string | false>, |
| 1126 | ): string | false | undefined { |
| 1127 | const normalizedPath = path.posix.normalize(relativePathInPkgDir) |
| 1128 | |
| 1129 | for (const key in map) { |
| 1130 | const normalizedKey = path.posix.normalize(key) |
| 1131 | if ( |
| 1132 | normalizedPath === normalizedKey || |
| 1133 | equalWithoutSuffix(normalizedPath, normalizedKey, '.js') || |
| 1134 | equalWithoutSuffix(normalizedPath, normalizedKey, '/index.js') |
| 1135 | ) { |
| 1136 | return map[key] |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | function equalWithoutSuffix(path: string, key: string, suffix: string) { |
| 1142 | return key.endsWith(suffix) && key.slice(0, -suffix.length) === path |
no test coverage detected