1+ import webpack from 'webpack' ;
2+ import colorette from 'colorette' ;
3+
14export default function setupHooks ( context ) {
25 function invalid ( ) {
36 if ( context . state ) {
4- context . logger . info ( 'Compiling ...') ;
7+ context . logger . log ( 'Compilation starting ...') ;
58 }
69
710 // We are now in invalid state
@@ -20,47 +23,71 @@ export default function setupHooks(context) {
2023
2124 // Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
2225 process . nextTick ( ( ) => {
23- const { state , compiler , callbacks , logger } = context ;
26+ const { compiler , logger , state , callbacks } = context ;
2427
2528 // Check if still in valid state
2629 if ( ! state ) {
2730 return ;
2831 }
2932
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+ ) ,
4441 }
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+ }
4658
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+ }
4863
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 ) ;
5373 }
5474
55- logger . info ( message ) ;
56- } ;
75+ statsOptions . colors = Boolean ( colorette . options . enabled ) ;
76+ }
5777
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 ) ;
6491 }
6592
6693 // eslint-disable-next-line no-param-reassign
@@ -73,7 +100,10 @@ export default function setupHooks(context) {
73100 } ) ;
74101 }
75102
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 ) ;
79109}
0 commit comments