Skip to content

Commit dfefd6b

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 63a2dbb commit dfefd6b

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

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

+23-8
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class AngularWebpackPlugin {
137137
// Example: module -> module__ivy_ngcc
138138
resolverFactoryHooks.resolveOptions
139139
.for('normal')
140-
.tap(PLUGIN_NAME, (resolveOptions: { mainFields: string[], plugins: unknown[] }) => {
140+
.tap(PLUGIN_NAME, (resolveOptions: { mainFields: string[]; plugins: unknown[] }) => {
141141
const originalMainFields = resolveOptions.mainFields;
142142
const ivyMainFields = originalMainFields.map((f) => `${f}_ivy_ngcc`);
143143

@@ -273,15 +273,12 @@ export class AngularWebpackPlugin {
273273
const currentUnused = new Set(
274274
allProgramFiles
275275
.filter((sourceFile) => !sourceFile.isDeclarationFile)
276-
.map((sourceFile) => sourceFile.fileName),
276+
.map((sourceFile) => normalizePath(sourceFile.fileName)),
277277
);
278278
Array.from(modules).forEach(({ resource }: compilation.Module & { resource?: string }) => {
279-
const sourceFile = resource && builder.getSourceFile(resource);
280-
if (!sourceFile) {
281-
return;
279+
if (resource) {
280+
this.markResourceUsed(normalizePath(resource), currentUnused);
282281
}
283-
284-
builder.getAllDependencies(sourceFile).forEach((dep) => currentUnused.delete(dep));
285282
});
286283
for (const unused of currentUnused) {
287284
if (previousUnused && previousUnused.has(unused)) {
@@ -301,6 +298,24 @@ export class AngularWebpackPlugin {
301298
});
302299
}
303300

301+
private markResourceUsed(
302+
normalizedResourcePath: string,
303+
currentUnused: Set<string>,
304+
): void {
305+
if (!currentUnused.has(normalizedResourcePath)) {
306+
return;
307+
}
308+
309+
currentUnused.delete(normalizedResourcePath);
310+
const dependencies = this.fileDependencies.get(normalizedResourcePath);
311+
if (!dependencies) {
312+
return;
313+
}
314+
for (const dependency of dependencies) {
315+
this.markResourceUsed(normalizePath(dependency), currentUnused);
316+
}
317+
}
318+
304319
private async rebuildRequiredFiles(
305320
modules: Iterable<compilation.Module>,
306321
compilation: WebpackCompilation,
@@ -599,7 +614,7 @@ export class AngularWebpackPlugin {
599614
}
600615

601616
const dependencies = [
602-
...this.fileDependencies.get(filePath) || [],
617+
...(this.fileDependencies.get(filePath) || []),
603618
...getExtraDependencies(sourceFile),
604619
].map(externalizePath);
605620

0 commit comments

Comments
 (0)