import path from "path" ; import ts from "typescript" ; import { getExtensionFromPath } from "./extension" ; import { resolveModule } from "./resolve" ; import type { ResolverContext } from "./types" ; param context param searchDir param modulePath param extensions returns export function searchForModuleFile ( context , searchDir , modulePath , extensions ) { for ( const pext of [ ... context . platformExtensions , "" ] ) { for ( const fext of extensions ) { const p = path . join ( searchDir , `${ modulePath }${ pext }${ fext }` ) ; if ( context . host . fileExists ( p ) ) { return { resolvedFileName : p , extension : fext } ; } } } return undefined ; } param context param searchDir param modulePath param extensions returns export function findModuleFile ( context , searchDir , modulePath , extensions ) { const { host } = context ; const extension = getExtensionFromPath ( modulePath ) ; if ( extension && extensions . includes ( extension ) ) { const p = path . join ( searchDir , modulePath ) ; return host . fileExists ( p ) ? { resolvedFileName : p , extension } : undefined ; } let module = searchForModuleFile ( context , searchDir , modulePath , extensions ) ; if ( ! module ) { if ( extension === ts . Extension . Js || extension === ts . Extension . Jsx ) { const modulePathNoExt = modulePath . substring ( 0 , modulePath . length - extension . length ) ; module = searchForModuleFile ( context , searchDir , modulePathNoExt , extensions ) ; } } if ( ! module ) { const moduleDir = path . join ( searchDir , modulePath ) ; if ( host . directoryExists ( moduleDir ) ) { if ( host . fileExists ( path . join ( moduleDir , "package.json" ) ) ) { module = resolveModule ( context , moduleDir , undefined , extensions ) ; } else { module = findModuleFile ( context , moduleDir , "index" , extensions ) ; } } } return module ; }