Skip to content

Commit 5534583

Browse files
authored
feat: support bonjour options (#3202)
1 parent 4b487f8 commit 5534583

6 files changed

+113
-29
lines changed

lib/options.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@
140140
},
141141
"properties": {
142142
"bonjour": {
143-
"type": "boolean",
143+
"anyOf": [
144+
{
145+
"type": "boolean"
146+
},
147+
{
148+
"type": "object"
149+
}
150+
],
144151
"description": "Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
145152
},
146153
"client": {

lib/utils/runBonjour.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
22

3-
function runBonjour({ port }) {
3+
function runBonjour(options) {
44
const bonjour = require('bonjour')();
55
const os = require('os');
6-
6+
const { https, port } = options;
77
bonjour.publish({
88
name: `Webpack Dev Server ${os.hostname()}:${port}`,
99
port,
10-
type: 'http',
10+
type: https ? 'https' : 'http',
1111
subtypes: ['webpack'],
12+
...options.bonjour,
1213
});
1314

1415
process.on('exit', () => {

test/__snapshots__/validate-options.test.js.snap.webpack4

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
exports[`options validate should throw an error on the "bonjour" option with '' value 1`] = `
44
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
5-
- configuration.bonjour should be a boolean.
6-
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
5+
- configuration.bonjour should be one of these:
6+
boolean | object { … }
7+
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour
8+
Details:
9+
* configuration.bonjour should be a boolean.
10+
* configuration.bonjour should be an object:
11+
object { … }"
712
`;
813

914
exports[`options validate should throw an error on the "client" option with '{"host":true,"path":"","port":8080}' value 1`] = `

test/__snapshots__/validate-options.test.js.snap.webpack5

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
exports[`options validate should throw an error on the "bonjour" option with '' value 1`] = `
44
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
5-
- configuration.bonjour should be a boolean.
6-
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
5+
- configuration.bonjour should be one of these:
6+
boolean | object { … }
7+
-> Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour
8+
Details:
9+
* configuration.bonjour should be a boolean.
10+
* configuration.bonjour should be an object:
11+
object { … }"
712
`;
813

914
exports[`options validate should throw an error on the "client" option with '{"host":true,"path":"","port":8080}' value 1`] = `

test/server/bonjour-option.test.js

+86-20
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,97 @@ describe('bonjour option', () => {
1919
});
2020
});
2121

22-
beforeEach((done) => {
23-
server = testServer.start(
24-
config,
25-
{
26-
bonjour: true,
22+
afterEach(() => {
23+
mockPublish.mockReset();
24+
mockUnpublishAll.mockReset();
25+
});
26+
27+
describe('http', () => {
28+
beforeEach((done) => {
29+
server = testServer.start(
30+
config,
31+
{
32+
bonjour: true,
33+
port,
34+
},
35+
done
36+
);
37+
});
38+
39+
afterEach((done) => {
40+
server.close(done);
41+
});
42+
43+
it('should call bonjour with correct params', () => {
44+
expect(mockPublish).toHaveBeenCalledTimes(1);
45+
expect(mockPublish).toHaveBeenCalledWith({
46+
name: `Webpack Dev Server ${os.hostname()}:${port}`,
2747
port,
28-
},
29-
done
30-
);
48+
type: 'http',
49+
subtypes: ['webpack'],
50+
});
51+
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
52+
});
3153
});
3254

33-
afterEach((done) => {
34-
mockPublish.mockReset();
35-
mockUnpublishAll.mockReset();
36-
server.close(done);
55+
describe('https option', () => {
56+
beforeEach((done) => {
57+
server = testServer.start(
58+
config,
59+
{
60+
bonjour: true,
61+
port,
62+
https: true,
63+
},
64+
done
65+
);
66+
});
67+
68+
afterEach((done) => {
69+
server.close(done);
70+
});
71+
72+
it('bonjour should use https when passed in option', () => {
73+
expect(mockPublish).toHaveBeenCalledTimes(1);
74+
expect(mockPublish).toHaveBeenCalledWith({
75+
name: `Webpack Dev Server ${os.hostname()}:${port}`,
76+
port,
77+
type: 'https',
78+
subtypes: ['webpack'],
79+
});
80+
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
81+
});
3782
});
3883

39-
it('should call bonjour with correct params', () => {
40-
expect(mockPublish).toHaveBeenCalledTimes(1);
41-
expect(mockPublish).toHaveBeenCalledWith({
42-
name: `Webpack Dev Server ${os.hostname()}:${port}`,
43-
port,
44-
type: 'http',
45-
subtypes: ['webpack'],
84+
describe('bonjour object', () => {
85+
beforeEach((done) => {
86+
server = testServer.start(
87+
config,
88+
{
89+
bonjour: {
90+
type: 'https',
91+
protocol: 'udp',
92+
},
93+
port,
94+
},
95+
done
96+
);
97+
});
98+
99+
afterEach((done) => {
100+
server.close(done);
101+
});
102+
103+
it('applies bonjour options', () => {
104+
expect(mockPublish).toHaveBeenCalledTimes(1);
105+
expect(mockPublish).toHaveBeenCalledWith({
106+
name: `Webpack Dev Server ${os.hostname()}:${port}`,
107+
port,
108+
type: 'https',
109+
protocol: 'udp',
110+
subtypes: ['webpack'],
111+
});
112+
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
46113
});
47-
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
48114
});
49115
});

test/validate-options.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const tests = {
2323
failure: [false],
2424
},
2525
bonjour: {
26-
success: [false, true],
26+
success: [false, true, { type: 'https' }],
2727
failure: [''],
2828
},
2929
client: {

0 commit comments

Comments
 (0)