@@ -14,6 +14,25 @@ export default function setupHooks(context) {
1414 context . stats = undefined ;
1515 }
1616
17+ const statsForWebpack4 = webpack . Stats && webpack . Stats . presetToOptions ;
18+
19+ function normalizeStatsOptions ( statsOptions ) {
20+ if ( statsForWebpack4 ) {
21+ if ( typeof statsOptions === 'undefined' ) {
22+ // eslint-disable-next-line no-param-reassign
23+ statsOptions = { } ;
24+ } else if (
25+ typeof statsOptions === 'boolean' ||
26+ typeof statsOptions === 'string'
27+ ) {
28+ // eslint-disable-next-line no-param-reassign
29+ statsOptions = webpack . Stats . presetToOptions ( statsOptions ) ;
30+ }
31+ }
32+
33+ return statsOptions ;
34+ }
35+
1736 function done ( stats ) {
1837 // We are now on valid state
1938 // eslint-disable-next-line no-param-reassign
@@ -33,28 +52,14 @@ export default function setupHooks(context) {
3352 logger . log ( 'Compilation finished' ) ;
3453
3554 let statsOptions = compiler . compilers
36- ? {
37- children : compiler . compilers . map ( ( child ) =>
38- // eslint-disable-next-line no-undefined
39- child . options ? child . options . stats : undefined
40- ) ,
41- }
42- : compiler . options
43- ? compiler . options . stats
44- : // eslint-disable-next-line no-undefined
45- undefined ;
46-
47- const statsForWebpack4 = webpack . Stats && webpack . Stats . presetToOptions ;
55+ ? { children : compiler . compilers . map ( ( child ) => child . options . stats ) }
56+ : compiler . options . stats ;
4857
4958 if ( compiler . compilers ) {
5059 statsOptions . children = statsOptions . children . map (
5160 ( childStatsOptions ) => {
52- if ( statsForWebpack4 ) {
53- // eslint-disable-next-line no-param-reassign
54- childStatsOptions = webpack . Stats . presetToOptions (
55- childStatsOptions
56- ) ;
57- }
61+ // eslint-disable-next-line no-param-reassign
62+ childStatsOptions = normalizeStatsOptions ( childStatsOptions ) ;
5863
5964 if ( typeof childStatsOptions . colors === 'undefined' ) {
6065 // eslint-disable-next-line no-param-reassign
@@ -64,15 +69,12 @@ export default function setupHooks(context) {
6469 return childStatsOptions ;
6570 }
6671 ) ;
67- } else if (
68- typeof statsOptions . colors === 'undefined' ||
69- typeof statsOptions === 'string'
70- ) {
71- if ( statsForWebpack4 ) {
72- statsOptions = webpack . Stats . presetToOptions ( statsOptions ) ;
73- }
72+ } else {
73+ statsOptions = normalizeStatsOptions ( statsOptions ) ;
7474
75- statsOptions . colors = Boolean ( colorette . options . enabled ) ;
75+ if ( typeof statsOptions . colors === 'undefined' ) {
76+ statsOptions . colors = Boolean ( colorette . options . enabled ) ;
77+ }
7678 }
7779
7880 // TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
0 commit comments