Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server): add updateOptions API helper #2117

Merged
merged 6 commits into from Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 updateOptions = require('./utils/updateOptions');
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'
);
}

updateOptions(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/updateOptions.js
@@ -0,0 +1,27 @@
'use strict';

/* eslint-disable
no-undefined
*/

function updateOptions(compiler, options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Loonride maybe normalizeOptions better name here? Anyway code looks good

// 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;
40 changes: 40 additions & 0 deletions 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,
},
}
`;
73 changes: 73 additions & 0 deletions test/server/utils/updateOptions.test.js
@@ -0,0 +1,73 @@
'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) {
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;
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();
}
});
});
});
});