Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bonjour options #3202

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@
},
"properties": {
"bonjour": {
"type": "boolean",
"anyOf": [
{
"type": "boolean"
},
{
"type": "object"
}
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
],
"description": "Broadcasts the server via ZeroConf networking on start. https://webpack.js.org/configuration/dev-server/#devserverbonjour"
},
"client": {
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/runBonjour.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

function runBonjour({ port }) {
function runBonjour(options) {
const bonjour = require('bonjour')();
const os = require('os');

const { https, port } = options;
bonjour.publish({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'http',
type: https ? 'https' : 'http',
subtypes: ['webpack'],
...options.bonjour,
});

process.on('exit', () => {
Expand Down
9 changes: 7 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

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

exports[`options validate should throw an error on the "client" option with '{"host":true,"path":"","port":8080}' value 1`] = `
Expand Down
9 changes: 7 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

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

exports[`options validate should throw an error on the "client" option with '{"host":true,"path":"","port":8080}' value 1`] = `
Expand Down
106 changes: 86 additions & 20 deletions test/server/bonjour-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,97 @@ describe('bonjour option', () => {
});
});

beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: true,
afterEach(() => {
mockPublish.mockReset();
mockUnpublishAll.mockReset();
});

describe('http', () => {
beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: true,
port,
},
done
);
});

afterEach((done) => {
server.close(done);
});

it('should call bonjour with correct params', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
},
done
);
type: 'http',
subtypes: ['webpack'],
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
});

afterEach((done) => {
mockPublish.mockReset();
mockUnpublishAll.mockReset();
server.close(done);
describe('https option', () => {
beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: true,
port,
https: true,
},
done
);
});

afterEach((done) => {
server.close(done);
});

it('bonjour should use https when passed in option', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'https',
subtypes: ['webpack'],
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
});

it('should call bonjour with correct params', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'http',
subtypes: ['webpack'],
describe('bonjour object', () => {
beforeEach((done) => {
server = testServer.start(
config,
{
bonjour: {
type: 'https',
protocol: 'udp',
},
port,
},
done
);
});

afterEach((done) => {
server.close(done);
});

it('applies bonjour options', () => {
expect(mockPublish).toHaveBeenCalledTimes(1);
expect(mockPublish).toHaveBeenCalledWith({
name: `Webpack Dev Server ${os.hostname()}:${port}`,
port,
type: 'https',
protocol: 'udp',
subtypes: ['webpack'],
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
expect(mockUnpublishAll).toHaveBeenCalledTimes(0);
});
});
2 changes: 1 addition & 1 deletion test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const tests = {
failure: [false],
},
bonjour: {
success: [false, true],
success: [false, true, { type: 'https' }],
failure: [''],
},
client: {
Expand Down