Skip to content

Commit b7c15a5

Browse files
clydinalan-agius4
authored andcommitted
perf(@ngtools/webpack): use precalculated dependencies in unused file check
This change uses the newly introduced precalculated file dependencies for each TypeScript file instead of querying TypeScript for the SourceFile's dependencies when performing the unused file check at the end of the build cycle. This change removes the need to recalculate the dependencies for each TypeScript file present in the Webpack compilation.
1 parent e326679 commit b7c15a5

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,12 @@ export class AngularWebpackPlugin {
276276
const currentUnused = new Set(
277277
allProgramFiles
278278
.filter((sourceFile) => !sourceFile.isDeclarationFile)
279-
.map((sourceFile) => sourceFile.fileName),
279+
.map((sourceFile) => normalizePath(sourceFile.fileName)),
280280
);
281281
modules.forEach(({ resource }: compilation.Module & { resource?: string }) => {
282-
const sourceFile = resource && builder.getSourceFile(resource);
283-
if (!sourceFile) {
284-
return;
282+
if (resource) {
283+
this.markResourceUsed(normalizePath(resource), currentUnused);
285284
}
286-
287-
builder.getAllDependencies(sourceFile).forEach((dep) => currentUnused.delete(dep));
288285
});
289286
for (const unused of currentUnused) {
290287
if (previousUnused && previousUnused.has(unused)) {
@@ -304,6 +301,24 @@ export class AngularWebpackPlugin {
304301
});
305302
}
306303

304+
private markResourceUsed(
305+
normalizedResourcePath: string,
306+
currentUnused: Set<string>,
307+
): void {
308+
if (!currentUnused.has(normalizedResourcePath)) {
309+
return;
310+
}
311+
312+
currentUnused.delete(normalizedResourcePath);
313+
const dependencies = this.fileDependencies.get(normalizedResourcePath);
314+
if (!dependencies) {
315+
return;
316+
}
317+
for (const dependency of dependencies) {
318+
this.markResourceUsed(normalizePath(dependency), currentUnused);
319+
}
320+
}
321+
307322
private async rebuildRequiredFiles(
308323
modules: Iterable<compilation.Module>,
309324
compilation: WebpackCompilation,
@@ -654,7 +669,7 @@ export class AngularWebpackPlugin {
654669
}
655670

656671
const dependencies = [
657-
...this.fileDependencies.get(filePath) || [],
672+
...(this.fileDependencies.get(filePath) || []),
658673
...getExtraDependencies(sourceFile),
659674
].map(externalizePath);
660675

0 commit comments

Comments
 (0)