Skip to content

Commit 38023fe

Browse files
valorkinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): fixed afterOptimizeChunkAssets is deprecated in webpack 5
1 parent defc8f5 commit 38023fe

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/any-component-style-budget-checker.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Compiler } from 'webpack';
1111
import { Budget, Type } from '../../browser/schema';
1212
import { ThresholdSeverity, calculateThresholds, checkThresholds } from '../../utils/bundle-calculator';
1313
import { addError, addWarning } from '../../utils/webpack-diagnostics';
14+
import { isWebpackFiveOrHigher } from '../../utils/webpack-version';
1415

1516
const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker';
1617

@@ -20,13 +21,14 @@ const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker';
2021
*/
2122
export class AnyComponentStyleBudgetChecker {
2223
private readonly budgets: Budget[];
24+
2325
constructor(budgets: Budget[]) {
2426
this.budgets = budgets.filter((budget) => budget.type === Type.AnyComponentStyle);
2527
}
2628

2729
apply(compiler: Compiler) {
2830
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
29-
compilation.hooks.afterOptimizeChunkAssets.tap(PLUGIN_NAME, () => {
31+
const afterOptimizeChunkAssets = () => {
3032
// In AOT compilations component styles get processed in child compilations.
3133
// tslint:disable-next-line: no-any
3234
const parentCompilation = (compilation.compiler as any).parentCompilation;
@@ -43,15 +45,15 @@ export class AnyComponentStyleBudgetChecker {
4345
];
4446

4547
const componentStyles = Object.keys(compilation.assets)
46-
.filter((name) => cssExtensions.includes(path.extname(name)))
47-
.map((name) => ({
48-
size: compilation.assets[name].size(),
49-
label: name,
50-
}));
48+
.filter((name) => cssExtensions.includes(path.extname(name)))
49+
.map((name) => ({
50+
size: compilation.assets[name].size(),
51+
label: name,
52+
}));
5153
const thresholds = flatMap(this.budgets, (budget) => calculateThresholds(budget));
5254

53-
for (const { size, label } of componentStyles) {
54-
for (const { severity, message } of checkThresholds(thresholds[Symbol.iterator](), size, label)) {
55+
for (const {size, label} of componentStyles) {
56+
for (const {severity, message} of checkThresholds(thresholds[Symbol.iterator](), size, label)) {
5557
switch (severity) {
5658
case ThresholdSeverity.Warning:
5759
addWarning(compilation, message);
@@ -65,7 +67,18 @@ export class AnyComponentStyleBudgetChecker {
6567
}
6668
}
6769
}
68-
});
70+
};
71+
72+
if (isWebpackFiveOrHigher()) {
73+
// webpack 5 migration "guide"
74+
// https://github.com/webpack/webpack/blob/07fc554bef5930f8577f91c91a8b81791fc29746/lib/Compilation.js#L535-L539
75+
// TODO_WEBPACK_5 const stage = Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE + 1;
76+
const stage = 101;
77+
// tslint:disable-next-line: no-any
78+
(compilation.hooks as any).processAssets.tap({name: PLUGIN_NAME, stage}, afterOptimizeChunkAssets);
79+
} else {
80+
compilation.hooks.afterOptimizeChunkAssets.tap(PLUGIN_NAME, afterOptimizeChunkAssets);
81+
}
6982
});
7083
}
7184
}

0 commit comments

Comments
 (0)