@@ -31,12 +31,24 @@ export class ImportResolver {
3131 string ,
3232 CachedCompilerOptions
3333 > ( )
34+ private readonly configPathCache = new Map < string , string | undefined > ( )
3435 private readonly resolutionCache = new Map < string , string | undefined > ( )
35-
36- public constructor ( private readonly host : SourceHost ) { }
36+ private readonly hostDirectoryExists : ( directoryPath : string ) => boolean
37+ private readonly hostFileExists : ( filePath : string ) => boolean
38+ private readonly hostReadFile : ( filePath : string ) => string | undefined
39+
40+ public constructor ( private readonly host : SourceHost ) {
41+ this . hostDirectoryExists = ( directoryPath ) =>
42+ host . fileExists ( directoryPath ) || ts . sys . directoryExists ( directoryPath )
43+ this . hostFileExists = ( filePath ) =>
44+ host . fileExists ( filePath ) || ts . sys . fileExists ( filePath )
45+ this . hostReadFile = ( filePath ) =>
46+ host . readFile ( filePath ) ?? ts . sys . readFile ( filePath )
47+ }
3748
3849 public clear ( ) : void {
3950 this . compilerOptionsCache . clear ( )
51+ this . configPathCache . clear ( )
4052 this . resolutionCache . clear ( )
4153 }
4254
@@ -53,15 +65,11 @@ export class ImportResolver {
5365
5466 const compilerOptions = this . getCompilerOptions ( normalizedFromFilePath )
5567 const resolutionHost : ts . ModuleResolutionHost = {
56- directoryExists : ( directoryPath ) =>
57- this . host . fileExists ( directoryPath ) ||
58- ts . sys . directoryExists ( directoryPath ) ,
59- fileExists : ( filePath ) =>
60- this . host . fileExists ( filePath ) || ts . sys . fileExists ( filePath ) ,
68+ directoryExists : this . hostDirectoryExists ,
69+ fileExists : this . hostFileExists ,
6170 getCurrentDirectory : ( ) => path . dirname ( normalizedFromFilePath ) ,
6271 getDirectories : ts . sys . getDirectories ,
63- readFile : ( filePath ) =>
64- this . host . readFile ( filePath ) ?? ts . sys . readFile ( filePath ) ,
72+ readFile : this . hostReadFile ,
6573 realpath : ts . sys . realpath ,
6674 useCaseSensitiveFileNames : ts . sys . useCaseSensitiveFileNames ,
6775 }
@@ -84,7 +92,14 @@ export class ImportResolver {
8492 }
8593
8694 private getCompilerOptions ( filePath : string ) : ts . CompilerOptions {
87- const configPath = findNearestConfigFile ( path . dirname ( filePath ) )
95+ const directory = path . dirname ( filePath )
96+ let configPath : string | undefined
97+ if ( this . configPathCache . has ( directory ) ) {
98+ configPath = this . configPathCache . get ( directory )
99+ } else {
100+ configPath = findNearestConfigFile ( directory )
101+ this . configPathCache . set ( directory , configPath )
102+ }
88103 if ( ! configPath ) {
89104 return DEFAULT_COMPILER_OPTIONS
90105 }
0 commit comments