@@ -68,6 +68,10 @@ export class VirtualFileStats extends VirtualStats {
68
68
set content ( v : string ) {
69
69
this . _content = v ;
70
70
this . _mtime = new Date ( ) ;
71
+ this . _sourceFile = null ;
72
+ }
73
+ setSourceFile ( sourceFile : ts . SourceFile ) {
74
+ this . _sourceFile = sourceFile ;
71
75
}
72
76
getSourceFile ( languageVersion : ts . ScriptTarget , setParentNodes : boolean ) {
73
77
if ( ! this . _sourceFile ) {
@@ -96,6 +100,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
96
100
private _basePath : string ;
97
101
private _setParentNodes : boolean ;
98
102
103
+ private _cache : boolean = false ;
104
+
99
105
constructor ( private _options : ts . CompilerOptions , basePath : string ) {
100
106
this . _setParentNodes = true ;
101
107
this . _delegate = ts . createCompilerHost ( this . _options , this . _setParentNodes ) ;
@@ -129,6 +135,10 @@ export class WebpackCompilerHost implements ts.CompilerHost {
129
135
this . _changed = true ;
130
136
}
131
137
138
+ enableCaching ( ) {
139
+ this . _cache = true ;
140
+ }
141
+
132
142
populateWebpackResolver ( resolver : any ) {
133
143
const fs = resolver . fileSystem ;
134
144
if ( ! this . _changed ) {
@@ -156,21 +166,33 @@ export class WebpackCompilerHost implements ts.CompilerHost {
156
166
this . _changed = false ;
157
167
}
158
168
169
+ invalidate ( fileName : string ) : void {
170
+ this . _files [ fileName ] = null ;
171
+ }
172
+
159
173
fileExists ( fileName : string ) : boolean {
160
174
fileName = this . _resolve ( fileName ) ;
161
- return fileName in this . _files || this . _delegate . fileExists ( fileName ) ;
175
+ return this . _files [ fileName ] != null || this . _delegate . fileExists ( fileName ) ;
162
176
}
163
177
164
178
readFile ( fileName : string ) : string {
165
179
fileName = this . _resolve ( fileName ) ;
166
- return ( fileName in this . _files )
167
- ? this . _files [ fileName ] . content
168
- : this . _delegate . readFile ( fileName ) ;
180
+ if ( this . _files [ fileName ] == null ) {
181
+ const result = this . _delegate . readFile ( fileName ) ;
182
+ if ( result !== undefined && this . _cache ) {
183
+ this . _setFileContent ( fileName , result ) ;
184
+ return result ;
185
+ } else {
186
+ return result ;
187
+ }
188
+ }
189
+ return this . _files [ fileName ] . content ;
169
190
}
170
191
171
192
directoryExists ( directoryName : string ) : boolean {
172
193
directoryName = this . _resolve ( directoryName ) ;
173
- return ( directoryName in this . _directories ) || this . _delegate . directoryExists ( directoryName ) ;
194
+ return ( this . _directories [ directoryName ] != null )
195
+ || this . _delegate . directoryExists ( directoryName ) ;
174
196
}
175
197
176
198
getFiles ( path : string ) : string [ ] {
@@ -198,8 +220,11 @@ export class WebpackCompilerHost implements ts.CompilerHost {
198
220
getSourceFile ( fileName : string , languageVersion : ts . ScriptTarget , onError ?: OnErrorFn ) {
199
221
fileName = this . _resolve ( fileName ) ;
200
222
201
- if ( ! ( fileName in this . _files ) ) {
202
- return this . _delegate . getSourceFile ( fileName , languageVersion , onError ) ;
223
+ if ( this . _files [ fileName ] == null ) {
224
+ const content = this . readFile ( fileName ) ;
225
+ if ( ! this . _cache ) {
226
+ return ts . createSourceFile ( fileName , content , languageVersion , this . _setParentNodes ) ;
227
+ }
203
228
}
204
229
205
230
return this . _files [ fileName ] . getSourceFile ( languageVersion , this . _setParentNodes ) ;
0 commit comments