@@ -74,38 +74,78 @@ 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 ( '' ) ;
117
+ }
118
+ set bufferContent ( buf : virtualFs . FileBuffer ) {
119
+ this . _bufferContent = buf ;
120
+ this . _content = null ;
121
+ this . resetMetadata ( ) ;
86
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
135
100
136
return this . _sourceFile ;
101
137
}
102
138
139
+ private resetMetadata ( ) : void {
140
+ this . _mtime = new Date ( ) ;
141
+ this . _sourceFile = null ;
142
+ }
143
+
103
144
isFile ( ) { return true ; }
104
145
105
- get size ( ) { return this . _content . length ; }
146
+ get size ( ) { return this . content . length ; }
106
147
}
107
148
108
-
109
149
export class WebpackCompilerHost implements ts . CompilerHost {
110
150
private _syncHost : virtualFs . SyncDelegateHost ;
111
151
private _files : { [ path : string ] : VirtualFileStats | null } = Object . create ( null ) ;
@@ -149,8 +189,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
149
189
}
150
190
}
151
191
152
- private _setFileContent ( fileName : Path , content : string ) {
153
- this . _files [ fileName ] = new VirtualFileStats ( fileName , content ) ;
192
+ private _cacheFile ( fileName : string , stats : VirtualFileStats ) {
193
+ this . _files [ fileName ] = stats ;
154
194
155
195
let p = dirname ( fileName ) ;
156
196
while ( p && ! this . _directories [ p ] ) {
@@ -211,25 +251,41 @@ export class WebpackCompilerHost implements ts.CompilerHost {
211
251
}
212
252
213
253
readFile ( fileName : string ) : string | undefined {
254
+ const stats = this . findVirtualFile ( fileName ) ;
255
+
256
+ return stats && stats . content ;
257
+ }
258
+
259
+ readFileBuffer ( fileName : string ) : Buffer | undefined {
260
+ const stats = this . findVirtualFile ( fileName ) ;
261
+ if ( stats ) {
262
+ const buffer = Buffer . from ( stats . bufferContent ) ;
263
+
264
+ return buffer ;
265
+ }
266
+ }
267
+
268
+ private findVirtualFile ( fileName : string ) : VirtualFileStats | undefined {
214
269
const p = this . resolve ( fileName ) ;
215
270
216
271
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
- }
272
+ if ( stats ) {
273
+ return stats ;
274
+ }
275
+
276
+ try {
277
+ const fileBuffer = this . _syncHost . read ( p ) ;
278
+ if ( fileBuffer ) {
279
+ const stats = VirtualFileStats . createFromBuffer ( p , fileBuffer ) ;
280
+ if ( this . _cache ) {
281
+ this . _cacheFile ( p , stats ) ;
224
282
}
225
283
226
- return result ;
227
- } catch ( e ) {
228
- return undefined ;
284
+ return stats ;
229
285
}
286
+ } catch ( e ) {
287
+ return undefined ;
230
288
}
231
-
232
- return stats . content ;
233
289
}
234
290
235
291
stat ( path : string ) : VirtualStats | null {
@@ -344,7 +400,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
344
400
_sourceFiles ?: ReadonlyArray < ts . SourceFile > ,
345
401
) : void => {
346
402
const p = this . resolve ( fileName ) ;
347
- this . _setFileContent ( p , data ) ;
403
+ const stats = VirtualFileStats . createFromString ( p , data ) ;
404
+ this . _cacheFile ( p , stats ) ;
348
405
} ;
349
406
}
350
407
0 commit comments