Skip to content

Commit 4735879

Browse files
committed
refactor(server): add updateOptions API helper (webpack#2117)
* refactor(server): add update options api helper * test(server): removed global require eslint comments * refactor(server): switch update options to normalize options
1 parent 4ce5dcf commit 4735879

File tree

4 files changed

+146
-18
lines changed

4 files changed

+146
-18
lines changed

lib/Server.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const webpack = require('webpack');
2424
const webpackDevMiddleware = require('webpack-dev-middleware');
2525
const validateOptions = require('schema-utils');
2626
const isAbsoluteUrl = require('is-absolute-url');
27+
const normalizeOptions = require('./utils/normalizeOptions');
2728
const updateCompiler = require('./utils/updateCompiler');
2829
const createLogger = require('./utils/createLogger');
2930
const getCertificate = require('./utils/getCertificate');
@@ -58,31 +59,22 @@ class Server {
5859
this.compiler = compiler;
5960
this.options = options;
6061

61-
// Setup default value
62-
this.options.contentBase =
63-
this.options.contentBase !== undefined
64-
? this.options.contentBase
65-
: process.cwd();
66-
6762
this.log = _log || createLogger(options);
6863

69-
// set serverMode default
70-
if (this.options.serverMode === undefined) {
71-
this.options.serverMode = 'sockjs';
72-
} else {
64+
if (this.options.serverMode !== undefined) {
7365
this.log.warn(
7466
'serverMode is an experimental option, meaning its usage could potentially change without warning'
7567
);
7668
}
77-
// set clientMode default
78-
if (this.options.clientMode === undefined) {
79-
this.options.clientMode = 'sockjs';
80-
} else {
69+
70+
if (this.options.clientMode !== undefined) {
8171
this.log.warn(
8272
'clientMode is an experimental option, meaning its usage could potentially change without warning'
8373
);
8474
}
8575

76+
normalizeOptions(this.compiler, this.options);
77+
8678
updateCompiler(this.compiler, this.options);
8779

8880
// this.SocketServerImplementation is a class, so it must be instantiated before use
@@ -112,10 +104,6 @@ class Server {
112104
this.allowedHosts = this.options.allowedHosts;
113105
this.disableHostCheck = !!this.options.disableHostCheck;
114106

115-
if (!this.options.watchOptions) {
116-
this.options.watchOptions = {};
117-
}
118-
119107
this.watchOptions = options.watchOptions || {};
120108

121109
// Replace leading and trailing slashes to normalize path

lib/utils/normalizeOptions.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
no-undefined
5+
*/
6+
7+
function normalizeOptions(compiler, options) {
8+
// Setup default value
9+
options.contentBase =
10+
options.contentBase !== undefined ? options.contentBase : process.cwd();
11+
12+
// set serverMode default
13+
if (options.serverMode === undefined) {
14+
options.serverMode = 'sockjs';
15+
}
16+
17+
// set clientMode default
18+
if (options.clientMode === undefined) {
19+
options.clientMode = 'sockjs';
20+
}
21+
22+
if (!options.watchOptions) {
23+
options.watchOptions = {};
24+
}
25+
}
26+
27+
module.exports = normalizeOptions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`normalizeOptions contentBase array should set correct options 1`] = `
4+
Object {
5+
"clientMode": "sockjs",
6+
"contentBase": Array [
7+
"/path/to/dist1",
8+
"/path/to/dist2",
9+
],
10+
"serverMode": "sockjs",
11+
"watchOptions": Object {},
12+
}
13+
`;
14+
15+
exports[`normalizeOptions contentBase string should set correct options 1`] = `
16+
Object {
17+
"clientMode": "sockjs",
18+
"contentBase": "/path/to/dist",
19+
"serverMode": "sockjs",
20+
"watchOptions": Object {},
21+
}
22+
`;
23+
24+
exports[`normalizeOptions no options should set correct options 1`] = `
25+
Object {
26+
"clientMode": "sockjs",
27+
"serverMode": "sockjs",
28+
"watchOptions": Object {},
29+
}
30+
`;
31+
32+
exports[`normalizeOptions watchOptions should set correct options 1`] = `
33+
Object {
34+
"clientMode": "sockjs",
35+
"serverMode": "sockjs",
36+
"watchOptions": Object {
37+
"poll": true,
38+
},
39+
}
40+
`;
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
const webpack = require('webpack');
4+
const normalizeOptions = require('../../../lib/utils/normalizeOptions');
5+
6+
describe('normalizeOptions', () => {
7+
const cases = [
8+
{
9+
title: 'no options',
10+
multiCompiler: false,
11+
options: {},
12+
optionsResults: null,
13+
},
14+
{
15+
title: 'contentBase string',
16+
multiCompiler: false,
17+
options: {
18+
contentBase: '/path/to/dist',
19+
},
20+
optionsResults: null,
21+
},
22+
{
23+
title: 'contentBase array',
24+
multiCompiler: false,
25+
options: {
26+
contentBase: ['/path/to/dist1', '/path/to/dist2'],
27+
},
28+
optionsResults: null,
29+
},
30+
{
31+
title: 'watchOptions',
32+
multiCompiler: false,
33+
options: {
34+
watchOptions: {
35+
poll: true,
36+
},
37+
},
38+
optionsResults: null,
39+
},
40+
];
41+
42+
cases.forEach((data) => {
43+
describe(data.title, () => {
44+
let compiler;
45+
beforeAll(() => {
46+
let webpackConfig;
47+
if (data.multiCompiler) {
48+
webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config');
49+
} else {
50+
webpackConfig = require('../../fixtures/simple-config/webpack.config');
51+
}
52+
53+
compiler = webpack(webpackConfig);
54+
});
55+
56+
it('should set correct options', () => {
57+
const originalContentBase = data.options.contentBase;
58+
normalizeOptions(compiler, data.options);
59+
if (data.optionsResults) {
60+
expect(data.options).toEqual(data.optionsResults);
61+
} else {
62+
if (data.options.contentBase !== originalContentBase) {
63+
// we don't want this in the snapshot, because it is
64+
// the current working directory
65+
expect(data.options.contentBase).toEqual(process.cwd());
66+
delete data.options.contentBase;
67+
}
68+
expect(data.options).toMatchSnapshot();
69+
}
70+
});
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)