@@ -5,6 +5,7 @@ var open = require("opn");
5
5
var fs = require ( "fs" ) ;
6
6
var net = require ( "net" ) ;
7
7
var url = require ( "url" ) ;
8
+ var portfinder = require ( "portfinder" ) ;
8
9
9
10
// Local version replaces global one
10
11
try {
@@ -46,6 +47,11 @@ var CONNECTION_GROUP = "Connection options:";
46
47
var RESPONSE_GROUP = "Response options:" ;
47
48
var BASIC_GROUP = "Basic options:" ;
48
49
50
+ // Taken out of yargs because we must know if
51
+ // it wasn't given by the user, in which case
52
+ // we should use portfinder.
53
+ var DEFAULT_PORT = 8080 ;
54
+
49
55
yargs . options ( {
50
56
"lazy" : {
51
57
type : "boolean" ,
@@ -143,7 +149,6 @@ yargs.options({
143
149
} ,
144
150
"port" : {
145
151
describe : "The port" ,
146
- default : 8080 ,
147
152
group : CONNECTION_GROUP
148
153
} ,
149
154
"socket" : {
@@ -190,9 +195,6 @@ function processOptions(wpOpt) {
190
195
if ( argv . public )
191
196
options . public = argv . public ;
192
197
193
- if ( argv . port !== 8080 || ! options . port )
194
- options . port = argv . port ;
195
-
196
198
if ( argv . socket )
197
199
options . socket = argv . socket ;
198
200
@@ -291,6 +293,25 @@ function processOptions(wpOpt) {
291
293
if ( argv [ "open" ] )
292
294
options . open = true ;
293
295
296
+ // Kind of weird, but ensures prior behavior isn't broken in cases
297
+ // that wouldn't throw errors. E.g. both argv.port and options.port
298
+ // were specified, but since argv.port is 8080, options.port will be
299
+ // tried first instead.
300
+ options . port = argv . port === DEFAULT_PORT ? ( options . port || argv . port ) : ( argv . port || options . port ) ;
301
+ if ( options . port ) {
302
+ startDevServer ( wpOpt , options ) ;
303
+ return ;
304
+ }
305
+
306
+ portfinder . basePort = DEFAULT_PORT ;
307
+ portfinder . getPort ( function ( err , port ) {
308
+ if ( err ) throw err ;
309
+ options . port = port ;
310
+ startDevServer ( wpOpt , options ) ;
311
+ } ) ;
312
+ }
313
+
314
+ function startDevServer ( wpOpt , options ) {
294
315
var protocol = options . https ? "https" : "http" ;
295
316
296
317
// the formatted domain (url without path) of the webpack server
0 commit comments