From 33dad8f8ecfd3655c96e76e004030284e2232e60 Mon Sep 17 00:00:00 2001 From: Ben Wiley Date: Thu, 3 Nov 2016 03:26:31 -0400 Subject: [PATCH 1/2] CLI finds and uses 1st available port when none specified. --- bin/webpack-dev-server.js | 29 +++++++++++++++++++++++++---- package.json | 1 + 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/bin/webpack-dev-server.js b/bin/webpack-dev-server.js index 9ceed7ca9f..29f4d3cda5 100755 --- a/bin/webpack-dev-server.js +++ b/bin/webpack-dev-server.js @@ -5,6 +5,7 @@ var open = require("opn"); var fs = require("fs"); var net = require("net"); var url = require("url"); +var portfinder = require("portfinder"); // Local version replaces global one try { @@ -46,6 +47,11 @@ var CONNECTION_GROUP = "Connection options:"; var RESPONSE_GROUP = "Response options:"; var BASIC_GROUP = "Basic options:"; +// Taken out of yargs because we must know if +// it wasn't given by the user, in which case +// we should use portfinder. +var DEFAULT_PORT = 8080; + yargs.options({ "lazy": { type: "boolean", @@ -143,7 +149,6 @@ yargs.options({ }, "port": { describe: "The port", - default: 8080, group: CONNECTION_GROUP }, "socket": { @@ -190,9 +195,6 @@ function processOptions(wpOpt) { if(argv.public) options.public = argv.public; - if(argv.port !== 8080 || !options.port) - options.port = argv.port; - if(argv.socket) options.socket = argv.socket; @@ -294,6 +296,25 @@ function processOptions(wpOpt) { if(argv["open"]) options.open = true; + // Kind of weird, but ensures prior behavior isn't broken in cases + // that wouldn't throw errors. E.g. both argv port and options port + // were specified, but since argv port is 8080, options port will be + // used instead. + options.port = argv.port === DEFAULT_PORT ? options.port || argv.port : argv.port; + if(options.port) { + startDevServer(wpOpt, options); + return; + } + + portfinder.basePort = DEFAULT_PORT; + portfinder.getPort(function(err, port) { + if(err) throw err; + options.port = port; + startDevServer(wpOpt, options); + }); +} + +function startDevServer(wpOpt, options) { var protocol = options.https ? "https" : "http"; // the formatted domain (url without path) of the webpack server diff --git a/package.json b/package.json index 9907b5a32b..3ccdc5c715 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "express": "^4.13.3", "http-proxy-middleware": "~0.17.1", "opn": "4.0.2", + "portfinder": "^1.0.9", "serve-index": "^1.7.2", "sockjs": "0.3.18", "sockjs-client": "1.1.1", From 335aa9e1b6cc9b0d81d06c8851024706300ab1ef Mon Sep 17 00:00:00 2001 From: Ben Wiley Date: Thu, 3 Nov 2016 06:09:18 -0400 Subject: [PATCH 2/2] Updated for case where options but not args specify port. --- bin/webpack-dev-server.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/webpack-dev-server.js b/bin/webpack-dev-server.js index 29f4d3cda5..ac753cb10c 100755 --- a/bin/webpack-dev-server.js +++ b/bin/webpack-dev-server.js @@ -297,10 +297,10 @@ function processOptions(wpOpt) { options.open = true; // Kind of weird, but ensures prior behavior isn't broken in cases - // that wouldn't throw errors. E.g. both argv port and options port - // were specified, but since argv port is 8080, options port will be - // used instead. - options.port = argv.port === DEFAULT_PORT ? options.port || argv.port : argv.port; + // that wouldn't throw errors. E.g. both argv.port and options.port + // were specified, but since argv.port is 8080, options.port will be + // tried first instead. + options.port = argv.port === DEFAULT_PORT ? (options.port || argv.port) : (argv.port || options.port); if(options.port) { startDevServer(wpOpt, options); return;