Skip to content

Commit deea056

Browse files
clydinjosephperrott
authored andcommitted
refactor(@ngtools/webpack): reduce stored resource data between rebuilds
This change makes several changes to attempt to reduce retained memory within the resource loader. The first is the preemptive clearing of the resource loader's parent compilation after all modules are built. The second removes the cached sourcemaps for each resource which are not yet used by the system. And finally, the child compilations are no longer stored on the parent compilation and instead the dependencies, errors, and warnings are propagated to the parent compilation. (cherry picked from commit d92805e)
1 parent fa0fc45 commit deea056

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ export class AngularWebpackPlugin {
265265
// Rebuild any remaining AOT required modules
266266
await this.rebuildRequiredFiles(modules, compilation, fileEmitter);
267267

268+
// Clear out the Webpack compilation to avoid an extra retaining reference
269+
resourceLoader.clearParentCompilation();
270+
268271
// Analyze program for unused files
269272
if (compilation.errors.length > 0) {
270273
return;

packages/ngtools/webpack/src/resource_loader.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class WebpackResourceLoader {
4444
}
4545
}
4646

47+
clearParentCompilation() {
48+
this._parentCompilation = undefined;
49+
}
50+
4751
getModifiedResourceFiles() {
4852
return this.modifiedResources;
4953
}
@@ -172,6 +176,22 @@ export class WebpackResourceLoader {
172176
return;
173177
}
174178

179+
// Workaround to attempt to reduce memory usage of child compilations.
180+
// This removes the child compilation from the main compilation and manually propagates
181+
// all dependencies, warnings, and errors.
182+
const parent = childCompiler.parentCompilation;
183+
if (parent) {
184+
parent.children = parent.children.filter((child) => child !== childCompilation);
185+
186+
parent.fileDependencies.addAll(childCompilation.fileDependencies);
187+
parent.contextDependencies.addAll(childCompilation.contextDependencies);
188+
parent.missingDependencies.addAll(childCompilation.missingDependencies);
189+
parent.buildDependencies.addAll(childCompilation.buildDependencies);
190+
191+
parent.warnings.push(...childCompilation.warnings);
192+
parent.errors.push(...childCompilation.errors);
193+
}
194+
175195
// Save the dependencies for this resource.
176196
if (filePath) {
177197
this._fileDependencies.set(filePath, new Set(childCompilation.fileDependencies));
@@ -188,7 +208,6 @@ export class WebpackResourceLoader {
188208

189209
resolve({
190210
content: finalContent ?? '',
191-
map: finalMap,
192211
success: childCompilation.errors?.length === 0,
193212
});
194213
});

0 commit comments

Comments
 (0)