From 3acd2f04db456fcd077d49323ac66a9b9a52e7ec Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Sun, 21 Jul 2019 23:12:33 -0600 Subject: [PATCH] fix(server): set port before instantiating server --- bin/webpack-dev-server.js | 16 ++++------------ lib/utils/processOptions.js | 17 ++++++++++++++++- test/cli/cli.test.js | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/bin/webpack-dev-server.js b/bin/webpack-dev-server.js index 26de7d4ba6..7c3456f5d3 100755 --- a/bin/webpack-dev-server.js +++ b/bin/webpack-dev-server.js @@ -15,7 +15,6 @@ const setupExitSignals = require('../lib/utils/setupExitSignals'); const colors = require('../lib/utils/colors'); const processOptions = require('../lib/utils/processOptions'); const createLogger = require('../lib/utils/createLogger'); -const findPort = require('../lib/utils/findPort'); const getVersions = require('../lib/utils/getVersions'); const options = require('./options'); @@ -148,18 +147,11 @@ function startDevServer(config, options) { }); }); } else { - findPort(options.port) - .then((port) => { - options.port = port; - server.listen(options.port, options.host, (err) => { - if (err) { - throw err; - } - }); - }) - .catch((err) => { + server.listen(options.port, options.host, (err) => { + if (err) { throw err; - }); + } + }); } } diff --git a/lib/utils/processOptions.js b/lib/utils/processOptions.js index 23da69b430..ba224ed690 100644 --- a/lib/utils/processOptions.js +++ b/lib/utils/processOptions.js @@ -2,6 +2,7 @@ const createConfig = require('./createConfig'); const defaultPort = require('./defaultPort'); +const findPort = require('./findPort'); function processOptions(config, argv, callback) { // processOptions {Promise} @@ -23,7 +24,21 @@ function processOptions(config, argv, callback) { // we should use portfinder. const options = createConfig(config, argv, { port: defaultPort }); - callback(config, options); + if (options.socket) { + callback(config, options); + } else { + findPort(options.port) + .then((port) => { + options.port = port; + callback(config, options); + }) + .catch((err) => { + // eslint-disable-next-line no-console + console.error(err.stack || err); + // eslint-disable-next-line no-process-exit + process.exit(1); + }); + } } module.exports = processOptions; diff --git a/test/cli/cli.test.js b/test/cli/cli.test.js index 91e82b7bc6..77c3f2eb70 100644 --- a/test/cli/cli.test.js +++ b/test/cli/cli.test.js @@ -78,7 +78,7 @@ describe('CLI', () => { testBin('--sockPath /mysockPath') .then((output) => { expect( - output.stdout.includes('http://localhost&sockPath=/mysockPath') + output.stdout.includes('http://localhost:8080&sockPath=/mysockPath') ).toEqual(true); done(); })