Skip to content

Commit

Permalink
perf(@ngtools/webpack): use precalculated dependencies in unused file…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
clydin authored and alan-agius4 committed Mar 12, 2021
1 parent e326679 commit b7c15a5
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/ngtools/webpack/src/ivy/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,12 @@ export class AngularWebpackPlugin {
const currentUnused = new Set(
allProgramFiles
.filter((sourceFile) => !sourceFile.isDeclarationFile)
.map((sourceFile) => sourceFile.fileName),
.map((sourceFile) => normalizePath(sourceFile.fileName)),
);
modules.forEach(({ resource }: compilation.Module & { resource?: string }) => {
const sourceFile = resource && builder.getSourceFile(resource);
if (!sourceFile) {
return;
if (resource) {
this.markResourceUsed(normalizePath(resource), currentUnused);
}

builder.getAllDependencies(sourceFile).forEach((dep) => currentUnused.delete(dep));
});
for (const unused of currentUnused) {
if (previousUnused && previousUnused.has(unused)) {
Expand All @@ -304,6 +301,24 @@ export class AngularWebpackPlugin {
});
}

private markResourceUsed(
normalizedResourcePath: string,
currentUnused: Set<string>,
): void {
if (!currentUnused.has(normalizedResourcePath)) {
return;
}

currentUnused.delete(normalizedResourcePath);
const dependencies = this.fileDependencies.get(normalizedResourcePath);
if (!dependencies) {
return;
}
for (const dependency of dependencies) {
this.markResourceUsed(normalizePath(dependency), currentUnused);
}
}

private async rebuildRequiredFiles(
modules: Iterable<compilation.Module>,
compilation: WebpackCompilation,
Expand Down Expand Up @@ -654,7 +669,7 @@ export class AngularWebpackPlugin {
}

const dependencies = [
...this.fileDependencies.get(filePath) || [],
...(this.fileDependencies.get(filePath) || []),
...getExtraDependencies(sourceFile),
].map(externalizePath);

Expand Down

0 comments on commit b7c15a5

Please sign in to comment.