Skip to content

Commit

Permalink
fix!: host can't be null or empty string (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 26, 2021
1 parent 7169c01 commit 216b0d3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 54 deletions.
10 changes: 2 additions & 8 deletions lib/options.json
Expand Up @@ -375,14 +375,8 @@
"description": "When using the HTML5 History API, the index.html page will likely have to be served in place of any 404 responses. https://webpack.js.org/configuration/dev-server/#devserverhistoryapifallback"
},
"host": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"type": "string",
"minLength": 1,
"description": "Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
},
"hot": {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/DevServerPlugin.js
Expand Up @@ -50,7 +50,7 @@ class DevServerPlugin {
host = options.webSocketServer.options.host;
}
// The `host` option is specified
else if (typeof this.options.host !== 'undefined' && this.options.host) {
else if (typeof this.options.host !== 'undefined') {
host = this.options.host;
}
// The `port` option is not specified
Expand Down
20 changes: 14 additions & 6 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Expand Up @@ -190,14 +190,22 @@ exports[`options validate should throw an error on the "historyApiFallback" opti
object { … }"
`;

exports[`options validate should throw an error on the "host" option with '' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.host should be an non-empty string.
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
`;

exports[`options validate should throw an error on the "host" option with 'false' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.host should be one of these:
string | null
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost
Details:
* configuration.host should be a string.
* configuration.host should be a null."
- configuration.host should be a non-empty string.
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
`;

exports[`options validate should throw an error on the "host" option with 'null' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.host should be a non-empty string.
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
`;

exports[`options validate should throw an error on the "hot" option with '' value 1`] = `
Expand Down
20 changes: 14 additions & 6 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Expand Up @@ -190,14 +190,22 @@ exports[`options validate should throw an error on the "historyApiFallback" opti
object { … }"
`;

exports[`options validate should throw an error on the "host" option with '' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.host should be an non-empty string.
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
`;

exports[`options validate should throw an error on the "host" option with 'false' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.host should be one of these:
string | null
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost
Details:
* configuration.host should be a string.
* configuration.host should be a null."
- configuration.host should be a non-empty string.
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
`;

exports[`options validate should throw an error on the "host" option with 'null' value 1`] = `
"ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration.host should be a non-empty string.
-> Specify a host to use. If you want your server to be accessible externally. https://webpack.js.org/configuration/dev-server/#devserverhost"
`;

exports[`options validate should throw an error on the "hot" option with '' value 1`] = `
Expand Down
31 changes: 0 additions & 31 deletions test/server/host-option.test.js
Expand Up @@ -78,37 +78,6 @@ describe('host option', () => {
afterAll(testServer.close);
});

describe('is null', () => {
beforeAll((done) => {
server = testServer.start(
config,
{
static: {
directory: staticDirectory,
watch: false,
},
host: null,
port,
},
done
);
req = request(server.app);
});

it('server address', () => {
const address = server.server.address();

expect(address.address).toBe('::');
expect(address.port).toBe(port);
});

it('Request to index', (done) => {
req.get('/').expect(200, done);
});

afterAll(testServer.close);
});

describe('is 127.0.0.1 (IPv4)', () => {
beforeAll((done) => {
server = testServer.start(
Expand Down
4 changes: 2 additions & 2 deletions test/validate-options.test.js
Expand Up @@ -175,8 +175,8 @@ const tests = {
failure: [''],
},
host: {
success: ['', 'localhost', '::', '::1', null],
failure: [false],
success: ['localhost', '::', '::1'],
failure: [false, '', null],
},
hot: {
success: [true, 'only'],
Expand Down

0 comments on commit 216b0d3

Please sign in to comment.