7
7
*/
8
8
import { Compiler , compilation } from 'webpack' ;
9
9
import { Budget , Type } from '../../browser/schema' ;
10
- import { Size , calculateBytes , calculateSizes } from '../utilities/bundle-calculator' ;
11
- import { formatSize } from '../utilities/stats' ;
12
-
13
- interface Thresholds {
14
- maximumWarning ?: number ;
15
- maximumError ?: number ;
16
- minimumWarning ?: number ;
17
- minimumError ?: number ;
18
- warningLow ?: number ;
19
- warningHigh ?: number ;
20
- errorLow ?: number ;
21
- errorHigh ?: number ;
22
- }
10
+ import { ThresholdSeverity , checkBudgets } from '../utilities/bundle-calculator' ;
23
11
24
12
export interface BundleBudgetPluginOptions {
25
13
budgets : Budget [ ] ;
@@ -35,97 +23,22 @@ export class BundleBudgetPlugin {
35
23
return ;
36
24
}
37
25
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
26
compiler . hooks . afterEmit . tap ( 'BundleBudgetPlugin' , ( compilation : compilation . Compilation ) => {
53
- const filteredBudgets = budgets . filter ( budget => budget . type !== Type . AnyComponentStyle ) ;
54
- this . runChecks ( filteredBudgets , compilation ) ;
27
+ this . runChecks ( budgets , compilation ) ;
55
28
} ) ;
56
29
}
57
30
58
- private checkMinimum ( threshold : number | undefined , size : Size , messages : string [ ] ) {
59
- if ( threshold && threshold > size . size ) {
60
- const sizeDifference = formatSize ( threshold - size . size ) ;
61
- messages . push ( `budgets, minimum exceeded for ${ size . label } . `
62
- + `Budget ${ formatSize ( threshold ) } was not reached by ${ sizeDifference } .` ) ;
63
- }
64
- }
65
-
66
- private checkMaximum ( threshold : number | undefined , size : Size , messages : string [ ] ) {
67
- if ( threshold && threshold < size . size ) {
68
- const sizeDifference = formatSize ( size . size - threshold ) ;
69
- messages . push ( `budgets, maximum exceeded for ${ size . label } . `
70
- + `Budget ${ formatSize ( threshold ) } was exceeded by ${ sizeDifference } .` ) ;
71
- }
72
- }
73
-
74
- private calculate ( budget : Budget ) : Thresholds {
75
- const thresholds : Thresholds = { } ;
76
- if ( budget . maximumWarning ) {
77
- thresholds . maximumWarning = calculateBytes ( budget . maximumWarning , budget . baseline , 1 ) ;
78
- }
79
-
80
- if ( budget . maximumError ) {
81
- thresholds . maximumError = calculateBytes ( budget . maximumError , budget . baseline , 1 ) ;
82
- }
83
-
84
- if ( budget . minimumWarning ) {
85
- thresholds . minimumWarning = calculateBytes ( budget . minimumWarning , budget . baseline , - 1 ) ;
86
- }
87
-
88
- if ( budget . minimumError ) {
89
- thresholds . minimumError = calculateBytes ( budget . minimumError , budget . baseline , - 1 ) ;
90
- }
91
-
92
- if ( budget . warning ) {
93
- thresholds . warningLow = calculateBytes ( budget . warning , budget . baseline , - 1 ) ;
94
- }
95
-
96
- if ( budget . warning ) {
97
- thresholds . warningHigh = calculateBytes ( budget . warning , budget . baseline , 1 ) ;
98
- }
99
-
100
- if ( budget . error ) {
101
- thresholds . errorLow = calculateBytes ( budget . error , budget . baseline , - 1 ) ;
102
- }
103
-
104
- if ( budget . error ) {
105
- thresholds . errorHigh = calculateBytes ( budget . error , budget . baseline , 1 ) ;
106
- }
107
-
108
- return thresholds ;
109
- }
110
-
111
31
private runChecks ( budgets : Budget [ ] , compilation : compilation . Compilation ) {
112
- budgets
113
- . map ( budget => ( {
114
- budget,
115
- thresholds : this . calculate ( budget ) ,
116
- sizes : calculateSizes ( budget , compilation ) ,
117
- } ) )
118
- . forEach ( budgetCheck => {
119
- budgetCheck . sizes . forEach ( size => {
120
- this . checkMaximum ( budgetCheck . thresholds . maximumWarning , size , compilation . warnings ) ;
121
- this . checkMaximum ( budgetCheck . thresholds . maximumError , size , compilation . errors ) ;
122
- this . checkMinimum ( budgetCheck . thresholds . minimumWarning , size , compilation . warnings ) ;
123
- this . checkMinimum ( budgetCheck . thresholds . minimumError , size , compilation . errors ) ;
124
- this . checkMinimum ( budgetCheck . thresholds . warningLow , size , compilation . warnings ) ;
125
- this . checkMaximum ( budgetCheck . thresholds . warningHigh , size , compilation . warnings ) ;
126
- this . checkMinimum ( budgetCheck . thresholds . errorLow , size , compilation . errors ) ;
127
- this . checkMaximum ( budgetCheck . thresholds . errorHigh , size , compilation . errors ) ;
128
- } ) ;
129
- } ) ;
32
+ const stats = compilation . getStats ( ) . toJson ( ) ;
33
+ for ( const { severity, message } of checkBudgets ( budgets , stats ) ) {
34
+ switch ( severity ) {
35
+ case ThresholdSeverity . Warning :
36
+ compilation . warnings . push ( `budgets: ${ message } ` ) ;
37
+ break ;
38
+ case ThresholdSeverity . Error :
39
+ compilation . errors . push ( `budgets: ${ message } ` ) ;
40
+ break ;
41
+ }
42
+ }
130
43
}
131
44
}
0 commit comments