diff --git a/lib/Server.js b/lib/Server.js index b5444d6eb6..0058ebfd47 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 normalizeOptions = require('./utils/normalizeOptions'); 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' ); } + normalizeOptions(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/normalizeOptions.js b/lib/utils/normalizeOptions.js new file mode 100644 index 0000000000..443d2df94a --- /dev/null +++ b/lib/utils/normalizeOptions.js @@ -0,0 +1,27 @@ +'use strict'; + +/* eslint-disable + no-undefined +*/ + +function normalizeOptions(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 = normalizeOptions; diff --git a/test/server/utils/__snapshots__/normalizeOptions.test.js.snap b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap new file mode 100644 index 0000000000..0658e08b4b --- /dev/null +++ b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`normalizeOptions contentBase array should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "contentBase": Array [ + "/path/to/dist1", + "/path/to/dist2", + ], + "serverMode": "sockjs", + "watchOptions": Object {}, +} +`; + +exports[`normalizeOptions contentBase string should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "contentBase": "/path/to/dist", + "serverMode": "sockjs", + "watchOptions": Object {}, +} +`; + +exports[`normalizeOptions no options should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "serverMode": "sockjs", + "watchOptions": Object {}, +} +`; + +exports[`normalizeOptions watchOptions should set correct options 1`] = ` +Object { + "clientMode": "sockjs", + "serverMode": "sockjs", + "watchOptions": Object { + "poll": true, + }, +} +`; diff --git a/test/server/utils/normalizeOptions.test.js b/test/server/utils/normalizeOptions.test.js new file mode 100644 index 0000000000..c1f66f4939 --- /dev/null +++ b/test/server/utils/normalizeOptions.test.js @@ -0,0 +1,73 @@ +'use strict'; + +const webpack = require('webpack'); +const normalizeOptions = require('../../../lib/utils/normalizeOptions'); + +describe('normalizeOptions', () => { + 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) { + webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config'); + } else { + webpackConfig = require('../../fixtures/simple-config/webpack.config'); + } + + compiler = webpack(webpackConfig); + }); + + it('should set correct options', () => { + const originalContentBase = data.options.contentBase; + normalizeOptions(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(); + } + }); + }); + }); +});