Skip to content

refactor(@ngtools/webpack): reduce amount of filesystem calls #12461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions packages/ngtools/webpack/src/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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[] {
Expand Down