From 216b0d3c71a617eeef32cff523f95fcd8916af72 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Wed, 26 May 2021 21:20:00 +0300 Subject: [PATCH] fix!: host can't be `null` or empty string (#3352) --- lib/options.json | 10 ++---- lib/utils/DevServerPlugin.js | 2 +- .../validate-options.test.js.snap.webpack4 | 20 ++++++++---- .../validate-options.test.js.snap.webpack5 | 20 ++++++++---- test/server/host-option.test.js | 31 ------------------- test/validate-options.test.js | 4 +-- 6 files changed, 33 insertions(+), 54 deletions(-) diff --git a/lib/options.json b/lib/options.json index f8fb2eb120..c33bad5d2e 100644 --- a/lib/options.json +++ b/lib/options.json @@ -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": { diff --git a/lib/utils/DevServerPlugin.js b/lib/utils/DevServerPlugin.js index b57b132c04..2df1f3e2f0 100644 --- a/lib/utils/DevServerPlugin.js +++ b/lib/utils/DevServerPlugin.js @@ -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 diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack4 b/test/__snapshots__/validate-options.test.js.snap.webpack4 index 1aa6a376e2..8f753d6415 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack4 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack4 @@ -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`] = ` diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack5 b/test/__snapshots__/validate-options.test.js.snap.webpack5 index 1aa6a376e2..8f753d6415 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack5 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack5 @@ -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`] = ` diff --git a/test/server/host-option.test.js b/test/server/host-option.test.js index 88418a3e78..f473f1d0d5 100644 --- a/test/server/host-option.test.js +++ b/test/server/host-option.test.js @@ -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( diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 9b7455fce3..3593cdb6de 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -175,8 +175,8 @@ const tests = { failure: [''], }, host: { - success: ['', 'localhost', '::', '::1', null], - failure: [false], + success: ['localhost', '::', '::1'], + failure: [false, '', null], }, hot: { success: [true, 'only'],