@@ -11,10 +11,7 @@ import { devServerOptionsType } from './types';
11
11
* @returns {Object[] } array of resulting servers
12
12
*/
13
13
export default async function startDevServer ( compiler , cliOptions , logger ) : Promise < object [ ] > {
14
- let isDevServer4 = false ,
15
- devServerVersion ,
16
- Server ,
17
- findPort ;
14
+ let devServerVersion , Server , findPort ;
18
15
19
16
try {
20
17
// eslint-disable-next-line node/no-extraneous-require
@@ -28,24 +25,6 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
28
25
process . exit ( 2 ) ;
29
26
}
30
27
31
- isDevServer4 = devServerVersion . startsWith ( '4' ) ;
32
-
33
- const defaultOpts = { } ;
34
- const devServerOptions = [ ] ;
35
- const compilers = compiler . compilers || [ compiler ] ;
36
-
37
- compilers . forEach ( ( compiler ) => {
38
- if ( compiler . options . devServer ) {
39
- devServerOptions . push ( compiler . options . devServer ) ;
40
- }
41
- } ) ;
42
-
43
- if ( devServerOptions . length === 0 ) {
44
- devServerOptions . push ( defaultOpts ) ;
45
- }
46
-
47
- const servers = [ ] ;
48
- const usedPorts : number [ ] = [ ] ;
49
28
const mergeOptions = ( cliOptions : devServerOptionsType , devServerOptions : devServerOptionsType ) : devServerOptionsType => {
50
29
// CLI options should take precedence over devServer options,
51
30
// and CLI options should have no default values included
@@ -60,35 +39,69 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
60
39
return options ;
61
40
} ;
62
41
63
- for ( const devServerOpts of devServerOptions ) {
64
- const options = mergeOptions ( cliOptions , devServerOpts ) ;
42
+ const isMultiCompiler = Boolean ( compiler . compilers ) ;
43
+
44
+ let compilersWithDevServerOption ;
45
+
46
+ if ( isMultiCompiler ) {
47
+ compilersWithDevServerOption = compiler . compilers . filter ( ( compiler ) => compiler . options . devServer ) ;
48
+
49
+ // No compilers found with the `devServer` option, let's use first compiler
50
+ if ( compilersWithDevServerOption . length === 0 ) {
51
+ compilersWithDevServerOption = [ compiler . compilers [ 0 ] ] ;
52
+ }
53
+ } else {
54
+ compilersWithDevServerOption = [ compiler ] ;
55
+ }
56
+
57
+ const isDevServer4 = devServerVersion . startsWith ( '4' ) ;
58
+ const usedPorts = [ ] ;
59
+ const devServersOptions = [ ] ;
60
+
61
+ for ( const compilerWithDevServerOption of compilersWithDevServerOption ) {
62
+ const options = mergeOptions ( cliOptions , compilerWithDevServerOption . options . devServer || { } ) ;
65
63
66
64
if ( isDevServer4 ) {
67
65
options . port = await findPort ( options . port ) ;
68
66
options . client = options . client || { } ;
69
67
options . client . port = options . client . port || options . port ;
70
68
} else {
69
+ if ( ! options . publicPath ) {
70
+ options . publicPath =
71
+ typeof compilerWithDevServerOption . options . output . publicPath === 'undefined' ||
72
+ compilerWithDevServerOption . options . output . publicPath === 'auto'
73
+ ? '/'
74
+ : compilerWithDevServerOption . options . output . publicPath ;
75
+ }
76
+
71
77
options . host = options . host || 'localhost' ;
72
78
options . port = options . port || 8080 ;
73
79
}
74
80
75
81
if ( options . port ) {
76
- const portNum = + options . port ;
82
+ const portNumber = Number ( options . port ) ;
77
83
78
- if ( usedPorts . find ( ( port ) => portNum === port ) ) {
84
+ if ( usedPorts . find ( ( port ) => portNumber === port ) ) {
79
85
throw new Error (
80
86
'Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.' ,
81
87
) ;
82
88
}
83
89
84
- usedPorts . push ( portNum ) ;
90
+ usedPorts . push ( portNumber ) ;
85
91
}
86
92
93
+ devServersOptions . push ( { compiler, options } ) ;
94
+ }
95
+
96
+ const servers = [ ] ;
97
+
98
+ for ( const devServerOptions of devServersOptions ) {
99
+ const { compiler, options } = devServerOptions ;
87
100
const server = new Server ( compiler , options ) ;
88
101
89
- server . listen ( options . port , options . host , ( err ) : void => {
90
- if ( err ) {
91
- throw err ;
102
+ server . listen ( options . port , options . host , ( error ) : void => {
103
+ if ( error ) {
104
+ throw error ;
92
105
}
93
106
} ) ;
94
107
0 commit comments