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 1 commit
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
// used instead.
options.port = argv.port === DEFAULT_PORT ? options.port || argv.port : argv.port;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be an error here. If I set the port via options, after this line it results in options.port being undefined.

Copy link
Contributor Author

@benwiley4000 benwiley4000 Nov 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, thanks for catching that. I didn't account for the case where options.port is defined but argv.port is not (I forgot since argv used to have a default).

What do you think of just changing it to:

options.port = argv.port === DEFAULT ? (options.port || argv.port) : (argv.port || options.port);

? This should account for all cases I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit strange semantically when argv.port is not DEFAULT and therefore favored because it's undefined, but we'll still land on options.port in the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, tested that and it appears to work in all cases :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My branch has been updated.

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