@@ -17,7 +17,6 @@ const debug = require('debug')('webpack-dev-server');
17
17
18
18
const fs = require ( 'fs' ) ;
19
19
const net = require ( 'net' ) ;
20
- const path = require ( 'path' ) ;
21
20
22
21
const portfinder = require ( 'portfinder' ) ;
23
22
const importLocal = require ( 'import-local' ) ;
@@ -27,13 +26,14 @@ const webpack = require('webpack');
27
26
28
27
const options = require ( './options' ) ;
29
28
30
- const { colors, status, version, bonjour, defaultTo } = require ( './utils' ) ;
29
+ const { colors, status, version, bonjour } = require ( './utils' ) ;
31
30
32
31
const Server = require ( '../lib/Server' ) ;
33
32
34
33
const addEntries = require ( '../lib/utils/addEntries' ) ;
35
34
const createDomain = require ( '../lib/utils/createDomain' ) ;
36
35
const createLogger = require ( '../lib/utils/createLogger' ) ;
36
+ const createConfig = require ( '../lib/utils/createConfig' ) ;
37
37
38
38
let server ;
39
39
@@ -88,6 +88,7 @@ const argv = yargs.argv;
88
88
const config = require ( 'webpack-cli/bin/convert-argv' ) ( yargs , argv , {
89
89
outputFilename : '/bundle.js' ,
90
90
} ) ;
91
+
91
92
// Taken out of yargs because we must know if
92
93
// it wasn't given by the user, in which case
93
94
// we should use portfinder.
@@ -105,182 +106,16 @@ function processOptions(config) {
105
106
return ;
106
107
}
107
108
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 } ) ;
169
110
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 ;
275
112
276
113
if ( options . port != null ) {
277
114
startDevServer ( config , options ) ;
278
115
279
116
return ;
280
117
}
281
118
282
- portfinder . basePort = DEFAULT_PORT ;
283
-
284
119
portfinder . getPort ( ( err , port ) => {
285
120
if ( err ) {
286
121
throw err ;
0 commit comments