@@ -11,6 +11,7 @@ import { Compiler } from 'webpack';
11
11
import { Budget , Type } from '../../browser/schema' ;
12
12
import { ThresholdSeverity , calculateThresholds , checkThresholds } from '../../utils/bundle-calculator' ;
13
13
import { addError , addWarning } from '../../utils/webpack-diagnostics' ;
14
+ import { isWebpackFiveOrHigher } from '../../utils/webpack-version' ;
14
15
15
16
const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker' ;
16
17
@@ -20,13 +21,14 @@ const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker';
20
21
*/
21
22
export class AnyComponentStyleBudgetChecker {
22
23
private readonly budgets : Budget [ ] ;
24
+
23
25
constructor ( budgets : Budget [ ] ) {
24
26
this . budgets = budgets . filter ( ( budget ) => budget . type === Type . AnyComponentStyle ) ;
25
27
}
26
28
27
29
apply ( compiler : Compiler ) {
28
30
compiler . hooks . compilation . tap ( PLUGIN_NAME , ( compilation ) => {
29
- compilation . hooks . afterOptimizeChunkAssets . tap ( PLUGIN_NAME , ( ) => {
31
+ const afterOptimizeChunkAssets = ( ) => {
30
32
// In AOT compilations component styles get processed in child compilations.
31
33
// tslint:disable-next-line: no-any
32
34
const parentCompilation = ( compilation . compiler as any ) . parentCompilation ;
@@ -43,15 +45,15 @@ export class AnyComponentStyleBudgetChecker {
43
45
] ;
44
46
45
47
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
+ } ) ) ;
51
53
const thresholds = flatMap ( this . budgets , ( budget ) => calculateThresholds ( budget ) ) ;
52
54
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 ) ) {
55
57
switch ( severity ) {
56
58
case ThresholdSeverity . Warning :
57
59
addWarning ( compilation , message ) ;
@@ -65,7 +67,18 @@ export class AnyComponentStyleBudgetChecker {
65
67
}
66
68
}
67
69
}
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
+ }
69
82
} ) ;
70
83
}
71
84
}
0 commit comments