@@ -65,7 +65,8 @@ export function makeServicesHost(
65
65
compiler . sys . fileExists ( filePathToCheck ) ||
66
66
readFile ( filePathToCheck ) !== undefined ;
67
67
68
- const moduleResolutionHost : ModuleResolutionHost = {
68
+ let clearCache : Action | null = null ;
69
+ let moduleResolutionHost : ModuleResolutionHost = {
69
70
fileExists,
70
71
readFile : readFileWithFallback ,
71
72
realpath : compiler . sys . realpath ,
@@ -74,7 +75,11 @@ export function makeServicesHost(
74
75
getDirectories : compiler . sys . getDirectories
75
76
} ;
76
77
77
- const clearCache = enableFileCaching ? addCache ( moduleResolutionHost ) : null ;
78
+ if ( enableFileCaching ) {
79
+ const cached = addCache ( moduleResolutionHost ) ;
80
+ clearCache = cached . clearCache ;
81
+ moduleResolutionHost = cached . moduleResolutionHost ;
82
+ }
78
83
79
84
// loader.context seems to work fine on Linux / Mac regardless causes problems for @types resolution on Windows for TypeScript < 2.3
80
85
const getCurrentDirectory = ( ) => loader . context ;
@@ -651,42 +656,29 @@ function populateDependencyGraphs(
651
656
} ) ;
652
657
}
653
658
654
- type CacheableFunction = Extract <
655
- keyof typescript . ModuleResolutionHost ,
656
- 'fileExists' | 'directoryExists' | 'realpath'
657
- > ;
658
- const cacheableFunctions : CacheableFunction [ ] = [
659
- 'fileExists' ,
660
- 'directoryExists' ,
661
- 'realpath'
662
- ] ;
663
-
664
- function addCache ( servicesHost : typescript . ModuleResolutionHost ) {
659
+ function addCache (
660
+ servicesHost : typescript . ModuleResolutionHost
661
+ ) : {
662
+ moduleResolutionHost : typescript . ModuleResolutionHost ;
663
+ clearCache : ( ) => void ;
664
+ } {
665
665
const clearCacheFunctions : Action [ ] = [ ] ;
666
-
667
- cacheableFunctions . forEach ( ( functionToCache : CacheableFunction ) => {
668
- const originalFunction = servicesHost [ functionToCache ] ;
669
- if ( originalFunction !== undefined ) {
670
- const cache = createCache < ReturnType < typeof originalFunction > > (
671
- originalFunction
672
- ) ;
673
- servicesHost [
674
- functionToCache
675
- ] = cache . getCached as typescript . ModuleResolutionHost [ CacheableFunction ] ;
676
- clearCacheFunctions . push ( cache . clear ) ;
677
- }
678
- } ) ;
679
-
680
- return ( ) => clearCacheFunctions . forEach ( clear => clear ( ) ) ;
681
- }
682
-
683
- function createCache < TOut > ( originalFunction : ( arg : string ) => TOut ) {
684
- const cache = new Map < string , TOut > ( ) ;
685
666
return {
686
- clear : ( ) => {
687
- cache . clear ( ) ;
667
+ moduleResolutionHost : {
668
+ ...servicesHost ,
669
+ fileExists : createCache ( servicesHost . fileExists ) ,
670
+ directoryExists :
671
+ servicesHost . directoryExists &&
672
+ createCache ( servicesHost . directoryExists ) ,
673
+ realpath : servicesHost . realpath && createCache ( servicesHost . realpath )
688
674
} ,
689
- getCached : ( arg : string ) => {
675
+ clearCache : ( ) => clearCacheFunctions . forEach ( clear => clear ( ) )
676
+ } ;
677
+
678
+ function createCache < TOut > ( originalFunction : ( arg : string ) => TOut ) {
679
+ const cache = new Map < string , TOut > ( ) ;
680
+ clearCacheFunctions . push ( ( ) => cache . clear ( ) ) ;
681
+ return function getCached ( arg : string ) {
690
682
let res = cache . get ( arg ) ;
691
683
if ( res !== undefined ) {
692
684
return res ;
@@ -695,6 +687,6 @@ function createCache<TOut>(originalFunction: (arg: string) => TOut) {
695
687
res = originalFunction ( arg ) ;
696
688
cache . set ( arg , res ) ;
697
689
return res ;
698
- }
699
- } ;
690
+ } ;
691
+ }
700
692
}
0 commit comments