MCPcopy
hub / github.com/vitejs/vite / mapWithBrowserField

Function mapWithBrowserField

packages/vite/src/node/plugins/resolve.ts:1123–1139  ·  view source on GitHub ↗

* 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>,
)

Source from the content-addressed store, hash-verified

1121 * - Returning `false` means this id is explicitly externalized for browser
1122 */
1123function 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
1141function equalWithoutSuffix(path: string, key: string, suffix: string) {
1142 return key.endsWith(suffix) && key.slice(0, -suffix.length) === path

Callers 2

resolvePackageEntryFunction · 0.85
resolveDeepImportFunction · 0.85

Calls 1

equalWithoutSuffixFunction · 0.85

Tested by

no test coverage detected