@@ -5,12 +5,13 @@ import { devServerOptionsType } from './types';
55 * Starts the devServer
66 *
77 * @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
910 * @param {Object } logger - logger
1011 *
1112 * @returns {Object[] } array of resulting servers
1213 */
13- export default async function startDevServer ( compiler , cliOptions , logger ) : Promise < object [ ] > {
14+ export default async function startDevServer ( compiler , devServerCliOptions , cliOptions , logger ) : Promise < object [ ] > {
1415 let devServerVersion , Server , findPort ;
1516
1617 try {
@@ -25,15 +26,15 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
2526 process . exit ( 2 ) ;
2627 }
2728
28- const mergeOptions = ( cliOptions : devServerOptionsType , devServerOptions : devServerOptionsType ) : devServerOptionsType => {
29+ const mergeOptions = ( devServerOptions : devServerOptionsType , devServerCliOptions : devServerOptionsType ) : devServerOptionsType => {
2930 // CLI options should take precedence over devServer options,
3031 // and CLI options should have no default values included
31- const options = { ...devServerOptions , ...cliOptions } ;
32+ const options = { ...devServerOptions , ...devServerCliOptions } ;
3233
33- if ( devServerOptions . client && cliOptions . client ) {
34+ if ( devServerOptions . client && devServerCliOptions . client ) {
3435 // the user could set some client options in their devServer config,
3536 // then also specify client options on the CLI
36- options . client = { ...devServerOptions . client , ...cliOptions . client } ;
37+ options . client = { ...devServerOptions . client , ...devServerCliOptions . client } ;
3738 }
3839
3940 return options ;
@@ -59,23 +60,48 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
5960 const devServersOptions = [ ] ;
6061
6162 for ( const compilerWithDevServerOption of compilersWithDevServerOption ) {
62- const options = mergeOptions ( cliOptions , compilerWithDevServerOption . options . devServer || { } ) ;
63+ const options = mergeOptions ( compilerWithDevServerOption . options . devServer || { } , devServerCliOptions ) ;
6364
6465 if ( isDevServer4 ) {
6566 options . port = await findPort ( options . port ) ;
6667 options . client = options . client || { } ;
6768 options . client . port = options . client . port || options . port ;
6869 } 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+ } ;
76100
77101 options . host = options . host || 'localhost' ;
78102 options . port = options . port || 8080 ;
103+ options . stats = getStatsOption ( ) ;
104+ options . publicPath = getPublicPathOption ( ) ;
79105 }
80106
81107 if ( options . port ) {
0 commit comments