Skip to content

Commit dc29772

Browse files
committed
refactor: migrate server.listen from bin
fixes: #1510
1 parent 8212134 commit dc29772

File tree

5 files changed

+74
-59
lines changed

5 files changed

+74
-59
lines changed

bin/webpack-dev-server.js

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ const Server = require('../lib/Server');
2222

2323
const colors = require('../lib/utils/colors');
2424
const createConfig = require('../lib/utils/createConfig');
25-
const createDomain = require('../lib/utils/createDomain');
2625
const createLogger = require('../lib/utils/createLogger');
2726
const defaultTo = require('../lib/utils/defaultTo');
2827
const findPort = require('../lib/utils/findPort');
2928
const getVersions = require('../lib/utils/getVersions');
30-
const runBonjour = require('../lib/utils/runBonjour');
31-
const status = require('../lib/utils/status');
3229
const tryParseInt = require('../lib/utils/tryParseInt');
3330

3431
let server;
@@ -159,11 +156,6 @@ function startDevServer(config, options) {
159156
}).apply(compiler);
160157
}
161158

162-
const suffix =
163-
options.inline !== false || options.lazy === true
164-
? '/'
165-
: '/webpack-dev-server/';
166-
167159
try {
168160
server = new Server(compiler, options, log);
169161
} catch (err) {
@@ -200,52 +192,27 @@ function startDevServer(config, options) {
200192
}
201193
});
202194

203-
server.listen(options.socket, options.host, (err) => {
195+
runServer();
196+
} else if (options.port) {
197+
runServer();
198+
} else {
199+
// only run port finder if no port as been specified
200+
findPort(server, DEFAULT_PORT, defaultPortRetry, (err, port) => {
204201
if (err) {
205202
throw err;
206203
}
207-
// chmod 666 (rw rw rw)
208-
const READ_WRITE = 438;
209-
210-
fs.chmod(options.socket, READ_WRITE, (err) => {
211-
if (err) {
212-
throw err;
213-
}
214-
215-
const uri = createDomain(options, server.listeningApp) + suffix;
216-
217-
status(uri, options, log, argv.color);
218-
});
204+
options.port = port;
205+
runServer();
219206
});
220-
return;
221207
}
222208

223-
const startServer = () => {
209+
function runServer() {
224210
server.listen(options.port, options.host, (err) => {
225211
if (err) {
226212
throw err;
227213
}
228-
if (options.bonjour) {
229-
runBonjour(options);
230-
}
231-
const uri = createDomain(options, server.listeningApp) + suffix;
232-
status(uri, options, log, argv.color);
233214
});
234-
};
235-
236-
if (options.port) {
237-
startServer();
238-
return;
239215
}
240-
241-
// only run port finder if no port as been specified
242-
findPort(server, DEFAULT_PORT, defaultPortRetry, (err, port) => {
243-
if (err) {
244-
throw err;
245-
}
246-
options.port = port;
247-
startServer();
248-
});
249216
}
250217

251218
processOptions(config);

examples/api/simple/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const webpackConfig = require('./webpack.config');
66

77
const compiler = Webpack(webpackConfig);
88
const devServerOptions = Object.assign({}, webpackConfig.devServer, {
9+
open: true,
910
stats: {
1011
colors: true,
1112
},

lib/Server.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const validateOptions = require('schema-utils');
2727
const updateCompiler = require('./utils/updateCompiler');
2828
const createLogger = require('./utils/createLogger');
2929
const getCertificate = require('./utils/getCertificate');
30+
const status = require('./utils/status');
31+
const createDomain = require('./utils/createDomain');
32+
const runBonjour = require('./utils/runBonjour');
33+
const open = require('./utils/open');
3034
const routes = require('./utils/routes');
3135
const schema = require('./options.json');
3236

@@ -809,6 +813,45 @@ class Server {
809813
prefix: this.sockPath,
810814
});
811815

816+
if (this.options.bonjour) {
817+
runBonjour(this.options);
818+
}
819+
820+
const showStatusAndOpen = () => {
821+
const suffix =
822+
this.options.inline !== false || this.options.lazy === true
823+
? '/'
824+
: '/webpack-dev-server/';
825+
826+
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;
827+
828+
// TODO: don't emit when webpack-dev-server is used as API
829+
status(
830+
uri,
831+
this.options,
832+
this.log,
833+
this.options.stats && this.options.stats.colors
834+
);
835+
if (this.options.open) {
836+
open(uri, this.options, this.log);
837+
}
838+
};
839+
840+
if (this.options.socket) {
841+
// chmod 666 (rw rw rw)
842+
const READ_WRITE = 438;
843+
844+
fs.chmod(this.options.socket, READ_WRITE, (err) => {
845+
if (err) {
846+
throw err;
847+
}
848+
849+
showStatusAndOpen();
850+
});
851+
} else {
852+
showStatusAndOpen();
853+
}
854+
812855
if (fn) {
813856
fn.call(this.listeningApp, err);
814857
}

lib/utils/open.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const opn = require('opn');
4+
5+
function open(uri, options, log) {
6+
let openOptions = {};
7+
let openMessage = 'Unable to open browser';
8+
9+
if (typeof options.open === 'string') {
10+
openOptions = { app: options.open };
11+
openMessage += `: ${options.open}`;
12+
}
13+
14+
opn(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
15+
log.warn(
16+
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
17+
);
18+
});
19+
}
20+
21+
module.exports = open;

lib/utils/status.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const open = require('opn');
43
const colors = require('./colors');
54

65
function status(uri, options, log, useColor) {
@@ -41,22 +40,6 @@ function status(uri, options, log, useColor) {
4140
'Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'
4241
);
4342
}
44-
45-
if (options.open) {
46-
let openOptions = {};
47-
let openMessage = 'Unable to open browser';
48-
49-
if (typeof options.open === 'string') {
50-
openOptions = { app: options.open };
51-
openMessage += `: ${options.open}`;
52-
}
53-
54-
open(uri + (options.openPage || ''), openOptions).catch(() => {
55-
log.warn(
56-
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
57-
);
58-
});
59-
}
6043
}
6144

6245
module.exports = status;

0 commit comments

Comments
 (0)