1
1
// eslint-disable-next-line node/no-extraneous-import
2
2
import type { Compiler , cli } from "webpack" ;
3
- import { devServerOptionsType } from "./types " ;
3
+ import { IWebpackCLI , WebpackDevServerOptions } from "webpack-cli " ;
4
4
5
5
const WEBPACK_PACKAGE = process . env . WEBPACK_PACKAGE || "webpack" ;
6
6
const WEBPACK_DEV_SERVER_PACKAGE = process . env . WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server" ;
7
7
8
8
type Problem = NonNullable < ReturnType < typeof cli [ "processArguments" ] > > [ 0 ] ;
9
-
9
+ type PublicPath = WebpackDevServerOptions [ "output" ] [ "publicPath" ] ;
10
10
class ServeCommand {
11
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
12
- async apply ( cli : any ) : Promise < void > {
11
+ async apply ( cli : IWebpackCLI ) : Promise < void > {
13
12
const loadDevServerOptions = ( ) => {
14
13
// TODO simplify this after drop webpack v4 and webpack-dev-server v3
15
14
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -188,8 +187,7 @@ class ServeCommand {
188
187
process . exit ( 2 ) ;
189
188
}
190
189
191
- const compilers =
192
- typeof compiler . compilers !== "undefined" ? compiler . compilers : [ compiler ] ;
190
+ const compilers = cli . isMultipleCompiler ( compiler ) ? compiler . compilers : [ compiler ] ;
193
191
const possibleCompilers = compilers . filter (
194
192
( compiler : Compiler ) => compiler . options . devServer ,
195
193
) ;
@@ -199,7 +197,7 @@ class ServeCommand {
199
197
const usedPorts : number [ ] = [ ] ;
200
198
201
199
for ( const compilerForDevServer of compilersForDevServer ) {
202
- let devServerOptions : devServerOptionsType ;
200
+ let devServerOptions : WebpackDevServerOptions ;
203
201
204
202
if ( isNewDevServerCLIAPI ) {
205
203
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -260,18 +258,23 @@ class ServeCommand {
260
258
process . exit ( 2 ) ;
261
259
}
262
260
263
- devServerOptions = result ;
261
+ devServerOptions = result as WebpackDevServerOptions ;
264
262
} else {
265
263
// TODO remove in the next major release
266
264
const mergeOptions = (
267
- devServerOptions : devServerOptionsType ,
268
- devServerCliOptions : devServerOptionsType ,
269
- ) : devServerOptionsType => {
265
+ devServerOptions : Partial < WebpackDevServerOptions > ,
266
+ devServerCliOptions : Partial < WebpackDevServerOptions > ,
267
+ ) : WebpackDevServerOptions => {
270
268
// CLI options should take precedence over devServer options,
271
269
// and CLI options should have no default values included
272
270
const options = { ...devServerOptions , ...devServerCliOptions } ;
273
271
274
- if ( devServerOptions . client && devServerCliOptions . client ) {
272
+ if (
273
+ devServerOptions . client &&
274
+ devServerCliOptions . client &&
275
+ typeof devServerOptions . client === "object" &&
276
+ typeof devServerCliOptions . client === "object"
277
+ ) {
275
278
// the user could set some client options in their devServer config,
276
279
// then also specify client options on the CLI
277
280
options . client = {
@@ -280,7 +283,7 @@ class ServeCommand {
280
283
} ;
281
284
}
282
285
283
- return options ;
286
+ return options as WebpackDevServerOptions ;
284
287
} ;
285
288
286
289
devServerOptions = mergeOptions (
@@ -291,8 +294,8 @@ class ServeCommand {
291
294
292
295
// TODO remove in the next major release
293
296
if ( ! isDevServer4 ) {
294
- const getPublicPathOption = ( ) : string => {
295
- const normalizePublicPath = ( publicPath : string ) : string =>
297
+ const getPublicPathOption = ( ) : PublicPath => {
298
+ const normalizePublicPath = ( publicPath : PublicPath ) : PublicPath =>
296
299
typeof publicPath === "undefined" || publicPath === "auto" ? "/" : publicPath ;
297
300
298
301
if ( options . outputPublicPath ) {
@@ -307,7 +310,7 @@ class ServeCommand {
307
310
308
311
return normalizePublicPath ( compilerForDevServer . options . output . publicPath ) ;
309
312
} ;
310
- const getStatsOption = ( ) : string | boolean => {
313
+ const getStatsOption = ( ) : WebpackDevServerOptions [ "stats" ] => {
311
314
if ( options . stats ) {
312
315
return options . stats ;
313
316
}
@@ -361,7 +364,7 @@ class ServeCommand {
361
364
362
365
servers . push ( server ) ;
363
366
} catch ( error ) {
364
- if ( cli . isValidationError ( error ) ) {
367
+ if ( cli . isValidationError ( error as Error ) ) {
365
368
cli . logger . error ( ( error as Error ) . message ) ;
366
369
} else {
367
370
cli . logger . error ( error ) ;
0 commit comments