Skip to content

Commit cfbf229

Browse files
jolyndenningevilebottnawi
authored andcommitted
fix(server): set port before instantiating server (#2143)
1 parent f80e2ae commit cfbf229

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

bin/webpack-dev-server.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const setupExitSignals = require('../lib/utils/setupExitSignals');
1515
const colors = require('../lib/utils/colors');
1616
const processOptions = require('../lib/utils/processOptions');
1717
const createLogger = require('../lib/utils/createLogger');
18-
const findPort = require('../lib/utils/findPort');
1918
const getVersions = require('../lib/utils/getVersions');
2019
const options = require('./options');
2120

@@ -148,18 +147,11 @@ function startDevServer(config, options) {
148147
});
149148
});
150149
} else {
151-
findPort(options.port)
152-
.then((port) => {
153-
options.port = port;
154-
server.listen(options.port, options.host, (err) => {
155-
if (err) {
156-
throw err;
157-
}
158-
});
159-
})
160-
.catch((err) => {
150+
server.listen(options.port, options.host, (err) => {
151+
if (err) {
161152
throw err;
162-
});
153+
}
154+
});
163155
}
164156
}
165157

lib/utils/processOptions.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const createConfig = require('./createConfig');
44
const defaultPort = require('./defaultPort');
5+
const findPort = require('./findPort');
56

67
function processOptions(config, argv, callback) {
78
// processOptions {Promise}
@@ -23,7 +24,21 @@ function processOptions(config, argv, callback) {
2324
// we should use portfinder.
2425
const options = createConfig(config, argv, { port: defaultPort });
2526

26-
callback(config, options);
27+
if (options.socket) {
28+
callback(config, options);
29+
} else {
30+
findPort(options.port)
31+
.then((port) => {
32+
options.port = port;
33+
callback(config, options);
34+
})
35+
.catch((err) => {
36+
// eslint-disable-next-line no-console
37+
console.error(err.stack || err);
38+
// eslint-disable-next-line no-process-exit
39+
process.exit(1);
40+
});
41+
}
2742
}
2843

2944
module.exports = processOptions;

test/cli/cli.test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,22 @@ describe('CLI', () => {
7878
testBin('--sockPath /mysockPath')
7979
.then((output) => {
8080
expect(
81-
output.stdout.includes('http://localhost&sockPath=/mysockPath')
81+
/http:\/\/localhost:[0-9]+&sockPath=\/mysockPath/.test(output.stdout)
8282
).toEqual(true);
8383
done();
8484
})
8585
.catch(done);
8686
});
8787

88+
it('unspecified port', (done) => {
89+
testBin('')
90+
.then((output) => {
91+
expect(/http:\/\/localhost:[0-9]+/.test(output.stdout)).toEqual(true);
92+
done();
93+
})
94+
.catch(done);
95+
});
96+
8897
it('--color', (done) => {
8998
testBin('--color')
9099
.then((output) => {

0 commit comments

Comments
 (0)