Skip to content

Commit c055245

Browse files
fix: respect open and bonjour for node API
1 parent fa96a76 commit c055245

File tree

7 files changed

+191
-163
lines changed

7 files changed

+191
-163
lines changed

bin/utils.js

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@
77
array-bracket-spacing,
88
space-before-function-paren
99
*/
10-
const open = require('opn');
11-
12-
const colors = {
13-
info (useColor, msg) {
14-
if (useColor) {
15-
// Make text blue and bold, so it *pops*
16-
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
17-
}
18-
19-
return msg;
20-
},
21-
error (useColor, msg) {
22-
if (useColor) {
23-
// Make text red and bold, so it *pops*
24-
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
25-
}
26-
27-
return msg;
28-
}
29-
};
3010

3111
// eslint-disable-next-line
3212
const defaultTo = (value, def) => {
@@ -38,77 +18,4 @@ function version () {
3818
`webpack ${require('webpack/package.json').version}`;
3919
}
4020

41-
function status (uri, options, log, useColor) {
42-
const contentBase = Array.isArray(options.contentBase)
43-
? options.contentBase.join(', ')
44-
: options.contentBase;
45-
46-
if (options.socket) {
47-
log.info(`Listening to socket at ${colors.info(useColor, options.socket)}`);
48-
} else {
49-
log.info(`Project is running at ${colors.info(useColor, uri)}`);
50-
}
51-
52-
log.info(
53-
`webpack output is served from ${colors.info(useColor, options.publicPath)}`
54-
);
55-
56-
if (contentBase) {
57-
log.info(
58-
`Content not from webpack is served from ${colors.info(useColor, contentBase)}`
59-
);
60-
}
61-
62-
if (options.historyApiFallback) {
63-
log.info(
64-
`404s will fallback to ${colors.info(useColor, options.historyApiFallback.index || '/index.html')}`
65-
);
66-
}
67-
68-
if (options.bonjour) {
69-
log.info(
70-
'Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'
71-
);
72-
}
73-
74-
if (options.open) {
75-
let openOptions = {};
76-
let openMessage = 'Unable to open browser';
77-
78-
if (typeof options.open === 'string') {
79-
openOptions = { app: options.open };
80-
openMessage += `: ${options.open}`;
81-
}
82-
83-
open(uri + (options.openPage || ''), openOptions).catch(() => {
84-
log.warn(
85-
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
86-
);
87-
});
88-
}
89-
}
90-
91-
function bonjour (options) {
92-
const bonjour = require('bonjour')();
93-
94-
bonjour.publish({
95-
name: 'Webpack Dev Server',
96-
port: options.port,
97-
type: 'http',
98-
subtypes: [ 'webpack' ]
99-
});
100-
101-
process.on('exit', () => {
102-
bonjour.unpublishAll(() => {
103-
bonjour.destroy();
104-
});
105-
});
106-
}
107-
108-
module.exports = {
109-
status,
110-
colors,
111-
version,
112-
bonjour,
113-
defaultTo
114-
};
21+
module.exports = { version, defaultTo };

bin/webpack-dev-server.js

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
const debug = require('debug')('webpack-dev-server');
1717

1818
const fs = require('fs');
19-
const net = require('net');
2019
const path = require('path');
2120

2221
const portfinder = require('portfinder');
@@ -27,19 +26,13 @@ const webpack = require('webpack');
2726

2827
const options = require('./options');
2928

30-
const {
31-
colors,
32-
status,
33-
version,
34-
bonjour,
35-
defaultTo
36-
} = require('./utils');
29+
const { version, defaultTo } = require('./utils');
3730

3831
const Server = require('../lib/Server');
3932

4033
const addEntries = require('../lib/utils/addEntries');
41-
const createDomain = require('../lib/utils/createDomain');
4234
const createLogger = require('../lib/utils/createLogger');
35+
const colors = require('../lib/utils/colors');
4336

4437
let server;
4538

@@ -349,8 +342,6 @@ function startDevServer(config, options) {
349342
}).apply(compiler);
350343
}
351344

352-
const suffix = (options.inline !== false || options.lazy === true ? '/' : '/webpack-dev-server/');
353-
354345
try {
355346
server = new Server(compiler, options, log);
356347
} catch (err) {
@@ -362,63 +353,6 @@ function startDevServer(config, options) {
362353

363354
throw err;
364355
}
365-
366-
if (options.socket) {
367-
server.listeningApp.on('error', (e) => {
368-
if (e.code === 'EADDRINUSE') {
369-
const clientSocket = new net.Socket();
370-
371-
clientSocket.on('error', (err) => {
372-
if (err.code === 'ECONNREFUSED') {
373-
// No other server listening on this socket so it can be safely removed
374-
fs.unlinkSync(options.socket);
375-
376-
server.listen(options.socket, options.host, (error) => {
377-
if (error) {
378-
throw error;
379-
}
380-
});
381-
}
382-
});
383-
384-
clientSocket.connect({ path: options.socket }, () => {
385-
throw new Error('This socket is already used');
386-
});
387-
}
388-
});
389-
390-
server.listen(options.socket, options.host, (err) => {
391-
if (err) {
392-
throw err;
393-
}
394-
// chmod 666 (rw rw rw)
395-
const READ_WRITE = 438;
396-
397-
fs.chmod(options.socket, READ_WRITE, (err) => {
398-
if (err) {
399-
throw err;
400-
}
401-
402-
const uri = createDomain(options, server.listeningApp) + suffix;
403-
404-
status(uri, options, log, argv.color);
405-
});
406-
});
407-
} else {
408-
server.listen(options.port, options.host, (err) => {
409-
if (err) {
410-
throw err;
411-
}
412-
413-
if (options.bonjour) {
414-
bonjour(options);
415-
}
416-
417-
const uri = createDomain(options, server.listeningApp) + suffix;
418-
419-
status(uri, options, log, argv.color);
420-
});
421-
}
422356
}
423357

