@@ -19,7 +19,6 @@ if (!process.env.WEBPACK_SERVE) {
19
19
class Server {
20
20
constructor ( options = { } , compiler ) {
21
21
// TODO: remove this after plugin support is published
22
-
23
22
if ( options . hooks ) {
24
23
const showDeprecationWarning = util . deprecate (
25
24
( ) => { } ,
@@ -128,25 +127,54 @@ class Server {
128
127
return path . resolve ( dir , "node_modules/.cache/webpack-dev-server" ) ;
129
128
}
130
129
131
- getCompilerConfigArray ( ) {
132
- const compilers = this . compiler . compilers
133
- ? this . compiler . compilers
134
- : [ this . compiler ] ;
130
+ getCompilerOptions ( ) {
131
+ if ( typeof this . compiler . compilers !== "undefined" ) {
132
+ if ( this . compiler . compilers . length === 1 ) {
133
+ return this . compiler . compilers [ 0 ] . options ;
134
+ }
135
+
136
+ // Configuration with the `devServer` options
137
+ const compilerWithDevServer = this . compiler . compilers . find (
138
+ ( config ) => config . options . devServer
139
+ ) ;
140
+
141
+ if ( compilerWithDevServer ) {
142
+ return compilerWithDevServer . options ;
143
+ }
144
+
145
+ // Configuration with `web` preset
146
+ const compilerWithWebPreset = this . compiler . compilers . find (
147
+ ( config ) =>
148
+ ( config . options . externalsPresets &&
149
+ config . options . externalsPresets . web ) ||
150
+ [
151
+ "web" ,
152
+ "webworker" ,
153
+ "electron-preload" ,
154
+ "electron-renderer" ,
155
+ "node-webkit" ,
156
+ // eslint-disable-next-line no-undefined
157
+ undefined ,
158
+ null ,
159
+ ] . includes ( config . options . target )
160
+ ) ;
161
+
162
+ if ( compilerWithWebPreset ) {
163
+ return compilerWithWebPreset . options ;
164
+ }
165
+
166
+ // Fallback
167
+ return this . compiler . compilers [ 0 ] . options ;
168
+ }
135
169
136
- return compilers . map ( ( compiler ) => compiler . options ) ;
170
+ return this . compiler . options ;
137
171
}
138
172
139
173
// eslint-disable-next-line class-methods-use-this
140
174
normalizeOptions ( options ) {
141
- // TODO: improve this to not use .find for compiler watchOptions
142
- const configArray = this . getCompilerConfigArray ( this . compiler ) ;
143
- const watchOptionsConfig = configArray . find (
144
- ( config ) => config . watch !== false && config . watchOptions
145
- ) ;
146
- const watchOptions = watchOptionsConfig
147
- ? watchOptionsConfig . watchOptions
148
- : { } ;
149
-
175
+ const compilerOptions = this . getCompilerOptions ( ) ;
176
+ // TODO remove `{}` after drop webpack v4 support
177
+ const watchOptions = compilerOptions . watchOptions || { } ;
150
178
const defaultOptionsForStatic = {
151
179
directory : path . join ( process . cwd ( ) , "public" ) ,
152
180
staticOptions : { } ,
@@ -216,14 +244,9 @@ class Server {
216
244
217
245
// Respect infrastructureLogging.level
218
246
if ( typeof options . client . logging === "undefined" ) {
219
- const configWithInfrastructureLogging =
220
- configArray . find (
221
- ( config ) =>
222
- config . infrastructureLogging && config . infrastructureLogging . level
223
- ) || configArray [ 0 ] ;
224
-
225
- options . client . logging =
226
- configWithInfrastructureLogging . infrastructureLogging . level ;
247
+ options . client . logging = compilerOptions . infrastructureLogging
248
+ ? compilerOptions . infrastructureLogging . level
249
+ : "info" ;
227
250
}
228
251
}
229
252
@@ -500,12 +523,11 @@ class Server {
500
523
return level ;
501
524
} ;
502
525
503
- const configWithDevServer =
504
- configArray . find ( ( config ) => config . devServer ) || configArray [ 0 ] ;
505
-
506
526
if ( typeof proxyOptions . logLevel === "undefined" ) {
507
527
proxyOptions . logLevel = getLogLevelForProxy (
508
- configWithDevServer . infrastructureLogging . level
528
+ compilerOptions . infrastructureLogging
529
+ ? compilerOptions . infrastructureLogging . level
530
+ : "info"
509
531
) ;
510
532
}
511
533
@@ -1270,13 +1292,13 @@ class Server {
1270
1292
logStatus ( ) {
1271
1293
const colorette = require ( "colorette" ) ;
1272
1294
1273
- const getColorsOption = ( configArray ) => {
1274
- const statsOption = this . getStatsOption ( configArray ) ;
1295
+ const getColorsOption = ( compilerOptions ) => {
1296
+ let colorsEnabled ;
1275
1297
1276
- let colorsEnabled = false ;
1277
-
1278
- if ( typeof statsOption === "object" && statsOption . colors ) {
1279
- colorsEnabled = statsOption . colors ;
1298
+ if ( typeof compilerOptions . stats . colors !== "undefined" ) {
1299
+ colorsEnabled = compilerOptions . stats ;
1300
+ } else {
1301
+ colorsEnabled = colorette . options . enabled ;
1280
1302
}
1281
1303
1282
1304
return colorsEnabled ;
@@ -1298,7 +1320,7 @@ class Server {
1298
1320
return msg ;
1299
1321
} ,
1300
1322
} ;
1301
- const useColor = getColorsOption ( this . getCompilerConfigArray ( ) ) ;
1323
+ const useColor = getColorsOption ( this . getCompilerOptions ( ) ) ;
1302
1324
1303
1325
if ( this . options . ipc ) {
1304
1326
this . logger . info ( `Project is running at: "${ this . server . address ( ) } "` ) ;
@@ -1578,31 +1600,12 @@ class Server {
1578
1600
}
1579
1601
}
1580
1602
1581
- // eslint-disable-next-line class-methods-use-this
1582
- getStatsOption ( configArray ) {
1583
- const isEmptyObject = ( val ) =>
1584
- typeof val === "object" && Object . keys ( val ) . length === 0 ;
1585
-
1586
- // in webpack@4 stats will not be defined if not provided,
1587
- // but in webpack@5 it will be an empty object
1588
- const statsConfig = configArray . find (
1589
- ( configuration ) =>
1590
- typeof configuration === "object" &&
1591
- configuration . stats &&
1592
- ! isEmptyObject ( configuration . stats )
1593
- ) ;
1594
-
1595
- return statsConfig ? statsConfig . stats : { } ;
1596
- }
1597
-
1598
1603
getStats ( statsObj ) {
1599
1604
const stats = Server . DEFAULT_STATS ;
1605
+ const compilerOptions = this . getCompilerOptions ( ) ;
1600
1606
1601
- const configArray = this . getCompilerConfigArray ( this . compiler ) ;
1602
- const statsOption = this . getStatsOption ( configArray ) ;
1603
-
1604
- if ( typeof statsOption === "object" && statsOption . warningsFilter ) {
1605
- stats . warningsFilter = statsOption . warningsFilter ;
1607
+ if ( compilerOptions . stats && compilerOptions . stats . warningsFilter ) {
1608
+ stats . warningsFilter = compilerOptions . stats . warningsFilter ;
1606
1609
}
1607
1610
1608
1611
return statsObj . toJson ( stats ) ;
0 commit comments