Skip to content

Commit cfbf229

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

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎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
constcolors=require('../lib/utils/colors');
1616
constprocessOptions=require('../lib/utils/processOptions');
1717
constcreateLogger=require('../lib/utils/createLogger');
18-
constfindPort=require('../lib/utils/findPort');
1918
constgetVersions=require('../lib/utils/getVersions');
2019
constoptions=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-
throwerr;
157-
}
158-
});
159-
})
160-
.catch((err)=>{
150+
server.listen(options.port,options.host,(err)=>{
151+
if(err){
161152
throwerr;
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
constcreateConfig=require('./createConfig');
44
constdefaultPort=require('./defaultPort');
5+
constfindPort=require('./findPort');
56

67
functionprocessOptions(config,argv,callback){
78
// processOptions {Promise}
@@ -23,7 +24,21 @@ function processOptions(config, argv, callback) {
2324
// we should use portfinder.
2425
constoptions=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: 10 additions & 1 deletion
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)