@@ -74,38 +74,77 @@ export class VirtualDirStats extends VirtualStats {
74
74
75
75
export class VirtualFileStats extends VirtualStats {
76
76
private _sourceFile : ts . SourceFile | null ;
77
- constructor ( _fileName : string , private _content : string ) {
77
+ private _content : string | null ;
78
+ private _bufferContent : virtualFs . FileBuffer | null ;
79
+
80
+ constructor ( _fileName : string ) {
78
81
super ( _fileName ) ;
79
82
}
80
83
81
- get content ( ) { return this . _content ; }
84
+ static createFromString ( _fileName : string , _content : string ) {
85
+ const stats = new VirtualFileStats ( _fileName ) ;
86
+ stats . content = _content ;
87
+
88
+ return stats ;
89
+ }
90
+
91
+ static createFromBuffer ( _fileName : string , _buffer : virtualFs . FileBuffer ) {
92
+ const stats = new VirtualFileStats ( _fileName ) ;
93
+ stats . bufferContent = _buffer ;
94
+
95
+ return stats ;
96
+ }
97
+
98
+ get content ( ) {
99
+ if ( ! this . _content && this . bufferContent ) {
100
+ this . _content = virtualFs . fileBufferToString ( this . bufferContent ) ;
101
+ }
102
+
103
+ return this . _content || "" ;
104
+ }
82
105
set content ( v : string ) {
83
106
this . _content = v ;
84
- this . _mtime = new Date ( ) ;
85
- this . _sourceFile = null ;
107
+ this . _bufferContent = null ;
108
+ this . resetMetadata ( ) ;
109
+ }
110
+
111
+ get bufferContent ( ) {
112
+ if ( ! this . _bufferContent && this . _content ) {
113
+ this . _bufferContent = virtualFs . stringToFileBuffer ( this . _content ) ;
114
+ }
115
+
116
+ return this . _bufferContent || virtualFs . stringToFileBuffer ( "" ) ;
86
117
}
118
+ set bufferContent ( buf : virtualFs . FileBuffer ) {
119
+ this . _bufferContent = buf ;
120
+ this . _content = null ;
121
+ this . resetMetadata ( ) ;
122
+ }
123
+
87
124
setSourceFile ( sourceFile : ts . SourceFile ) {
88
125
this . _sourceFile = sourceFile ;
89
126
}
90
127
getSourceFile ( languageVersion : ts . ScriptTarget , setParentNodes : boolean ) {
91
128
if ( ! this . _sourceFile ) {
92
- // console.log(this._path)
93
129
this . _sourceFile = ts . createSourceFile (
94
130
workaroundResolve ( this . _path ) ,
95
- this . _content ,
131
+ this . content ,
96
132
languageVersion ,
97
133
setParentNodes ) ;
98
134
}
99
-
100
135
return this . _sourceFile ;
101
136
}
102
137
138
+ private resetMetadata ( ) : void {
139
+ this . _mtime = new Date ( ) ;
140
+ this . _sourceFile = null ;
141
+ }
142
+
103
143
isFile ( ) { return true ; }
104
144
105
- get size ( ) { return this . _content . length ; }
145
+ get size ( ) { return this . content . length ; }
106
146
}
107
147
108
-
109
148
export class WebpackCompilerHost implements ts . CompilerHost {
110
149
private _syncHost : virtualFs . SyncDelegateHost ;
111
150
private _files : { [ path : string ] : VirtualFileStats | null } = Object . create ( null ) ;
@@ -149,8 +188,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
149
188
}
150
189
}
151
190
152
- private _setFileContent ( fileName : Path , content : string ) {
153
- this . _files [ fileName ] = new VirtualFileStats ( fileName , content ) ;
191
+ private _cacheFile ( fileName : string , stats : VirtualFileStats ) {
192
+ this . _files [ fileName ] = stats ;
154
193
155
194
let p = dirname ( fileName ) ;
156
195
while ( p && ! this . _directories [ p ] ) {
@@ -211,25 +250,39 @@ export class WebpackCompilerHost implements ts.CompilerHost {
211
250
}
212
251
213
252
readFile ( fileName : string ) : string | undefined {
253
+ const stats = this . findVirtualFile ( fileName ) ;
254
+ return stats && stats . content ;
255
+ }
256
+
257
+ readFileBuffer ( fileName : string ) : Buffer | undefined {
258
+ const stats = this . findVirtualFile ( fileName ) ;
259
+ if ( stats ) {
260
+ const buffer = Buffer . from ( stats . bufferContent ) ;
261
+ return buffer ;
262
+ }
263
+ }
264
+
265
+ private findVirtualFile ( fileName : string ) : VirtualFileStats | undefined {
214
266
const p = this . resolve ( fileName ) ;
215
267
216
268
const stats = this . _files [ p ] ;
217
- if ( ! stats ) {
218
- try {
219
- const result = virtualFs . fileBufferToString ( this . _syncHost . read ( p ) ) ;
220
- if ( result !== undefined ) {
221
- if ( this . _cache ) {
222
- this . _setFileContent ( p , result ) ;
223
- }
269
+ if ( stats ) {
270
+ return stats ;
271
+ }
272
+
273
+ try {
274
+ const fileBuffer = this . _syncHost . read ( p ) ;
275
+ if ( fileBuffer ) {
276
+ const stats = VirtualFileStats . createFromBuffer ( p , fileBuffer ) ;
277
+ if ( this . _cache ) {
278
+ this . _cacheFile ( p , stats ) ;
224
279
}
225
280
226
- return result ;
227
- } catch ( e ) {
228
- return undefined ;
281
+ return stats ;
229
282
}
283
+ } catch ( e ) {
284
+ return undefined ;
230
285
}
231
-
232
- return stats . content ;
233
286
}
234
287
235
288
stat ( path : string ) : VirtualStats | null {
@@ -344,7 +397,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
344
397
_sourceFiles ?: ReadonlyArray < ts . SourceFile > ,
345
398
) : void => {
346
399
const p = this . resolve ( fileName ) ;
347
- this . _setFileContent ( p , data ) ;
400
+ const stats = VirtualFileStats . createFromString ( p , data ) ;
401
+ this . _cacheFile ( p , stats ) ;
348
402
} ;
349
403
}
350
404
0 commit comments