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

Function resolvePackageEntry

packages/vite/src/node/plugins/resolve.ts:906–995  ·  packages/vite/src/node/plugins/resolve.ts::resolvePackageEntry
(
  id: string,
  { dir, data, setResolvedCache, getResolvedCache }: PackageData,
  options: InternalResolveOptions,
  externalize?: boolean,
)

Source from the content-addressed store, hash-verified

904}
905
906export function resolvePackageEntry(
907 id: string,
908 { dir, data, setResolvedCache, getResolvedCache }: PackageData,
909 options: InternalResolveOptions,
910 externalize?: boolean,
911): string | undefined {
912 const { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id)
913
914 const cached = getResolvedCache(class="st">'.', options)
915 if (cached) {
916 return cached + postfix
917 }
918
919 try {
920 let entryPoint: string | undefined
921
922 class="cm">// resolve exports field with highest priority
923 class="cm">// using https://github.com/lukeed/resolve.exports
924 if (data.exports) {
925 entryPoint = resolveExportsOrImports(
926 data,
927 class="st">'.',
928 options,
929 class="st">'exports',
930 externalize,
931 )
932 }
933
934 class="cm">// fallback to mainFields if still not resolved
935 if (!entryPoint) {
936 for (const field of options.mainFields) {
937 if (typeof data[field] === class="st">'string') {
938 entryPoint = data[field]
939 break
940 } else if (field === class="st">'browser') {
941 const browser = data[field]
942 if (isObject(browser) && browser[class="st">'.']) {
943 entryPoint = browser[class="st">'.']
944 break
945 }
946 }
947 }
948 }
949 entryPoint ||= data.main
950
951 class="cm">// try default entry when entry is not define
952 class="cm">// https://nodejs.org/api/modules.html#all-together
953 const entryPoints = entryPoint
954 ? [entryPoint]
955 : [class="st">'index.js', class="st">'index.json', class="st">'index.node']
956
957 for (let entry of entryPoints) {
958 class="cm">// make sure we don't get scripts when looking for sass
959 let skipPackageJson = false
960 if (
961 options.mainFields[0] === class="st">'sass' &&
962 !options.extensions.includes(path.extname(entry))
963 ) {

Callers 1

tryCleanFsResolveFunction · 0.85

Calls 8

splitFileAndPostfixFunction · 0.90
isObjectFunction · 0.90
getResolvedCacheFunction · 0.85
resolveExportsOrImportsFunction · 0.85
mapWithBrowserFieldFunction · 0.85
tryFsResolveFunction · 0.85
setResolvedCacheFunction · 0.85
packageEntryFailureFunction · 0.85

Tested by

no test coverage detected