@@ -126,12 +126,47 @@ class Server {
126
126
return path . resolve ( dir , "node_modules/.cache/webpack-dev-server" ) ;
127
127
}
128
128
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
+ }
133
168
134
- return compilers . map ( ( compiler ) => compiler . options ) ;
169
+ return this . compiler . options ;
135
170
}
136
171
137
172
// eslint-disable-next-line class-methods-use-this
@@ -142,15 +177,9 @@ class Server {
142
177
this . logger = this . compiler . getInfrastructureLogger ( "webpack-dev-server" ) ;
143
178
}
144
179
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 || { } ;
154
183
const defaultOptionsForStatic = {
155
184
directory : path . join ( process . cwd ( ) , "public" ) ,
156
185
staticOptions : { } ,
@@ -218,6 +247,13 @@ class Server {
218
247
...options . client . overlay ,
219
248
} ;
220
249
}
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
+ }
221
257
}
222
258
223
259
if ( typeof options . compress === "undefined" ) {
@@ -493,12 +529,11 @@ class Server {
493
529
return level ;
494
530
} ;
495
531
496
- const configWithDevServer =
497
- configArray . find ( ( config ) => config . devServer ) || configArray [ 0 ] ;
498
-
499
532
if ( typeof proxyOptions . logLevel === "undefined" ) {
500
533
proxyOptions . logLevel = getLogLevelForProxy (
501
- configWithDevServer . infrastructureLogging . level
534
+ compilerOptions . infrastructureLogging
535
+ ? compilerOptions . infrastructureLogging . level
536
+ : "info"
502
537
) ;
503
538
}
504
539
@@ -707,6 +742,17 @@ class Server {
707
742
this . app = new express ( ) ;
708
743
}
709
744
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
+
710
756
setupHooks ( ) {
711
757
const addHooks = ( compiler ) => {
712
758
compiler . hooks . invalid . tap ( "webpack-dev-server" , ( ) => {
@@ -1260,13 +1306,16 @@ class Server {
1260
1306
logStatus ( ) {
1261
1307
const colorette = require ( "colorette" ) ;
1262
1308
1263
- const getColorsOption = ( configArray ) => {
1264
- const statsOption = this . getStatsOption ( configArray ) ;
1309
+ const getColorsOption = ( compilerOptions ) => {
1310
+ let colorsEnabled ;
1265
1311
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 ;
1270
1319
}
1271
1320
1272
1321
return colorsEnabled ;
@@ -1288,7 +1337,7 @@ class Server {
1288
1337
return msg ;
1289
1338
} ,
1290
1339
} ;
1291
- const useColor = getColorsOption ( this . getCompilerConfigArray ( ) ) ;
1340
+ const useColor = getColorsOption ( this . getCompilerOptions ( ) ) ;
1292
1341
1293
1342
if ( this . options . ipc ) {
1294
1343
this . logger . info ( `Project is running at: "${ this . server . address ( ) } "` ) ;
@@ -1420,36 +1469,6 @@ class Server {
1420
1469
}
1421
1470
}
1422
1471
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
-
1453
1472
setHeaders ( req , res , next ) {
1454
1473
let { headers } = this . options ;
1455
1474
0 commit comments