From 644b5270f981f984331a3fd044394006a9e35386 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Thu, 11 Jul 2019 21:57:36 -0500 Subject: [PATCH 1/3] refactor(server): add update options api helper --- lib/Server.js | 24 ++---- lib/utils/updateOptions.js | 27 +++++++ .../__snapshots__/updateOptions.test.js.snap | 40 ++++++++++ test/server/utils/updateOptions.test.js | 75 +++++++++++++++++++ 4 files changed, 148 insertions(+), 18 deletions(-) create mode 100644 lib/utils/updateOptions.js create mode 100644 test/server/utils/__snapshots__/updateOptions.test.js.snap create mode 100644 test/server/utils/updateOptions.test.js diff --git a/lib/Server.js b/lib/Server.js index 460471bd68..5a35d036a9 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -24,6 +24,7 @@ const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); const validateOptions = require('schema-utils'); const isAbsoluteUrl = require('is-absolute-url'); +const updateOptions = require('./utils/updateOptions'); const updateCompiler = require('./utils/updateCompiler'); const createLogger = require('./utils/createLogger'); const getCertificate = require('./utils/getCertificate'); @@ -58,31 +59,22 @@ class Server { this.compiler = compiler; this.options = options; - // Setup default value - this.options.contentBase = - this.options.contentBase !== undefined - ? this.options.contentBase - : process.cwd(); - this.log = _log || createLogger(options); - // set serverMode default - if (this.options.serverMode === undefined) { - this.options.serverMode = 'sockjs'; - } else { + if (this.options.serverMode !== undefined) { this.log.warn( 'serverMode is an experimental option, meaning its usage could potentially change without warning' ); } - // set clientMode default - if (this.options.clientMode === undefined) { - this.options.clientMode = 'sockjs'; - } else { + + if (this.options.clientMode !== undefined) { this.log.warn( 'clientMode is an experimental option, meaning its usage could potentially change without warning' ); } + updateOptions(this.compiler, this.options); + updateCompiler(this.compiler, this.options); // this.SocketServerImplementation is a class, so it must be instantiated before use @@ -112,10 +104,6 @@ class Server { this.allowedHosts = this.options.allowedHosts; this.disableHostCheck = !!this.options.disableHostCheck; - if (!this.options.watchOptions) { - this.options.watchOptions = {}; - } - this.watchOptions = options.watchOptions || {}; // Replace leading and trailing slashes to normalize path diff --git a/lib/utils/updateOptions.js b/lib/utils/updateOptions.js new file mode 100644 index 0000000000..36cb770cf4 --- /dev/null +++ b/lib/utils/updateOptions.js @@ -0,0 +1,27 @@ +'use strict'; + +/* eslint-disable + no-undefined +*/ + +function updateOptions(compiler, options) { + // Setup default value + options.contentBase = + options.contentBase !== undefined ? options.contentBase : process.cwd(); + + // set serverMode default + if (options.serverMode === undefined) { + options.serverMode = 'sockjs'; + } + + // set clientMode default + if (options.clientMode === undefined) { + options.clientMode = 'sockjs'; + } + + if (!options.watchOptions) { + options.watchOptions = {}; + } +} + +module.exports = updateOptions; diff --git a/test/server/utils/__snapshots__/updateOptions.test.js.snap b/test/server/utils/__snapshots__/updateOptions.test.js.snap new file mode 100644 index 0000000000..0e8ad30035 --- /dev/null +++ b/test/server/utils/__snapshots__/updateOptions.test.js.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`updateOptions contentBase array should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "contentBase": Array [ + "/path/to/dist1", + "/path/to/dist2", + ], + "serverMode": "sockjs", + "watchOptions": Object {}, +} +`; + +exports[`updateOptions contentBase string should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "contentBase": "/path/to/dist", + "serverMode": "sockjs", + "watchOptions": Object {}, +} +`; + +exports[`updateOptions no options should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "serverMode": "sockjs", + "watchOptions": Object {}, +} +`; + +exports[`updateOptions watchOptions should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "serverMode": "sockjs", + "watchOptions": Object { + "poll": true, + }, +} +`; diff --git a/test/server/utils/updateOptions.test.js b/test/server/utils/updateOptions.test.js new file mode 100644 index 0000000000..1bf29d8114 --- /dev/null +++ b/test/server/utils/updateOptions.test.js @@ -0,0 +1,75 @@ +'use strict'; + +const webpack = require('webpack'); +const updateOptions = require('../../../lib/utils/updateOptions'); + +describe('updateOptions', () => { + const cases = [ + { + title: 'no options', + multiCompiler: false, + options: {}, + optionsResults: null, + }, + { + title: 'contentBase string', + multiCompiler: false, + options: { + contentBase: '/path/to/dist', + }, + optionsResults: null, + }, + { + title: 'contentBase array', + multiCompiler: false, + options: { + contentBase: ['/path/to/dist1', '/path/to/dist2'], + }, + optionsResults: null, + }, + { + title: 'watchOptions', + multiCompiler: false, + options: { + watchOptions: { + poll: true, + }, + }, + optionsResults: null, + }, + ]; + + cases.forEach((data) => { + describe(data.title, () => { + let compiler; + beforeAll(() => { + let webpackConfig; + if (data.multiCompiler) { + // eslint-disable-next-line global-require + webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config'); + } else { + // eslint-disable-next-line global-require + webpackConfig = require('../../fixtures/simple-config/webpack.config'); + } + + compiler = webpack(webpackConfig); + }); + + it('should set correct options', () => { + const originalContentBase = data.options.contentBase; + updateOptions(compiler, data.options); + if (data.optionsResults) { + expect(data.options).toEqual(data.optionsResults); + } else { + if (data.options.contentBase !== originalContentBase) { + // we don't want this in the snapshot, because it is + // the current working directory + expect(data.options.contentBase).toEqual(process.cwd()); + delete data.options.contentBase; + } + expect(data.options).toMatchSnapshot(); + } + }); + }); + }); +}); From d19861d0785bc929155e6cca05b45aaefbe67aea Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Mon, 22 Jul 2019 17:02:05 -0500 Subject: [PATCH 2/3] test(server): removed global require eslint comments --- test/server/utils/updateOptions.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/server/utils/updateOptions.test.js b/test/server/utils/updateOptions.test.js index 1bf29d8114..e1c5d63b96 100644 --- a/test/server/utils/updateOptions.test.js +++ b/test/server/utils/updateOptions.test.js @@ -45,10 +45,8 @@ describe('updateOptions', () => { beforeAll(() => { let webpackConfig; if (data.multiCompiler) { - // eslint-disable-next-line global-require webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config'); } else { - // eslint-disable-next-line global-require webpackConfig = require('../../fixtures/simple-config/webpack.config'); } From b4fd742f7e7672ddd3699be28941ef09cc0d2b7b Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Tue, 23 Jul 2019 09:37:18 -0500 Subject: [PATCH 3/3] refactor(server): switch update options to normalize options --- lib/Server.js | 4 ++-- lib/utils/{updateOptions.js => normalizeOptions.js} | 4 ++-- ...Options.test.js.snap => normalizeOptions.test.js.snap} | 8 ++++---- .../{updateOptions.test.js => normalizeOptions.test.js} | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) rename lib/utils/{updateOptions.js => normalizeOptions.js} (85%) rename test/server/utils/__snapshots__/{updateOptions.test.js.snap => normalizeOptions.test.js.snap} (64%) rename test/server/utils/{updateOptions.test.js => normalizeOptions.test.js} (91%) diff --git a/lib/Server.js b/lib/Server.js index 8f4a54930d..0058ebfd47 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -24,7 +24,7 @@ const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); const validateOptions = require('schema-utils'); const isAbsoluteUrl = require('is-absolute-url'); -const updateOptions = require('./utils/updateOptions'); +const normalizeOptions = require('./utils/normalizeOptions'); const updateCompiler = require('./utils/updateCompiler'); const createLogger = require('./utils/createLogger'); const getCertificate = require('./utils/getCertificate'); @@ -73,7 +73,7 @@ class Server { ); } - updateOptions(this.compiler, this.options); + normalizeOptions(this.compiler, this.options); updateCompiler(this.compiler, this.options); diff --git a/lib/utils/updateOptions.js b/lib/utils/normalizeOptions.js similarity index 85% rename from lib/utils/updateOptions.js rename to lib/utils/normalizeOptions.js index 36cb770cf4..443d2df94a 100644 --- a/lib/utils/updateOptions.js +++ b/lib/utils/normalizeOptions.js @@ -4,7 +4,7 @@ no-undefined */ -function updateOptions(compiler, options) { +function normalizeOptions(compiler, options) { // Setup default value options.contentBase = options.contentBase !== undefined ? options.contentBase : process.cwd(); @@ -24,4 +24,4 @@ function updateOptions(compiler, options) { } } -module.exports = updateOptions; +module.exports = normalizeOptions; diff --git a/test/server/utils/__snapshots__/updateOptions.test.js.snap b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap similarity index 64% rename from test/server/utils/__snapshots__/updateOptions.test.js.snap rename to test/server/utils/__snapshots__/normalizeOptions.test.js.snap index 0e8ad30035..0658e08b4b 100644 --- a/test/server/utils/__snapshots__/updateOptions.test.js.snap +++ b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`updateOptions contentBase array should set correct options 1`] = ` +exports[`normalizeOptions contentBase array should set correct options 1`] = ` Object { "clientMode": "sockjs", "contentBase": Array [ @@ -12,7 +12,7 @@ Object { } `; -exports[`updateOptions contentBase string should set correct options 1`] = ` +exports[`normalizeOptions contentBase string should set correct options 1`] = ` Object { "clientMode": "sockjs", "contentBase": "/path/to/dist", @@ -21,7 +21,7 @@ Object { } `; -exports[`updateOptions no options should set correct options 1`] = ` +exports[`normalizeOptions no options should set correct options 1`] = ` Object { "clientMode": "sockjs", "serverMode": "sockjs", @@ -29,7 +29,7 @@ Object { } `; -exports[`updateOptions watchOptions should set correct options 1`] = ` +exports[`normalizeOptions watchOptions should set correct options 1`] = ` Object { "clientMode": "sockjs", "serverMode": "sockjs", diff --git a/test/server/utils/updateOptions.test.js b/test/server/utils/normalizeOptions.test.js similarity index 91% rename from test/server/utils/updateOptions.test.js rename to test/server/utils/normalizeOptions.test.js index e1c5d63b96..c1f66f4939 100644 --- a/test/server/utils/updateOptions.test.js +++ b/test/server/utils/normalizeOptions.test.js @@ -1,9 +1,9 @@ 'use strict'; const webpack = require('webpack'); -const updateOptions = require('../../../lib/utils/updateOptions'); +const normalizeOptions = require('../../../lib/utils/normalizeOptions'); -describe('updateOptions', () => { +describe('normalizeOptions', () => { const cases = [ { title: 'no options', @@ -55,7 +55,7 @@ describe('updateOptions', () => { it('should set correct options', () => { const originalContentBase = data.options.contentBase; - updateOptions(compiler, data.options); + normalizeOptions(compiler, data.options); if (data.optionsResults) { expect(data.options).toEqual(data.optionsResults); } else {