Skip to content

Commit f5791a0

Browse files
Joel Denningknagaitsev
Joel Denning
authored andcommitted
fix(server): set port before instantiating server (webpack#2143)
1 parent 543bbe1 commit f5791a0

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

bin/webpack-dev-server.js

Lines changed: 4 additions & 12 deletions
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

Lines changed: 16 additions & 1 deletion
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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,36 @@ describe('CLI', () => {
6262
expect(stdout.includes('Project is running at')).toBe(true);
6363
});
6464

65-
it('--sockPath', async () => {
66-
const { stdout } = await testBin('--sockPath /mysockPath');
67-
expect(stdout.includes('http://localhost&sockPath=/mysockPath')).toBe(true);
65+
it('--sockPath', (done) => {
66+
testBin('--sockPath /mysockPath')
67+
.then((output) => {
68+
expect(
69+
/http:\/\/localhost:[0-9]+&sockPath=\/mysockPath/.test(output.stdout)
70+
).toEqual(true);
71+
done();
72+
})
73+
.catch(done);
74+
});
75+
76+
it('unspecified port', (done) => {
77+
testBin('')
78+
.then((output) => {
79+
expect(/http:\/\/localhost:[0-9]+/.test(output.stdout)).toEqual(true);
80+
done();
81+
})
82+
.catch(done);
6883
});
6984

70-
it('--color', async () => {
71-
const { stdout } = await testBin('--color');
72-
// https://github.com/webpack/webpack-dev-server/blob/master/lib/utils/colors.js
73-
expect(stdout.includes('\u001b[39m \u001b[90m「wds」\u001b[39m:')).toBe(true);
85+
it('--color', (done) => {
86+
testBin('--color')
87+
.then((output) => {
88+
// https://github.com/webpack/webpack-dev-server/blob/master/lib/utils/colors.js
89+
expect(
90+
output.stdout.includes('\u001b[39m \u001b[90m「wds」\u001b[39m:')
91+
).toEqual(true);
92+
done();
93+
})
94+
.catch(done);
7495
});
7596

7697
// The Unix socket to listen to (instead of a host).

0 commit comments

Comments
 (0)