@@ -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 {
@@ -53,6 +54,11 @@ var CONNECTION_GROUP = "Connection options:";
53
54
var RESPONSE_GROUP = "Response options:" ;
54
55
var BASIC_GROUP = "Basic options:" ;
55
56
57
+ // Taken out of yargs because we must know if
58
+ // it wasn't given by the user, in which case
59
+ // we should use portfinder.
60
+ var DEFAULT_PORT = 8080 ;
61
+
56
62
yargs . options ( {
57
63
"lazy" : {
58
64
type : "boolean" ,
@@ -150,7 +156,6 @@ yargs.options({
150
156
} ,
151
157
"port" : {
152
158
describe : "The port" ,
153
- default : 8080 ,
154
159
group : CONNECTION_GROUP
155
160
} ,
156
161
"socket" : {
@@ -197,9 +202,6 @@ function processOptions(wpOpt) {
197
202
if ( argv . public )
198
203
options . public = argv . public ;
199
204
200
- if ( argv . port !== 8080 || ! options . port )
201
- options . port = argv . port ;
202
-
203
205
if ( argv . socket )
204
206
options . socket = argv . socket ;
205
207
@@ -296,6 +298,25 @@ function processOptions(wpOpt) {
296
298
if ( argv [ "open" ] )
297
299
options . open = true ;
298
300
301
+ // Kind of weird, but ensures prior behavior isn't broken in cases
302
+ // that wouldn't throw errors. E.g. both argv.port and options.port
303
+ // were specified, but since argv.port is 8080, options.port will be
304
+ // tried first instead.
305
+ options . port = argv . port === DEFAULT_PORT ? ( options . port || argv . port ) : ( argv . port || options . port ) ;
306
+ if ( options . port ) {
307
+ startDevServer ( wpOpt , options ) ;
308
+ return ;
309
+ }
310
+
311
+ portfinder . basePort = DEFAULT_PORT ;
312
+ portfinder . getPort ( function ( err , port ) {
313
+ if ( err ) throw err ;
314
+ options . port = port ;
315
+ startDevServer ( wpOpt , options ) ;
316
+ } ) ;
317
+ }
318
+
319
+ function startDevServer ( wpOpt , options ) {
299
320
var protocol = options . https ? "https" : "http" ;
300
321
301
322
// the formatted domain (url without path) of the webpack server
0 commit comments