1
+ import webpack from 'webpack' ;
2
+ import colorette from 'colorette' ;
3
+
1
4
export default function setupHooks ( context ) {
2
5
function invalid ( ) {
3
6
if ( context . state ) {
4
- context . logger . info ( 'Compiling ...') ;
7
+ context . logger . log ( 'Compilation starting ...') ;
5
8
}
6
9
7
10
// We are now in invalid state
@@ -20,47 +23,71 @@ export default function setupHooks(context) {
20
23
21
24
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
22
25
process . nextTick ( ( ) => {
23
- const { state , compiler , callbacks , logger } = context ;
26
+ const { compiler , logger , state , callbacks } = context ;
24
27
25
28
// Check if still in valid state
26
29
if ( ! state ) {
27
30
return ;
28
31
}
29
32
30
- // Print webpack output
31
- const printStats = ( childCompiler , childStats ) => {
32
- const statsString = childStats . toString ( childCompiler . options . stats ) ;
33
- const name = childCompiler . options . name
34
- ? `Child "${ childCompiler . options . name } ": `
35
- : '' ;
36
-
37
- if ( statsString . length ) {
38
- if ( childStats . hasErrors ( ) ) {
39
- logger . error ( `${ name } ${ statsString } ` ) ;
40
- } else if ( childStats . hasWarnings ( ) ) {
41
- logger . warn ( `${ name } ${ statsString } ` ) ;
42
- } else {
43
- logger . info ( `${ name } ${ statsString } ` ) ;
33
+ logger . log ( 'Compilation finished' ) ;
34
+
35
+ 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
+ ) ,
44
41
}
45
- }
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 ;
48
+
49
+ if ( compiler . compilers ) {
50
+ statsOptions . children = statsOptions . children . map (
51
+ ( childStatsOptions ) => {
52
+ if ( statsForWebpack4 ) {
53
+ // eslint-disable-next-line no-param-reassign
54
+ childStatsOptions = webpack . Stats . presetToOptions (
55
+ childStatsOptions
56
+ ) ;
57
+ }
46
58
47
- let message = `${ name } Compiled successfully.` ;
59
+ if ( typeof childStatsOptions . colors === 'undefined' ) {
60
+ // eslint-disable-next-line no-param-reassign
61
+ childStatsOptions . colors = Boolean ( colorette . options . enabled ) ;
62
+ }
48
63
49
- if ( childStats . hasErrors ( ) ) {
50
- message = `${ name } Failed to compile.` ;
51
- } else if ( childStats . hasWarnings ( ) ) {
52
- message = `${ name } Compiled with warnings.` ;
64
+ return childStatsOptions ;
65
+ }
66
+ ) ;
67
+ } else if (
68
+ typeof statsOptions . colors === 'undefined' ||
69
+ typeof statsOptions === 'string'
70
+ ) {
71
+ if ( statsForWebpack4 ) {
72
+ statsOptions = webpack . Stats . presetToOptions ( statsOptions ) ;
53
73
}
54
74
55
- logger . info ( message ) ;
56
- } ;
75
+ statsOptions . colors = Boolean ( colorette . options . enabled ) ;
76
+ }
57
77
58
- if ( compiler . compilers ) {
59
- compiler . compilers . forEach ( ( compilerFromMultiCompileMode , index ) => {
60
- printStats ( compilerFromMultiCompileMode , stats . stats [ index ] ) ;
61
- } ) ;
62
- } else {
63
- printStats ( compiler , stats ) ;
78
+ // TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
79
+ if ( compiler . compilers && statsForWebpack4 ) {
80
+ statsOptions . colors = statsOptions . children . some (
81
+ ( child ) => child . colors
82
+ ) ;
83
+ }
84
+
85
+ const printedStats = stats . toString ( statsOptions ) ;
86
+
87
+ // Avoid extra empty line when `stats: 'none'`
88
+ if ( printedStats ) {
89
+ // eslint-disable-next-line no-console
90
+ console . log ( printedStats ) ;
64
91
}
65
92
66
93
// eslint-disable-next-line no-param-reassign
@@ -73,7 +100,10 @@ export default function setupHooks(context) {
73
100
} ) ;
74
101
}
75
102
76
- context . compiler . hooks . watchRun . tap ( 'DevMiddleware' , invalid ) ;
77
- context . compiler . hooks . invalid . tap ( 'DevMiddleware' , invalid ) ;
78
- context . compiler . hooks . done . tap ( 'DevMiddleware' , done ) ;
103
+ context . compiler . hooks . watchRun . tap ( 'webpack-dev-middleware' , invalid ) ;
104
+ context . compiler . hooks . invalid . tap ( 'webpack-dev-middleware' , invalid ) ;
105
+ ( context . compiler . webpack
106
+ ? context . compiler . hooks . afterDone
107
+ : context . compiler . hooks . done
108
+ ) . tap ( 'webpack-dev-middleware' , done ) ;
79
109
}
0 commit comments