@@ -74,38 +74,78 @@ export class VirtualDirStats extends VirtualStats {
7474
7575export class VirtualFileStats extends VirtualStats {
7676 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 ) {
7881 super ( _fileName ) ;
7982 }
8083
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+ }
82105 set content ( v : string ) {
83106 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 ( ) ;
86122 }
123+
87124 setSourceFile ( sourceFile : ts . SourceFile ) {
88125 this . _sourceFile = sourceFile ;
89126 }
90127 getSourceFile ( languageVersion : ts . ScriptTarget , setParentNodes : boolean ) {
91128 if ( ! this . _sourceFile ) {
92- // console.log(this._path)
93129 this . _sourceFile = ts . createSourceFile (
94130 workaroundResolve ( this . _path ) ,
95- this . _content ,
131+ this . content ,
96132 languageVersion ,
97133 setParentNodes ) ;
98134 }
99135
100136 return this . _sourceFile ;
101137 }
102138
139+ private resetMetadata ( ) : void {
140+ this . _mtime = new Date ( ) ;
141+ this . _sourceFile = null ;
142+ }
143+
103144 isFile ( ) { return true ; }
104145
105- get size ( ) { return this . _content . length ; }
146+ get size ( ) { return this . content . length ; }
106147}
107148
108-
109149export class WebpackCompilerHost implements ts . CompilerHost {
110150 private _syncHost : virtualFs . SyncDelegateHost ;
111151 private _files : { [ path : string ] : VirtualFileStats | null } = Object . create ( null ) ;
@@ -149,8 +189,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
149189 }
150190 }
151191
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 ;
154194
155195 let p = dirname ( fileName ) ;
156196 while ( p && ! this . _directories [ p ] ) {
@@ -211,25 +251,41 @@ export class WebpackCompilerHost implements ts.CompilerHost {
211251 }
212252
213253 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 {
214269 const p = this . resolve ( fileName ) ;
215270
216271 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 ) ;
224282 }
225283
226- return result ;
227- } catch ( e ) {
228- return undefined ;
284+ return stats ;
229285 }
286+ } catch ( e ) {
287+ return undefined ;
230288 }
231-
232- return stats . content ;
233289 }
234290
235291 stat ( path : string ) : VirtualStats | null {
@@ -344,7 +400,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
344400 _sourceFiles ?: ReadonlyArray < ts . SourceFile > ,
345401 ) : void => {
346402 const p = this . resolve ( fileName ) ;
347- this . _setFileContent ( p , data ) ;
403+ const stats = VirtualFileStats . createFromString ( p , data ) ;
404+ this . _cacheFile ( p , stats ) ;
348405 } ;
349406 }
350407
0 commit comments