424358
processOptions(config);

lib/Server.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
const fs = require('fs');
1313
const path = require('path');
14+
const net = require('net');
1415

1516
const ip = require('ip');
1617
const tls = require('tls');
@@ -35,8 +36,12 @@ const historyApiFallback = require('connect-history-api-fallback');
3536
const webpack = require('webpack');
3637
const webpackDevMiddleware = require('webpack-dev-middleware');
3738

38-
const createLogger = require('./utils/createLogger');
39+
const createBonjour = require('./utils/createBonjour');
3940
const createCertificate = require('./utils/createCertificate');
41+
const createDomain = require('../lib/utils/createDomain');
42+
const createLogger = require('./utils/createLogger');
43+
const logStatus = require('./utils/logStatus');
44+
const open = require('./utils/open');
4045

4146
const validateOptions = require('schema-utils');
4247
const schema = require('./options.json');
@@ -608,6 +613,73 @@ function Server (compiler, options = {}, _log) {
608613
websocketProxies.forEach(function (wsProxy) {
609614
this.listeningApp.on('upgrade', wsProxy.upgrade);
610615
}, this);
616+
617+
const suffix = (options.inline !== false || options.lazy === true ? '/' : '/webpack-dev-server/');
618+
619+
if (options.socket) {
620+
this.server.listeningApp.on('error', (e) => {
621+
if (e.code === 'EADDRINUSE') {
622+
const clientSocket = new net.Socket();
623+
624+
clientSocket.on('error', (err) => {
625+
if (err.code === 'ECONNREFUSED') {
626+
// No other server listening on this socket so it can be safely removed
627+
fs.unlinkSync(options.socket);
628+
629+
this.server.listen(options.socket, options.host, (error) => {
630+
if (error) {
631+
throw error;
632+
}
633+
});
634+
}
635+
});
636+
637+
clientSocket.connect({ path: options.socket }, () => {
638+
throw new Error('This socket is already used');
639+
});
640+
}
641+
});
642+
643+
this.server.listen(options.socket, options.host, (err) => {
644+
if (err) {
645+
throw err;
646+
}
647+
// chmod 666 (rw rw rw)
648+
const READ_WRITE = 438;
649+
650+
fs.chmod(options.socket, READ_WRITE, (err) => {
651+
if (err) {
652+
throw err;
653+
}
654+
655+
const uri = createDomain(options, this.listeningApp) + suffix;
656+
657+
logStatus(uri, options, this.log, options.stats.colors);
658+
659+
if (options.open) {
660+
open(uri, options, this.log);
661+
}
662+
});
663+
});
664+
} else {
665+
this.server.listen(options.port, options.host, (err) => {
666+
if (err) {
667+
throw err;
668+
}
669+
670+
if (options.bonjour) {
671+
createBonjour(options);
672+
}
673+
674+
const uri = createDomain(options, this.listeningApp) + suffix;
675+
676+
logStatus(uri, options, this.log, options.stats.colors);
677+
678+
if (options.open) {
679+
open(uri, options, this.log);
680+
}
681+
});
682+
}
611683
}
612684

613685
Server.prototype.use = function () {

lib/utils/colors.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const colors = {
4+
info(useColor, msg) {
5+
if (useColor) {
6+
// Make text blue and bold, so it *pops*
7+
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
8+
}
9+
10+
return msg;
11+
},
12+
error(useColor, msg) {
13+
if (useColor) {
14+
// Make text red and bold, so it *pops*
15+
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
16+
}
17+
18+
return msg;
19+
}
20+
};
21+
22+
module.exports = colors;

lib/utils/createBonjour.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
global-require,
5+
*/
6+
7+
function createBonjour(options) {
8+
const bonjour = require('bonjour')();
9+
10+
bonjour.publish({
11+
name: 'Webpack Dev Server',
12+
port: options.port,
13+
type: 'http',
14+
subtypes: [ 'webpack' ]
15+
});
16+
17+
process.on('exit', () => {
18+
bonjour.unpublishAll(() => {
19+
bonjour.destroy();
20+
});
21+
});
22+
}
23+
24+
module.exports = createBonjour;

lib/utils/logStatus.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
multiline-ternary
5+
*/
6+
7+
const open = require('opn');
8+
const colors = require('./colors');
9+
10+
function logStatus(uri, options, log, useColor) {
11+
const contentBase = Array.isArray(options.contentBase)
12+
? options.contentBase.join(', ')
13+
: options.contentBase;
14+
15+
if (options.socket) {
16+
log.info(`Listening to socket at ${colors.info(useColor, options.socket)}`);
17+
} else {
18+
log.info(`Project is running at ${colors.info(useColor, uri)}`);
19+
}
20+
21+
log.info(
22+
`webpack output is served from ${colors.info(useColor, options.publicPath)}`
23+
);
24+
25+
if (contentBase) {
26+
log.info(
27+
`Content not from webpack is served from ${colors.info(useColor, contentBase)}`
28+
);
29+
}
30+
31+
if (options.historyApiFallback) {
32+
log.info(
33+
`404s will fallback to ${colors.info(useColor, options.historyApiFallback.index || '/index.html')}`
34+
);
35+
}
36+
37+
if (options.bonjour) {
38+
log.info(
39+
'Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'
40+
);
41+
}
42+
}
43+
44+
module.exports = logStatus;

0 commit comments

Comments
 (0)