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 63a2dbb commit dfefd6b
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/ngtools/webpack/src/ivy/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class AngularWebpackPlugin {
// Example: module -> module__ivy_ngcc
resolverFactoryHooks.resolveOptions
.for('normal')
.tap(PLUGIN_NAME, (resolveOptions: { mainFields: string[], plugins: unknown[] }) => {
.tap(PLUGIN_NAME, (resolveOptions: { mainFields: string[]; plugins: unknown[] }) => {
const originalMainFields = resolveOptions.mainFields;
const ivyMainFields = originalMainFields.map((f) => `${f}_ivy_ngcc`);

Expand Down Expand Up @@ -273,15 +273,12 @@ export class AngularWebpackPlugin {
const currentUnused = new Set(
allProgramFiles
.filter((sourceFile) => !sourceFile.isDeclarationFile)
.map((sourceFile) => sourceFile.fileName),
.map((sourceFile) => normalizePath(sourceFile.fileName)),
);
Array.from(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 @@ -301,6 +298,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 @@ -599,7 +614,7 @@ export class AngularWebpackPlugin {
}

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

Expand Down

0 comments on commit dfefd6b

Please sign in to comment.