Skip to content

Commit 3e03760

Browse files
committed
refactor(@ngtools/webpack): reduce amount of filesystem calls
1 parent dea0118 commit 3e03760

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

packages/ngtools/webpack/src/compiler_host.ts

+31-17
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,50 @@ export class WebpackCompilerHost implements ts.CompilerHost {
8787
fileExists(fileName: string, delegate = true): boolean {
8888
const p = this.resolve(fileName);
8989

90-
const exists = this._syncHost.exists(p) && this._syncHost.isFile(p);
91-
if (delegate) {
92-
return exists;
93-
} else {
94-
const backend = new virtualFs.SyncDelegateHost(
95-
(this._syncHost.delegate as virtualFs.CordHost).backend as virtualFs.Host,
96-
);
90+
try {
91+
const exists = this._syncHost.isFile(p);
92+
if (delegate) {
93+
return exists;
94+
} else if (exists) {
95+
const backend = new virtualFs.SyncDelegateHost(
96+
(this._syncHost.delegate as virtualFs.CordHost).backend as virtualFs.Host,
97+
);
98+
99+
return !backend.isFile(p);
100+
}
101+
} catch { }
97102

98-
return exists && !(backend.exists(p) && backend.isFile(p));
99-
}
103+
return false;
100104
}
101105

102106
readFile(fileName: string): string | undefined {
103107
const filePath = this.resolve(fileName);
104-
if (!this._syncHost.exists(filePath) || !this._syncHost.isFile(filePath)) {
108+
109+
try {
110+
return virtualFs.fileBufferToString(this._syncHost.read(filePath));
111+
} catch {
105112
return undefined;
106113
}
107-
108-
return virtualFs.fileBufferToString(this._syncHost.read(filePath));
109114
}
110115

111116
readFileBuffer(fileName: string): Buffer | undefined {
112117
const filePath = this.resolve(fileName);
113-
if (!this._syncHost.exists(filePath) || !this._syncHost.isFile(filePath)) {
118+
119+
try {
120+
return Buffer.from(this._syncHost.read(filePath));
121+
} catch {
114122
return undefined;
115123
}
116-
117-
return Buffer.from(this._syncHost.read(filePath));
118124
}
119125

120126
stat(path: string): Stats | null {
121127
const p = this.resolve(path);
122128

123-
const stats = this._syncHost.exists(p) && this._syncHost.stat(p);
129+
let stats;
130+
try {
131+
stats = this._syncHost.stat(p);
132+
} catch { }
133+
124134
if (!stats) {
125135
return null;
126136
}
@@ -151,7 +161,11 @@ export class WebpackCompilerHost implements ts.CompilerHost {
151161
directoryExists(directoryName: string): boolean {
152162
const p = this.resolve(directoryName);
153163

154-
return this._syncHost.exists(p) && this._syncHost.isDirectory(p);
164+
try {
165+
return this._syncHost.isDirectory(p);
166+
} catch {
167+
return false;
168+
}
155169
}
156170

157171
getDirectories(path: string): string[] {

0 commit comments

Comments
 (0)