From 437c8d3be7041e4203920d3d8a6af5a8071406fd Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 15 May 2021 17:49:42 +0530 Subject: [PATCH] feat: add port "auto" and remove port: null (#3297) --- lib/options.json | 4 ++-- lib/utils/findPort.js | 2 +- .../validate-options.test.js.snap.webpack4 | 19 +++++++++++++++---- .../validate-options.test.js.snap.webpack5 | 19 +++++++++++++++---- test/server/port-option.test.js | 4 ++-- test/validate-options.test.js | 6 +++--- 6 files changed, 38 insertions(+), 16 deletions(-) diff --git a/lib/options.json b/lib/options.json index 95d3a47658..cd6d505962 100644 --- a/lib/options.json +++ b/lib/options.json @@ -174,7 +174,7 @@ "type": "string" }, { - "type": "null" + "enum": ["auto"] } ], "description": "Tells clients connected to devServer to use the provided port." @@ -424,7 +424,7 @@ "type": "string" }, { - "type": "null" + "enum": ["auto"] } ], "description": "Specify a port number to listen for requests on. https://webpack.js.org/configuration/dev-server/#devserverport" diff --git a/lib/utils/findPort.js b/lib/utils/findPort.js index e4bd84e9a2..7257d46ae9 100644 --- a/lib/utils/findPort.js +++ b/lib/utils/findPort.js @@ -18,7 +18,7 @@ function runPortFinder() { } function findPort(port) { - if (port) { + if (port && port !== 'auto') { return Promise.resolve(port); } diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack4 b/test/__snapshots__/validate-options.test.js.snap.webpack4 index db08b53a2a..02263763a0 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack4 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack4 @@ -82,12 +82,12 @@ exports[`options validate should throw an error on the "client" option with '{"p exports[`options validate should throw an error on the "client" option with '{"port":true}' value 1`] = ` "ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema. - configuration.client.port should be one of these: - number | string | null + number | string | \\"auto\\" -> Tells clients connected to devServer to use the provided port. Details: * configuration.client.port should be a number. * configuration.client.port should be a string. - * configuration.client.port should be a null." + * configuration.client.port should be \\"auto\\"." `; exports[`options validate should throw an error on the "client" option with '{"progress":""}' value 1`] = ` @@ -361,12 +361,23 @@ exports[`options validate should throw an error on the "open" option with '{"tar exports[`options validate should throw an error on the "port" 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.port should be one of these: - number | string | null + number | string | \\"auto\\" -> Specify a port number to listen for requests on. https://webpack.js.org/configuration/dev-server/#devserverport Details: * configuration.port should be a number. * configuration.port should be a string. - * configuration.port should be a null." + * configuration.port should be \\"auto\\"." +`; + +exports[`options validate should throw an error on the "port" 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.port should be one of these: + number | string | \\"auto\\" + -> Specify a port number to listen for requests on. https://webpack.js.org/configuration/dev-server/#devserverport + Details: + * configuration.port should be a number. + * configuration.port should be a string. + * configuration.port should be \\"auto\\"." `; exports[`options validate should throw an error on the "proxy" 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 db08b53a2a..02263763a0 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack5 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack5 @@ -82,12 +82,12 @@ exports[`options validate should throw an error on the "client" option with '{"p exports[`options validate should throw an error on the "client" option with '{"port":true}' value 1`] = ` "ValidationError: Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema. - configuration.client.port should be one of these: - number | string | null + number | string | \\"auto\\" -> Tells clients connected to devServer to use the provided port. Details: * configuration.client.port should be a number. * configuration.client.port should be a string. - * configuration.client.port should be a null." + * configuration.client.port should be \\"auto\\"." `; exports[`options validate should throw an error on the "client" option with '{"progress":""}' value 1`] = ` @@ -361,12 +361,23 @@ exports[`options validate should throw an error on the "open" option with '{"tar exports[`options validate should throw an error on the "port" 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.port should be one of these: - number | string | null + number | string | \\"auto\\" -> Specify a port number to listen for requests on. https://webpack.js.org/configuration/dev-server/#devserverport Details: * configuration.port should be a number. * configuration.port should be a string. - * configuration.port should be a null." + * configuration.port should be \\"auto\\"." +`; + +exports[`options validate should throw an error on the "port" 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.port should be one of these: + number | string | \\"auto\\" + -> Specify a port number to listen for requests on. https://webpack.js.org/configuration/dev-server/#devserverport + Details: + * configuration.port should be a number. + * configuration.port should be a string. + * configuration.port should be \\"auto\\"." `; exports[`options validate should throw an error on the "proxy" option with '[]' value 1`] = ` diff --git a/test/server/port-option.test.js b/test/server/port-option.test.js index 5e2d5019cb..1874fca8d3 100644 --- a/test/server/port-option.test.js +++ b/test/server/port-option.test.js @@ -78,7 +78,7 @@ describe('port', () => { afterAll(testServer.close); }); - describe('is null', () => { + describe('is auto', () => { beforeAll((done) => { server = testServer.start( config, @@ -87,7 +87,7 @@ describe('port', () => { directory: staticDirectory, watch: false, }, - port: null, + port: 'auto', }, done ); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 9500875bbb..e706c15c6e 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -70,7 +70,7 @@ const tests = { { host: '', path: '', - port: null, + port: 'auto', }, { progress: false, @@ -247,8 +247,8 @@ const tests = { failure: ['', [], { foo: 'bar' }, { target: 90 }, { app: true }], }, port: { - success: ['', 0, null], - failure: [false], + success: ['', 0, 'auto'], + failure: [false, null], }, proxy: { success: [