From 5429ccec2eeb2b40b5484cb68ecf4d29f4a517da Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 3 Oct 2018 15:21:24 -0400 Subject: [PATCH] refactor(@ngtools/webpack): reduce amount of filesystem calls --- packages/ngtools/webpack/src/compiler_host.ts | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/ngtools/webpack/src/compiler_host.ts b/packages/ngtools/webpack/src/compiler_host.ts index 2c0ea9b65c93..19565fa0652d 100644 --- a/packages/ngtools/webpack/src/compiler_host.ts +++ b/packages/ngtools/webpack/src/compiler_host.ts @@ -87,40 +87,50 @@ export class WebpackCompilerHost implements ts.CompilerHost { fileExists(fileName: string, delegate = true): boolean { const p = this.resolve(fileName); - const exists = this._syncHost.exists(p) && this._syncHost.isFile(p); - if (delegate) { - return exists; - } else { - const backend = new virtualFs.SyncDelegateHost( - (this._syncHost.delegate as virtualFs.CordHost).backend as virtualFs.Host, - ); + try { + const exists = this._syncHost.isFile(p); + if (delegate) { + return exists; + } else if (exists) { + const backend = new virtualFs.SyncDelegateHost( + (this._syncHost.delegate as virtualFs.CordHost).backend as virtualFs.Host, + ); + + return !backend.isFile(p); + } + } catch { } - return exists && !(backend.exists(p) && backend.isFile(p)); - } + return false; } readFile(fileName: string): string | undefined { const filePath = this.resolve(fileName); - if (!this._syncHost.exists(filePath) || !this._syncHost.isFile(filePath)) { + + try { + return virtualFs.fileBufferToString(this._syncHost.read(filePath)); + } catch { return undefined; } - - return virtualFs.fileBufferToString(this._syncHost.read(filePath)); } readFileBuffer(fileName: string): Buffer | undefined { const filePath = this.resolve(fileName); - if (!this._syncHost.exists(filePath) || !this._syncHost.isFile(filePath)) { + + try { + return Buffer.from(this._syncHost.read(filePath)); + } catch { return undefined; } - - return Buffer.from(this._syncHost.read(filePath)); } stat(path: string): Stats | null { const p = this.resolve(path); - const stats = this._syncHost.exists(p) && this._syncHost.stat(p); + let stats; + try { + stats = this._syncHost.stat(p); + } catch { } + if (!stats) { return null; } @@ -151,7 +161,11 @@ export class WebpackCompilerHost implements ts.CompilerHost { directoryExists(directoryName: string): boolean { const p = this.resolve(directoryName); - return this._syncHost.exists(p) && this._syncHost.isDirectory(p); + try { + return this._syncHost.isDirectory(p); + } catch { + return false; + } } getDirectories(path: string): string[] {