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

Function tryOptimizedResolve

packages/vite/src/node/plugins/resolve.ts:851–904  ·  view source on GitHub ↗
(
  depsOptimizer: DepsOptimizer,
  id: string,
  importer?: string,
  preserveSymlinks?: boolean,
  packageCache?: PackageCache,
)

Source from the content-addressed store, hash-verified

849}
850
851export async function tryOptimizedResolve(
852 depsOptimizer: DepsOptimizer,
853 id: string,
854 importer?: string,
855 preserveSymlinks?: boolean,
856 packageCache?: PackageCache,
857): Promise<string | undefined> {
858 // TODO: we need to wait until scanning is done here as this function
859 // is used in the preAliasPlugin to decide if an aliased dep is optimized,
860 // and avoid replacing the bare import with the resolved path.
861 // We should be able to remove this in the future
862 await depsOptimizer.scanProcessing
863
864 const metadata = depsOptimizer.metadata
865
866 const depInfo = optimizedDepInfoFromId(metadata, id)
867 if (depInfo) {
868 return depsOptimizer.getOptimizedDepId(depInfo)
869 }
870
871 if (!importer) return
872
873 // further check if id is imported by nested dependency
874 let idPkgDir: string | undefined
875 const nestedIdMatch = `> ${id}`
876
877 for (const optimizedData of metadata.depInfoList) {
878 if (!optimizedData.src) continue // Ignore chunks
879
880 // check where "foo" is nested in "my-lib > foo"
881 if (!optimizedData.id.endsWith(nestedIdMatch)) continue
882
883 // lazily initialize idPkgDir
884 if (idPkgDir == null) {
885 const pkgName = getNpmPackageName(id)
886 if (!pkgName) break
887 idPkgDir = resolvePackageData(
888 pkgName,
889 importer,
890 preserveSymlinks,
891 packageCache,
892 )?.dir
893 // if still null, it likely means that this id isn't a dep for importer.
894 // break to bail early
895 if (idPkgDir == null) break
896 idPkgDir = normalizePath(idPkgDir)
897 }
898
899 // match by src to correctly identify if id belongs to nested dependency
900 if (optimizedData.src.startsWith(withTrailingSlash(idPkgDir))) {
901 return depsOptimizer.getOptimizedDepId(optimizedData)
902 }
903 }
904}
905
906export function resolvePackageEntry(
907 id: string,

Callers 2

resolveIdFunction · 0.90
handlerFunction · 0.85

Calls 5

optimizedDepInfoFromIdFunction · 0.90
getNpmPackageNameFunction · 0.90
resolvePackageDataFunction · 0.90
normalizePathFunction · 0.90
withTrailingSlashFunction · 0.90

Tested by

no test coverage detected