Skip to content

Commit 6fc84ff

Browse files
committed
perf(@ngtools/webpack): reduce source file and Webpack module iteration
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.
1 parent f62b042 commit 6fc84ff

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

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

+20-14
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,23 @@ export class AngularWebpackPlugin {
258258
resourceLoader,
259259
);
260260

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

265-
// Ensure all program files are considered part of the compilation and will be watched
266-
allProgramFiles.forEach((sourceFile) =>
267-
compilation.fileDependencies.add(sourceFile.fileName),
268-
);
264+
for (const sourceFile of builder.getSourceFiles()) {
265+
if (internalFiles?.has(sourceFile)) {
266+
continue;
267+
}
268+
269+
// Ensure all program files are considered part of the compilation and will be watched
270+
compilation.fileDependencies.add(sourceFile.fileName);
271+
272+
// Add all non-declaration files to the initial set of unused files. The set will be
273+
// analyzed and pruned after all Webpack modules are finished building.
274+
if (!sourceFile.isDeclarationFile) {
275+
currentUnused.add(normalizePath(sourceFile.fileName));
276+
}
277+
}
269278

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

282-
const currentUnused = new Set(
283-
allProgramFiles
284-
.filter((sourceFile) => !sourceFile.isDeclarationFile)
285-
.map((sourceFile) => normalizePath(sourceFile.fileName)),
286-
);
287-
Array.from(modules).forEach(({ resource }: Module & { resource?: string }) => {
291+
for (const webpackModule of modules) {
292+
const resource = (webpackModule as NormalModule).resource;
288293
if (resource) {
289294
this.markResourceUsed(normalizePath(resource), currentUnused);
290295
}
291-
});
296+
}
297+
292298
for (const unused of currentUnused) {
293299
if (previousUnused && previousUnused.has(unused)) {
294300
continue;

0 commit comments

Comments
 (0)