Skip to content

Commit 1fef334

Browse files
clydinjosephperrott
authored andcommitted
refactor(@ngtools/webpack): use Webpack 5 modified/removed file sets for changed list
Webpack 5 directly provides the set of modified and removed files. This feature allows for the removal of the file timestamp logic within the plugin that was previously used to generated the set of changed files on a rebuild. (cherry picked from commit 86754e4)
1 parent 222e118 commit 1fef334

File tree

2 files changed

+14
-37
lines changed

2 files changed

+14
-37
lines changed

packages/ngtools/webpack/src/ivy/cache.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,12 @@ import { normalizePath } from './paths';
1111
export class SourceFileCache extends Map<string, ts.SourceFile> {
1212
private readonly angularDiagnostics = new Map<ts.SourceFile, ts.Diagnostic[]>();
1313

14-
invalidate(
15-
fileTimestamps: Map<string, 'ignore' | number | { safeTime: number } | null>,
16-
buildTimestamp: number,
17-
): Set<string> {
18-
const changedFiles = new Set<string>();
19-
for (const [file, timeOrEntry] of fileTimestamps) {
20-
if (timeOrEntry === 'ignore') {
21-
continue;
22-
}
23-
24-
let time;
25-
if (typeof timeOrEntry === 'number') {
26-
time = timeOrEntry;
27-
} else if (timeOrEntry) {
28-
time = timeOrEntry.safeTime;
29-
}
30-
31-
if (!time || time >= buildTimestamp) {
32-
// Cache stores paths using the POSIX directory separator
33-
const normalizedFile = normalizePath(file);
34-
const sourceFile = this.get(normalizedFile);
35-
if (sourceFile) {
36-
this.delete(normalizedFile);
37-
this.angularDiagnostics.delete(sourceFile);
38-
}
39-
changedFiles.add(normalizedFile);
40-
}
14+
invalidate(file: string): void {
15+
const sourceFile = this.get(file);
16+
if (sourceFile) {
17+
this.delete(file);
18+
this.angularDiagnostics.delete(sourceFile);
4119
}
42-
43-
return changedFiles;
4420
}
4521

4622
updateAngularDiagnostics(sourceFile: ts.SourceFile, diagnostics: ts.Diagnostic[]): void {

packages/ngtools/webpack/src/ivy/plugin.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ export class AngularWebpackPlugin {
9595
private ngtscNextProgram?: NgtscProgram;
9696
private builder?: ts.EmitAndSemanticDiagnosticsBuilderProgram;
9797
private sourceFileCache?: SourceFileCache;
98-
private buildTimestamp!: number;
9998
private readonly fileDependencies = new Map<string, Set<string>>();
10099
private readonly requiredFilesToEmit = new Set<string>();
101100
private readonly requiredFilesToEmitCache = new Map<string, EmitFileResult | undefined>();
@@ -204,12 +203,15 @@ export class AngularWebpackPlugin {
204203
let cache = this.sourceFileCache;
205204
let changedFiles;
206205
if (cache) {
207-
// Invalidate existing cache based on compiler file timestamps
208-
changedFiles = cache.invalidate(compiler.fileTimestamps, this.buildTimestamp);
209-
210-
// Invalidate file dependencies of changed files
211-
for (const changedFile of changedFiles) {
212-
this.fileDependencies.delete(normalizePath(changedFile));
206+
changedFiles = new Set<string>();
207+
for (const changedFile of [...compiler.modifiedFiles, ...compiler.removedFiles]) {
208+
const normalizedChangedFile = normalizePath(changedFile);
209+
// Invalidate file dependencies
210+
this.fileDependencies.delete(normalizedChangedFile);
211+
// Invalidate existing cache
212+
cache.invalidate(normalizedChangedFile);
213+
214+
changedFiles.add(normalizedChangedFile);
213215
}
214216
} else {
215217
// Initialize a new cache
@@ -219,7 +221,6 @@ export class AngularWebpackPlugin {
219221
this.sourceFileCache = cache;
220222
}
221223
}
222-
this.buildTimestamp = Date.now();
223224
augmentHostWithCaching(host, cache);
224225

225226
const moduleResolutionCache = ts.createModuleResolutionCache(

0 commit comments

Comments
 (0)