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

fix(server): set port before instantiating server #2143

Merged
merged 10 commits into from
Jul 30, 2019
16 changes: 4 additions & 12 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
});
}
});
}
}

Expand Down
17 changes: 16 additions & 1 deletion lib/utils/processOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const createConfig = require('./createConfig');
const defaultPort = require('./defaultPort');
const findPort = require('./findPort');

function processOptions(config, argv, callback) {
// processOptions {Promise}
Expand All @@ -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);
joeldenning marked this conversation as resolved.
Show resolved Hide resolved
});
}
}

module.exports = processOptions;
11 changes: 10 additions & 1 deletion test/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,22 @@ describe('CLI', () => {
testBin('--sockPath /mysockPath')
.then((output) => {
expect(
output.stdout.includes('http://localhost&sockPath=/mysockPath')
/http:\/\/localhost:[0-9]+&sockPath=\/mysockPath/.test(output.stdout)
).toEqual(true);
done();
})
.catch(done);
});

it('unspecified port', (done) => {
testBin('')
.then((output) => {
expect(/http:\/\/localhost:[0-9]+/.test(output.stdout)).toEqual(true);
done();
})
.catch(done);
});

it('--color', (done) => {
testBin('--color')
.then((output) => {
Expand Down