@@ -126,12 +126,47 @@ class Server {
126126 return path . resolve ( dir , "node_modules/.cache/webpack-dev-server" ) ;
127127 }
128128
129- getCompilerConfigArray ( ) {
130- const compilers = this . compiler . compilers
131- ? this . compiler . compilers
132- : [ this . compiler ] ;
129+ getCompilerOptions ( ) {
130+ if ( typeof this . compiler . compilers !== "undefined" ) {
131+ if ( this . compiler . compilers . length === 1 ) {
132+ return this . compiler . compilers [ 0 ] . options ;
133+ }
134+
135+ // Configuration with the `devServer` options
136+ const compilerWithDevServer = this . compiler . compilers . find (
137+ ( config ) => config . options . devServer
138+ ) ;
139+
140+ if ( compilerWithDevServer ) {
141+ return compilerWithDevServer . options ;
142+ }
143+
144+ // Configuration with `web` preset
145+ const compilerWithWebPreset = this . compiler . compilers . find (
146+ ( config ) =>
147+ ( config . options . externalsPresets &&
148+ config . options . externalsPresets . web ) ||
149+ [
150+ "web" ,
151+ "webworker" ,
152+ "electron-preload" ,
153+ "electron-renderer" ,
154+ "node-webkit" ,
155+ // eslint-disable-next-line no-undefined
156+ undefined ,
157+ null ,
158+ ] . includes ( config . options . target )
159+ ) ;
160+
161+ if ( compilerWithWebPreset ) {
162+ return compilerWithWebPreset . options ;
163+ }
164+
165+ // Fallback
166+ return this . compiler . compilers [ 0 ] . options ;
167+ }
133168
134- return compilers . map ( ( compiler ) => compiler . options ) ;
169+ return this . compiler . options ;
135170 }
136171
137172 // eslint-disable-next-line class-methods-use-this
@@ -142,15 +177,9 @@ class Server {
142177 this . logger = this . compiler . getInfrastructureLogger ( "webpack-dev-server" ) ;
143178 }
144179
145- // TODO: improve this to not use .find for compiler watchOptions
146- const configArray = this . getCompilerConfigArray ( ) ;
147- const watchOptionsConfig = configArray . find (
148- ( config ) => config . watch !== false && config . watchOptions
149- ) ;
150- const watchOptions = watchOptionsConfig
151- ? watchOptionsConfig . watchOptions
152- : { } ;
153-
180+ const compilerOptions = this . getCompilerOptions ( ) ;
181+ // TODO remove `{}` after drop webpack v4 support
182+ const watchOptions = compilerOptions . watchOptions || { } ;
154183 const defaultOptionsForStatic = {
155184 directory : path . join ( process . cwd ( ) , "public" ) ,
156185 staticOptions : { } ,
@@ -218,6 +247,13 @@ class Server {
218247 ...options . client . overlay ,
219248 } ;
220249 }
250+
251+ // Respect infrastructureLogging.level
252+ if ( typeof options . client . logging === "undefined" ) {
253+ options . client . logging = compilerOptions . infrastructureLogging
254+ ? compilerOptions . infrastructureLogging . level
255+ : "info" ;
256+ }
221257 }
222258
223259 if ( typeof options . compress === "undefined" ) {
@@ -493,12 +529,11 @@ class Server {
493529 return level ;
494530 } ;
495531
496- const configWithDevServer =
497- configArray . find ( ( config ) => config . devServer ) || configArray [ 0 ] ;
498-
499532 if ( typeof proxyOptions . logLevel === "undefined" ) {
500533 proxyOptions . logLevel = getLogLevelForProxy (
501- configWithDevServer . infrastructureLogging . level
534+ compilerOptions . infrastructureLogging
535+ ? compilerOptions . infrastructureLogging . level
536+ : "info"
502537 ) ;
503538 }
504539
@@ -707,6 +742,17 @@ class Server {
707742 this . app = new express ( ) ;
708743 }
709744
745+ getStats ( statsObj ) {
746+ const stats = Server . DEFAULT_STATS ;
747+ const compilerOptions = this . getCompilerOptions ( ) ;
748+
749+ if ( compilerOptions . stats && compilerOptions . stats . warningsFilter ) {
750+ stats . warningsFilter = compilerOptions . stats . warningsFilter ;
751+ }
752+
753+ return statsObj . toJson ( stats ) ;
754+ }
755+
710756 setupHooks ( ) {
711757 const addHooks = ( compiler ) => {
712758 compiler . hooks . invalid . tap ( "webpack-dev-server" , ( ) => {
@@ -1260,13 +1306,16 @@ class Server {
12601306 logStatus ( ) {
12611307 const colorette = require ( "colorette" ) ;
12621308
1263- const getColorsOption = ( configArray ) => {
1264- const statsOption = this . getStatsOption ( configArray ) ;
1309+ const getColorsOption = ( compilerOptions ) => {
1310+ let colorsEnabled ;
12651311
1266- let colorsEnabled = false ;
1267-
1268- if ( typeof statsOption === "object" && statsOption . colors ) {
1269- colorsEnabled = statsOption . colors ;
1312+ if (
1313+ compilerOptions . stats &&
1314+ typeof compilerOptions . stats . colors !== "undefined"
1315+ ) {
1316+ colorsEnabled = compilerOptions . stats ;
1317+ } else {
1318+ colorsEnabled = colorette . options . enabled ;
12701319 }
12711320
12721321 return colorsEnabled ;
@@ -1288,7 +1337,7 @@ class Server {
12881337 return msg ;
12891338 } ,
12901339 } ;
1291- const useColor = getColorsOption ( this . getCompilerConfigArray ( ) ) ;
1340+ const useColor = getColorsOption ( this . getCompilerOptions ( ) ) ;
12921341
12931342 if ( this . options . ipc ) {
12941343 this . logger . info ( `Project is running at: "${ this . server . address ( ) } "` ) ;
@@ -1420,36 +1469,6 @@ class Server {
14201469 }
14211470 }
14221471
1423- // eslint-disable-next-line class-methods-use-this
1424- getStatsOption ( configArray ) {
1425- const isEmptyObject = ( val ) =>
1426- typeof val === "object" && Object . keys ( val ) . length === 0 ;
1427-
1428- // in webpack@4 stats will not be defined if not provided,
1429- // but in webpack@5 it will be an empty object
1430- const statsConfig = configArray . find (
1431- ( configuration ) =>
1432- typeof configuration === "object" &&
1433- configuration . stats &&
1434- ! isEmptyObject ( configuration . stats )
1435- ) ;
1436-
1437- return statsConfig ? statsConfig . stats : { } ;
1438- }
1439-
1440- getStats ( statsObj ) {
1441- const stats = Server . DEFAULT_STATS ;
1442-
1443- const configArray = this . getCompilerConfigArray ( ) ;
1444- const statsOption = this . getStatsOption ( configArray ) ;
1445-
1446- if ( typeof statsOption === "object" && statsOption . warningsFilter ) {
1447- stats . warningsFilter = statsOption . warningsFilter ;
1448- }
1449-
1450- return statsObj . toJson ( stats ) ;
1451- }
1452-
14531472 setHeaders ( req , res , next ) {
14541473 let { headers } = this . options ;
14551474
0 commit comments