@@ -5,12 +5,13 @@ import { devServerOptionsType } from './types';
5
5
* Starts the devServer
6
6
*
7
7
* @param {Object } compiler - a webpack compiler
8
- * @param {Object } cliOptions - devServer args
8
+ * @param {Object } devServerCliOptions - dev server CLI options
9
+ * @param {Object } cliOptions - CLI options
9
10
* @param {Object } logger - logger
10
11
*
11
12
* @returns {Object[] } array of resulting servers
12
13
*/
13
- export default async function startDevServer ( compiler , cliOptions , logger ) : Promise < object [ ] > {
14
+ export default async function startDevServer ( compiler , devServerCliOptions , cliOptions , logger ) : Promise < object [ ] > {
14
15
let devServerVersion , Server , findPort ;
15
16
16
17
try {
@@ -25,15 +26,15 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
25
26
process . exit ( 2 ) ;
26
27
}
27
28
28
- const mergeOptions = ( cliOptions : devServerOptionsType , devServerOptions : devServerOptionsType ) : devServerOptionsType => {
29
+ const mergeOptions = ( devServerOptions : devServerOptionsType , devServerCliOptions : devServerOptionsType ) : devServerOptionsType => {
29
30
// CLI options should take precedence over devServer options,
30
31
// and CLI options should have no default values included
31
- const options = { ...devServerOptions , ...cliOptions } ;
32
+ const options = { ...devServerOptions , ...devServerCliOptions } ;
32
33
33
- if ( devServerOptions . client && cliOptions . client ) {
34
+ if ( devServerOptions . client && devServerCliOptions . client ) {
34
35
// the user could set some client options in their devServer config,
35
36
// then also specify client options on the CLI
36
- options . client = { ...devServerOptions . client , ...cliOptions . client } ;
37
+ options . client = { ...devServerOptions . client , ...devServerCliOptions . client } ;
37
38
}
38
39
39
40
return options ;
@@ -59,23 +60,48 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
59
60
const devServersOptions = [ ] ;
60
61
61
62
for ( const compilerWithDevServerOption of compilersWithDevServerOption ) {
62
- const options = mergeOptions ( cliOptions , compilerWithDevServerOption . options . devServer || { } ) ;
63
+ const options = mergeOptions ( compilerWithDevServerOption . options . devServer || { } , devServerCliOptions ) ;
63
64
64
65
if ( isDevServer4 ) {
65
66
options . port = await findPort ( options . port ) ;
66
67
options . client = options . client || { } ;
67
68
options . client . port = options . client . port || options . port ;
68
69
} 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
- }
70
+ const getPublicPathOption = ( ) => {
71
+ const normalizePublicPath = ( publicPath ) => ( typeof publicPath === 'undefined' || publicPath === 'auto' ? '/' : publicPath ) ;
72
+
73
+ if ( cliOptions . outputPublicPath ) {
74
+ return normalizePublicPath ( compilerWithDevServerOption . options . output . publicPath ) ;
75
+ }
76
+
77
+ // webpack-dev-server@3
78
+ if ( options . publicPath ) {
79
+ return normalizePublicPath ( options . publicPath ) ;
80
+ }
81
+
82
+ // webpack-dev-server@4
83
+ if ( options . dev && options . dev . publicPath ) {
84
+ return normalizePublicPath ( options . dev . publicPath ) ;
85
+ }
86
+
87
+ return normalizePublicPath ( compilerWithDevServerOption . options . output . publicPath ) ;
88
+ } ;
89
+ const getStatsOption = ( ) => {
90
+ if ( cliOptions . stats ) {
91
+ return compilerWithDevServerOption . options . stats ;
92
+ }
93
+
94
+ if ( options . stats ) {
95
+ return options . stats ;
96
+ }
97
+
98
+ return compilerWithDevServerOption . options . stats ;
99
+ } ;
76
100
77
101
options . host = options . host || 'localhost' ;
78
102
options . port = options . port || 8080 ;
103
+ options . stats = getStatsOption ( ) ;
104
+ options . publicPath = getPublicPathOption ( ) ;
79
105
}
80
106
81
107
if ( options . port ) {
0 commit comments