Skip to content

Commit

Permalink
perf(@ngtools/webpack): reduce source file and Webpack module iteration
Browse files Browse the repository at this point in the history
During a build, the number of iterations over both the TypeScript program's source files and Webpack's modules has been reduced. Both of these collections can contain a significant number of elements especially in larger applications.

(cherry picked from commit 6fc84ff)
  • Loading branch information
clydin committed May 3, 2021
1 parent 057ba0c commit 1ec630f
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/ngtools/webpack/src/ivy/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,23 @@ export class AngularWebpackPlugin {
resourceLoader,
);

const allProgramFiles = builder
.getSourceFiles()
.filter((sourceFile) => !internalFiles?.has(sourceFile));
// Set of files used during the unused TypeScript file analysis
const currentUnused = new Set<string>();

// Ensure all program files are considered part of the compilation and will be watched
allProgramFiles.forEach((sourceFile) =>
compilation.fileDependencies.add(sourceFile.fileName),
);
for (const sourceFile of builder.getSourceFiles()) {
if (internalFiles?.has(sourceFile)) {
continue;
}

// Ensure all program files are considered part of the compilation and will be watched
compilation.fileDependencies.add(sourceFile.fileName);

// Add all non-declaration files to the initial set of unused files. The set will be
// analyzed and pruned after all Webpack modules are finished building.
if (!sourceFile.isDeclarationFile) {
currentUnused.add(normalizePath(sourceFile.fileName));
}
}

compilation.hooks.finishModules.tapPromise(PLUGIN_NAME, async (modules) => {
// Rebuild any remaining AOT required modules
Expand All @@ -279,16 +288,13 @@ export class AngularWebpackPlugin {
return;
}

const currentUnused = new Set(
allProgramFiles
.filter((sourceFile) => !sourceFile.isDeclarationFile)
.map((sourceFile) => normalizePath(sourceFile.fileName)),
);
Array.from(modules).forEach(({ resource }: Module & { resource?: string }) => {
for (const webpackModule of modules) {
const resource = (webpackModule as NormalModule).resource;
if (resource) {
this.markResourceUsed(normalizePath(resource), currentUnused);
}
});
}

for (const unused of currentUnused) {
if (previousUnused && previousUnused.has(unused)) {
continue;
Expand Down

0 comments on commit 1ec630f

Please sign in to comment.