Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI finds and uses 1st available port when none specified. #685

Merged
merged 2 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -143,7 +149,6 @@ yargs.options({
},
"port": {
describe: "The port",
default: 8080,
group: CONNECTION_GROUP
},
"socket": {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
// tried first instead.
options.port = argv.port === DEFAULT_PORT ? (options.port || argv.port) : (argv.port || options.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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down