Skip to content

Commit 5c5aebf

Browse files
committed
refactor(@ngtools/webpack): reduce webpack fs decorator system calls
1 parent 64d1524 commit 5c5aebf

File tree

2 files changed

+55
-60
lines changed

2 files changed

+55
-60
lines changed

packages/ngtools/webpack/src/compiler_host.ts

+38-28
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,46 @@ export class WebpackCompilerHost implements ts.CompilerHost {
113113
}
114114
}
115115

116-
readFileBuffer(fileName: string): Buffer | undefined {
116+
readFileBuffer(fileName: string): Buffer {
117117
const filePath = this.resolve(fileName);
118118

119-
try {
120-
return Buffer.from(this._syncHost.read(filePath));
121-
} catch {
122-
return undefined;
123-
}
119+
return Buffer.from(this._syncHost.read(filePath));
120+
}
121+
122+
private _makeStats(stats: virtualFs.Stats<Partial<Stats>>): Stats {
123+
return {
124+
isFile: () => stats.isFile(),
125+
isDirectory: () => stats.isDirectory(),
126+
isBlockDevice: () => stats.isBlockDevice && stats.isBlockDevice() || false,
127+
isCharacterDevice: () => stats.isCharacterDevice && stats.isCharacterDevice() || false,
128+
isFIFO: () => stats.isFIFO && stats.isFIFO() || false,
129+
isSymbolicLink: () => stats.isSymbolicLink && stats.isSymbolicLink() || false,
130+
isSocket: () => stats.isSocket && stats.isSocket() || false,
131+
dev: stats.dev === undefined ? dev : stats.dev,
132+
ino: stats.ino === undefined ? Math.floor(Math.random() * 100000) : stats.ino,
133+
mode: stats.mode === undefined ? parseInt('777', 8) : stats.mode,
134+
nlink: stats.nlink === undefined ? 1 : stats.nlink,
135+
uid: stats.uid || 0,
136+
gid: stats.gid || 0,
137+
rdev: stats.rdev || 0,
138+
size: stats.size,
139+
blksize: stats.blksize === undefined ? 512 : stats.blksize,
140+
blocks: stats.blocks === undefined ? Math.ceil(stats.size / 512) : stats.blocks,
141+
atime: stats.atime,
142+
atimeMs: stats.atime.getTime(),
143+
mtime: stats.mtime,
144+
mtimeMs: stats.mtime.getTime(),
145+
ctime: stats.ctime,
146+
ctimeMs: stats.ctime.getTime(),
147+
birthtime: stats.birthtime,
148+
birthtimeMs: stats.birthtime.getTime(),
149+
};
124150
}
125151

126152
stat(path: string): Stats | null {
127153
const p = this.resolve(path);
128154

129-
let stats;
155+
let stats: virtualFs.Stats<Partial<Stats>> | Stats | null = null;
130156
try {
131157
stats = this._syncHost.stat(p);
132158
} catch { }
@@ -135,27 +161,11 @@ export class WebpackCompilerHost implements ts.CompilerHost {
135161
return null;
136162
}
137163

138-
return {
139-
isBlockDevice: () => false,
140-
isCharacterDevice: () => false,
141-
isFIFO: () => false,
142-
isSymbolicLink: () => false,
143-
isSocket: () => false,
144-
dev,
145-
ino: Math.floor(Math.random() * 100000),
146-
mode: parseInt('777', 8),
147-
nlink: 1,
148-
uid: 0,
149-
gid: 0,
150-
rdev: 0,
151-
blksize: 512,
152-
blocks: Math.ceil(stats.size / 512),
153-
atimeMs: stats.atime.getTime(),
154-
mtimeMs: stats.mtime.getTime(),
155-
ctimeMs: stats.ctime.getTime(),
156-
birthtimeMs: stats.birthtime.getTime(),
157-
...stats,
158-
};
164+
if (stats instanceof Stats) {
165+
return stats;
166+
}
167+
168+
return this._makeStats(stats);
159169
}
160170

161171
directoryExists(directoryName: string): boolean {

packages/ngtools/webpack/src/virtual_file_system_decorator.ts

+17-32
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, getSystemPath, normalize } from '@angular-devkit/core';
8+
import { FileDoesNotExistException, Path, getSystemPath, normalize } from '@angular-devkit/core';
99
import { Stats } from 'fs';
1010
import { InputFileSystem } from 'webpack';
1111
import { WebpackCompilerHost } from './compiler_host';
@@ -21,33 +21,17 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
2121
private _webpackCompilerHost: WebpackCompilerHost,
2222
) { }
2323

24-
private _readFileSync(path: string): Buffer | null {
25-
if (this._webpackCompilerHost.fileExists(path)) {
26-
return this._webpackCompilerHost.readFileBuffer(path) || null;
27-
}
28-
29-
return null;
30-
}
31-
32-
private _statSync(path: string): Stats | null {
33-
if (this._webpackCompilerHost.fileExists(path)) {
34-
return this._webpackCompilerHost.stat(path);
35-
}
36-
37-
return null;
38-
}
39-
4024
getVirtualFilesPaths() {
4125
return this._webpackCompilerHost.getNgFactoryPaths();
4226
}
4327

4428
stat(path: string, callback: (err: Error, stats: Stats) => void): void {
45-
const result = this._statSync(path);
46-
if (result) {
29+
try {
30+
// tslint:disable-next-line:no-any
31+
callback(null as any, this._webpackCompilerHost.stat(path) as any);
32+
} catch (e) {
4733
// tslint:disable-next-line:no-any
48-
callback(null as any, result);
49-
} else {
50-
this._inputFileSystem.stat(path, callback);
34+
callback(e, undefined as any);
5135
}
5236
}
5337

@@ -57,12 +41,12 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
5741
}
5842

5943
readFile(path: string, callback: (err: Error, contents: Buffer) => void): void {
60-
const result = this._readFileSync(path);
61-
if (result) {
44+
try {
6245
// tslint:disable-next-line:no-any
63-
callback(null as any, result);
64-
} else {
65-
this._inputFileSystem.readFile(path, callback);
46+
callback(null as any, this._webpackCompilerHost.readFileBuffer(path));
47+
} catch (e) {
48+
// tslint:disable-next-line:no-any
49+
callback(e, undefined as any);
6650
}
6751
}
6852

@@ -76,9 +60,12 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
7660
}
7761

7862
statSync(path: string): Stats {
79-
const result = this._statSync(path);
63+
const stats = this._webpackCompilerHost.stat(path);
64+
if (stats === null) {
65+
throw new FileDoesNotExistException(path);
66+
}
8067

81-
return result || this._inputFileSystem.statSync(path);
68+
return stats;
8269
}
8370

8471
readdirSync(path: string): string[] {
@@ -87,9 +74,7 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
8774
}
8875

8976
readFileSync(path: string): Buffer {
90-
const result = this._readFileSync(path);
91-
92-
return result || this._inputFileSystem.readFileSync(path);
77+
return this._webpackCompilerHost.readFileBuffer(path);
9378
}
9479

9580
readJsonSync(path: string): string {

0 commit comments

Comments
 (0)