6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { Compiler , compilation } from 'webpack' ;
9
- import { Budget } from '../../browser/schema' ;
9
+ import { Budget , Type } from '../../browser/schema' ;
10
10
import { Size , calculateBytes , calculateSizes } from '../utilities/bundle-calculator' ;
11
11
import { formatSize } from '../utilities/stats' ;
12
12
@@ -30,33 +30,28 @@ export class BundleBudgetPlugin {
30
30
31
31
apply ( compiler : Compiler ) : void {
32
32
const { budgets } = this . options ;
33
- compiler . hooks . afterEmit . tap ( 'BundleBudgetPlugin' , ( compilation : compilation . Compilation ) => {
34
- if ( ! budgets || budgets . length === 0 ) {
35
- return ;
36
- }
37
33
38
- budgets . map ( budget => {
39
- const thresholds = this . calculate ( budget ) ;
40
-
41
- return {
42
- budget,
43
- thresholds,
44
- sizes : calculateSizes ( budget , compilation ) ,
45
- } ;
46
- } )
47
- . forEach ( budgetCheck => {
48
- budgetCheck . sizes . forEach ( size => {
49
- this . checkMaximum ( budgetCheck . thresholds . maximumWarning , size , compilation . warnings ) ;
50
- this . checkMaximum ( budgetCheck . thresholds . maximumError , size , compilation . errors ) ;
51
- this . checkMinimum ( budgetCheck . thresholds . minimumWarning , size , compilation . warnings ) ;
52
- this . checkMinimum ( budgetCheck . thresholds . minimumError , size , compilation . errors ) ;
53
- this . checkMinimum ( budgetCheck . thresholds . warningLow , size , compilation . warnings ) ;
54
- this . checkMaximum ( budgetCheck . thresholds . warningHigh , size , compilation . warnings ) ;
55
- this . checkMinimum ( budgetCheck . thresholds . errorLow , size , compilation . errors ) ;
56
- this . checkMaximum ( budgetCheck . thresholds . errorHigh , size , compilation . errors ) ;
57
- } ) ;
34
+ if ( ! budgets || budgets . length === 0 ) {
35
+ return ;
36
+ }
58
37
59
- } ) ;
38
+ compiler . hooks . compilation . tap ( 'BundleBudgetPlugin' , ( compilation : compilation . Compilation ) => {
39
+ compilation . hooks . afterOptimizeChunkAssets . tap ( 'BundleBudgetPlugin' , ( ) => {
40
+ // In AOT compilations component styles get processed in child compilations.
41
+ // tslint:disable-next-line: no-any
42
+ const parentCompilation = ( compilation . compiler as any ) . parentCompilation ;
43
+ if ( ! parentCompilation ) {
44
+ return ;
45
+ }
46
+
47
+ const filteredBudgets = budgets . filter ( budget => budget . type === Type . AnyComponentStyle ) ;
48
+ this . runChecks ( filteredBudgets , compilation ) ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ compiler . hooks . afterEmit . tap ( 'BundleBudgetPlugin' , ( compilation : compilation . Compilation ) => {
53
+ const filteredBudgets = budgets . filter ( budget => budget . type !== Type . AnyComponentStyle ) ;
54
+ this . runChecks ( filteredBudgets , compilation ) ;
60
55
} ) ;
61
56
}
62
57
@@ -116,4 +111,25 @@ export class BundleBudgetPlugin {
116
111
117
112
return thresholds ;
118
113
}
114
+
115
+ private runChecks ( budgets : Budget [ ] , compilation : compilation . Compilation ) {
116
+ budgets
117
+ . map ( budget => ( {
118
+ budget,
119
+ thresholds : this . calculate ( budget ) ,
120
+ sizes : calculateSizes ( budget , compilation ) ,
121
+ } ) )
122
+ . forEach ( budgetCheck => {
123
+ budgetCheck . sizes . forEach ( size => {
124
+ this . checkMaximum ( budgetCheck . thresholds . maximumWarning , size , compilation . warnings ) ;
125
+ this . checkMaximum ( budgetCheck . thresholds . maximumError , size , compilation . errors ) ;
126
+ this . checkMinimum ( budgetCheck . thresholds . minimumWarning , size , compilation . warnings ) ;
127
+ this . checkMinimum ( budgetCheck . thresholds . minimumError , size , compilation . errors ) ;
128
+ this . checkMinimum ( budgetCheck . thresholds . warningLow , size , compilation . warnings ) ;
129
+ this . checkMaximum ( budgetCheck . thresholds . warningHigh , size , compilation . warnings ) ;
130
+ this . checkMinimum ( budgetCheck . thresholds . errorLow , size , compilation . errors ) ;
131
+ this . checkMaximum ( budgetCheck . thresholds . errorHigh , size , compilation . errors ) ;
132
+ } ) ;
133
+ } ) ;
134
+ }
119
135
}
0 commit comments