Skip to content

Commit 71d25a5

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

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

packages/ngtools/webpack/src/compiler_host.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,16 @@ 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));
124120
}
125121

126122
stat(path: string): Stats | null {
127123
const p = this.resolve(path);
128124

129-
let stats;
125+
let stats: virtualFs.Stats<Partial<Stats>> | null = null;
130126
try {
131127
stats = this._syncHost.stat(p);
132128
} catch { }
@@ -136,25 +132,31 @@ export class WebpackCompilerHost implements ts.CompilerHost {
136132
}
137133

138134
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),
135+
isFile: stats.isFile,
136+
isDirectory: stats.isDirectory,
137+
isBlockDevice: stats.isBlockDevice || (() => false),
138+
isCharacterDevice: stats.isCharacterDevice || (() => false),
139+
isFIFO: stats.isFIFO || (() => false),
140+
isSymbolicLink: stats.isSymbolicLink || (() => false),
141+
isSocket: stats.isSocket || (() => false),
142+
dev: stats.dev || dev,
143+
ino: stats.ino || Math.floor(Math.random() * 100000),
146144
mode: parseInt('777', 8),
147145
nlink: 1,
148146
uid: 0,
149147
gid: 0,
150148
rdev: 0,
149+
size: stats.size,
151150
blksize: 512,
152151
blocks: Math.ceil(stats.size / 512),
152+
atime: stats.atime,
153153
atimeMs: stats.atime.getTime(),
154+
mtime: stats.mtime,
154155
mtimeMs: stats.mtime.getTime(),
156+
ctime: stats.ctime,
155157
ctimeMs: stats.ctime.getTime(),
158+
birthtime: stats.birthtime,
156159
birthtimeMs: stats.birthtime.getTime(),
157-
...stats,
158160
};
159161
}
160162

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)