@@ -17,7 +17,6 @@ const debug = require('debug')('webpack-dev-server');
1717
1818const fs = require ( 'fs' ) ;
1919const net = require ( 'net' ) ;
20- const path = require ( 'path' ) ;
2120
2221const portfinder = require ( 'portfinder' ) ;
2322const importLocal = require ( 'import-local' ) ;
@@ -27,13 +26,14 @@ const webpack = require('webpack');
2726
2827const options = require ( './options' ) ;
2928
30- const { colors, status, version, bonjour, defaultTo } = require ( './utils' ) ;
29+ const { colors, status, version, bonjour } = require ( './utils' ) ;
3130
3231const Server = require ( '../lib/Server' ) ;
3332
3433const addEntries = require ( '../lib/utils/addEntries' ) ;
3534const createDomain = require ( '../lib/utils/createDomain' ) ;
3635const createLogger = require ( '../lib/utils/createLogger' ) ;
36+ const createConfig = require ( '../lib/utils/createConfig' ) ;
3737
3838let server ;
3939
@@ -88,6 +88,7 @@ const argv = yargs.argv;
8888const config = require ( 'webpack-cli/bin/convert-argv' ) ( yargs , argv , {
8989 outputFilename : '/bundle.js' ,
9090} ) ;
91+
9192// Taken out of yargs because we must know if
9293// it wasn't given by the user, in which case
9394// we should use portfinder.
@@ -105,182 +106,16 @@ function processOptions(config) {
105106 return ;
106107 }
107108
108- const firstWpOpt = Array . isArray ( config ) ? config [ 0 ] : config ;
109-
110- const options = config . devServer || firstWpOpt . devServer || { } ;
111-
112- if ( argv . bonjour ) {
113- options . bonjour = true ;
114- }
115-
116- if ( argv . host !== 'localhost' || ! options . host ) {
117- options . host = argv . host ;
118- }
119-
120- if ( argv [ 'allowed-hosts' ] ) {
121- options . allowedHosts = argv [ 'allowed-hosts' ] . split ( ',' ) ;
122- }
123-
124- if ( argv . public ) {
125- options . public = argv . public ;
126- }
127-
128- if ( argv . socket ) {
129- options . socket = argv . socket ;
130- }
131-
132- if ( argv . progress ) {
133- options . progress = argv . progress ;
134- }
135-
136- if ( ! options . publicPath ) {
137- // eslint-disable-next-line
138- options . publicPath =
139- ( firstWpOpt . output && firstWpOpt . output . publicPath ) || '' ;
140-
141- if (
142- ! / ^ ( h t t p s ? : ) ? \/ \/ / . test ( options . publicPath ) &&
143- options . publicPath [ 0 ] !== '/'
144- ) {
145- options . publicPath = `/${ options . publicPath } ` ;
146- }
147- }
148-
149- if ( ! options . filename ) {
150- options . filename = firstWpOpt . output && firstWpOpt . output . filename ;
151- }
152-
153- if ( ! options . watchOptions ) {
154- options . watchOptions = firstWpOpt . watchOptions ;
155- }
156-
157- if ( argv . stdin ) {
158- process . stdin . on ( 'end' , ( ) => {
159- // eslint-disable-next-line no-process-exit
160- process . exit ( 0 ) ;
161- } ) ;
162-
163- process . stdin . resume ( ) ;
164- }
165-
166- if ( ! options . hot ) {
167- options . hot = argv . hot ;
168- }
109+ const options = createConfig ( config , argv , { port : DEFAULT_PORT } ) ;
169110
170- if ( ! options . hotOnly ) {
171- options . hotOnly = argv [ 'hot-only' ] ;
172- }
173-
174- if ( ! options . clientLogLevel ) {
175- options . clientLogLevel = argv [ 'client-log-level' ] ;
176- }
177-
178- // eslint-disable-next-line
179- if ( options . contentBase === undefined ) {
180- if ( argv [ 'content-base' ] ) {
181- options . contentBase = argv [ 'content-base' ] ;
182-
183- if ( Array . isArray ( options . contentBase ) ) {
184- options . contentBase = options . contentBase . map ( ( p ) => path . resolve ( p ) ) ;
185- } else if ( / ^ [ 0 - 9 ] $ / . test ( options . contentBase ) ) {
186- options . contentBase = + options . contentBase ;
187- } else if ( ! / ^ ( h t t p s ? : ) ? \/ \/ / . test ( options . contentBase ) ) {
188- options . contentBase = path . resolve ( options . contentBase ) ;
189- }
190- // It is possible to disable the contentBase by using
191- // `--no-content-base`, which results in arg["content-base"] = false
192- } else if ( argv [ 'content-base' ] === false ) {
193- options . contentBase = false ;
194- }
195- }
196-
197- if ( argv [ 'watch-content-base' ] ) {
198- options . watchContentBase = true ;
199- }
200-
201- if ( ! options . stats ) {
202- options . stats = {
203- cached : false ,
204- cachedAssets : false ,
205- } ;
206- }
207-
208- if (
209- typeof options . stats === 'object' &&
210- typeof options . stats . colors === 'undefined'
211- ) {
212- options . stats = Object . assign ( { } , options . stats , { colors : argv . color } ) ;
213- }
214-
215- if ( argv . lazy ) {
216- options . lazy = true ;
217- }
218-
219- if ( ! argv . info ) {
220- options . noInfo = true ;
221- }
222-
223- if ( argv . quiet ) {
224- options . quiet = true ;
225- }
226-
227- if ( argv . https ) {
228- options . https = true ;
229- }
230-
231- if ( argv [ 'pfx-passphrase' ] ) {
232- options . pfxPassphrase = argv [ 'pfx-passphrase' ] ;
233- }
234-
235- if ( argv . inline === false ) {
236- options . inline = false ;
237- }
238-
239- if ( argv [ 'history-api-fallback' ] ) {
240- options . historyApiFallback = true ;
241- }
242-
243- if ( argv . compress ) {
244- options . compress = true ;
245- }
246-
247- if ( argv [ 'disable-host-check' ] ) {
248- options . disableHostCheck = true ;
249- }
250-
251- if ( argv [ 'open-page' ] ) {
252- options . open = true ;
253- options . openPage = argv [ 'open-page' ] ;
254- }
255-
256- if ( typeof argv . open !== 'undefined' ) {
257- options . open = argv . open !== '' ? argv . open : true ;
258- }
259-
260- if ( options . open && ! options . openPage ) {
261- options . openPage = '' ;
262- }
263-
264- if ( argv . useLocalIp ) {
265- options . useLocalIp = true ;
266- }
267- // Kind of weird, but ensures prior behavior isn't broken in cases
268- // that wouldn't throw errors. E.g. both argv.port and options.port
269- // were specified, but since argv.port is 8080, options.port will be
270- // tried first instead.
271- options . port =
272- argv . port === DEFAULT_PORT
273- ? defaultTo ( options . port , argv . port )
274- : defaultTo ( argv . port , options . port ) ;
111+ portfinder . basePort = DEFAULT_PORT ;
275112
276113 if ( options . port != null ) {
277114 startDevServer ( config , options ) ;
278115
279116 return ;
280117 }
281118
282- portfinder . basePort = DEFAULT_PORT ;
283-
284119 portfinder . getPort ( ( err , port ) => {
285120 if ( err ) {
286121 throw err ;
0 commit comments