Skip to content

Commit

Permalink
refactor(server): add updateOptions API helper (webpack#2117)
Browse files Browse the repository at this point in the history
* refactor(server): add update options api helper

* test(server): removed global require eslint comments

* refactor(server): switch update options to normalize options
  • Loading branch information
knagaitsev committed Jul 31, 2019
1 parent 4ce5dcf commit 4735879
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 18 deletions.
24 changes: 6 additions & 18 deletions lib/Server.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions 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;
40 changes: 40 additions & 0 deletions 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,
},
}
`;
73 changes: 73 additions & 0 deletions 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();
}
});
});
});
});

0 comments on commit 4735879

Please sign in to comment.