Skip to content

Commit c9fe53d

Browse files
samuraisamshellscape
authored andcommitted
zeroconf dns (bonjour) service publishing (#930)
* zeroconf dns (bonjour) service publishing * address code style issues * zeroconf: cleanup console.logs and fix broken test * bonjour: add test file
1 parent 14d77a5 commit c9fe53d

File tree

8 files changed

+55
-1
lines changed

8 files changed

+55
-1
lines changed

bin/webpack-dev-server.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const net = require("net");
88
const portfinder = require("portfinder");
99
const addDevServerEntrypoints = require("../lib/util/addDevServerEntrypoints");
1010
const createDomain = require("../lib/util/createDomain");
11+
const bonjour = require("bonjour")();
1112

1213
// Local version replaces global one
1314
try {
@@ -64,6 +65,10 @@ const BASIC_GROUP = "Basic options:";
6465
const DEFAULT_PORT = 8080;
6566

6667
yargs.options({
68+
"bonjour": {
69+
type: "boolean",
70+
describe: "Broadcasts the server via ZeroConf networking on start"
71+
},
6772
"lazy": {
6873
type: "boolean",
6974
describe: "Lazy"
@@ -214,6 +219,9 @@ function processOptions(wpOpt) {
214219

215220
const options = wpOpt.devServer || firstWpOpt.devServer || {};
216221

222+
if(argv.bonjour)
223+
options.bonjour = true;
224+
217225
if(argv.host !== "localhost" || !options.host)
218226
options.host = argv.host;
219227

@@ -410,6 +418,7 @@ function startDevServer(wpOpt, options) {
410418
} else {
411419
server.listen(options.port, options.host, function(err) {
412420
if(err) throw err;
421+
if(options.bonjour) broadcastZeroconf(options);
413422
reportReadiness(uri, options);
414423
});
415424
}
@@ -434,6 +443,22 @@ function reportReadiness(uri, options) {
434443
console.log("Unable to open browser. If you are running in a headless environment, please do not use the open flag.");
435444
});
436445
}
446+
if(options.bonjour)
447+
console.log("Broadcasting \"http\" with subtype of \"webpack\" via ZeroConf DNS (Bonjour)");
448+
}
449+
450+
function broadcastZeroconf(options) {
451+
bonjour.publish({
452+
name: "Webpack Dev Server",
453+
port: options.port,
454+
type: "http",
455+
subtypes: ["webpack"]
456+
});
457+
process.on("exit", function() {
458+
bonjour.unpublishAll(function() {
459+
bonjour.destroy();
460+
});
461+
});
437462
}
438463

439464
processOptions(wpOpt);

examples/bonjour/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# host and port
2+
3+
For basic usage
4+
```shell
5+
node ../../bin/webpack-dev-server.js --bonjour
6+
```
7+
8+
## What should happen
9+
10+
It should publish a zeroconf service with a type of `http` and a subtype of `webpack`.

examples/bonjour/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.write("It's working.");

examples/bonjour/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="bundle.js" type="text/javascript" charset="utf-8"></script>
5+
</head>
6+
<body>
7+
<h1>Example: host and port</h1>
8+
</body>
9+
</html>

examples/bonjour/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
context: __dirname,
3+
entry: "./app.js",
4+
}

lib/optionsSchema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
"description": "Disables watch mode and recompiles bundle only on a request.",
1414
"type": "boolean"
1515
},
16+
"bonjour": {
17+
"description": "Publishes the ZeroConf DNS service",
18+
"type": "boolean"
19+
},
1620
"host": {
1721
"description": "The host the server listens to.",
1822
"type": "string"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"ansi-html": "0.0.7",
11+
"bonjour": "^3.5.0",
1112
"chokidar": "^1.6.0",
1213
"compression": "^1.5.2",
1314
"connect-history-api-fallback": "^1.3.0",

test/Validation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("Validation", function() {
4848
config: { asdf: true },
4949
message: [
5050
" - configuration has an unknown property 'asdf'. These properties are valid:",
51-
" object { hot?, hotOnly?, lazy?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
51+
" object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
5252
"watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, " +
5353
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, openPage?, features?, " +
5454
"compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, " +

0 commit comments

Comments
 (0)