import fs from "fs" ; import ts from "typescript" ; export function changeModuleResolutionHostToUseReadCache ( host ) { const { fileExists : originalFileExists , readFile : originalReadFile } = host ; const originalDirectoryExists = host . directoryExists ?? ts . sys . directoryExists ; const originalRealpath = host . realpath ?? fs . realpathSync ; const originalGetDirectories = host . getDirectories ?? ts . sys . getDirectories ; const cacheFileExists = new Map < string , boolean > ( ) ; host . fileExists = ( fileName ) : boolean => { if ( ! cacheFileExists . has ( fileName ) ) { cacheFileExists . set ( fileName , originalFileExists ( fileName ) ) ; } return cacheFileExists . get ( fileName ) ! ; } ; const cacheReadFile = new Map < string , string | undefined > ( ) ; host . readFile = ( fileName ) : string | undefined => { if ( ! cacheReadFile . has ( fileName ) ) { cacheReadFile . set ( fileName , originalReadFile ( fileName ) ) ; } return cacheReadFile . get ( fileName ) ; } ; const cacheDirectoryExists = new Map < string , boolean > ( ) ; host . directoryExists = ( directoryName ) : boolean => { if ( ! cacheDirectoryExists . has ( directoryName ) ) { cacheDirectoryExists . set ( directoryName , originalDirectoryExists ( directoryName ) ) ; } return cacheDirectoryExists . get ( directoryName ) ! ; } ; const cacheRealpath = new Map < string , string > ( ) ; host . realpath = ( path ) : string => { if ( ! cacheRealpath . has ( path ) ) { cacheRealpath . set ( path , originalRealpath ( path ) ) ; } return cacheRealpath . get ( path ) ! ; } ; const cacheGetDirectories = new Map < string , string [ ] > ( ) ; host . getDirectories = ( path ) : string [ ] => { if ( ! cacheGetDirectories . has ( path ) ) { cacheGetDirectories . set ( path , originalGetDirectories ( path ) ) ; } return cacheGetDirectories . get ( path ) ! ; } ; }