Skip to content

Commit 7e37db5

Browse files
committed
refactor(server): switched to normalize options
1 parent 93d6ca3 commit 7e37db5

File tree

6 files changed

+191
-65
lines changed

6 files changed

+191
-65
lines changed

lib/utils/normalizeOptions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66

77
function normalizeOptions(compiler, options) {
8+
const firstWpOpt = compiler.compilers
9+
? compiler.compilers[0].options
10+
: compiler.options;
11+
812
// Setup default value
913
options.contentBase =
1014
options.contentBase !== undefined ? options.contentBase : process.cwd();
@@ -22,6 +26,10 @@ function normalizeOptions(compiler, options) {
2226
if (!options.watchOptions) {
2327
options.watchOptions = {};
2428
}
29+
30+
if (!options.filename && firstWpOpt.output && firstWpOpt.output.filename) {
31+
options.filename = firstWpOpt.output.filename;
32+
}
2533
}
2634

2735
module.exports = normalizeOptions;

lib/utils/setFilename.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/utils/updateCompiler.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
const webpack = require('webpack');
88
const addEntries = require('./addEntries');
99
const getSocketClientPath = require('./getSocketClientPath');
10-
const setFilename = require('./setFilename');
1110

1211
function updateCompiler(compiler, options) {
13-
setFilename(compiler, options);
14-
1512
if (options.inline !== false) {
1613
const findHMRPlugin = (config) => {
1714
if (!config.plugins) {

test/server/utils/__snapshots__/normalizeOptions.test.js.snap

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Object {
77
"/path/to/dist1",
88
"/path/to/dist2",
99
],
10+
"filename": "[name].js",
1011
"serverMode": "sockjs",
1112
"watchOptions": Object {},
1213
}
@@ -16,6 +17,79 @@ exports[`normalizeOptions contentBase string should set correct options 1`] = `
1617
Object {
1718
"clientMode": "sockjs",
1819
"contentBase": "/path/to/dist",
20+
"filename": "[name].js",
21+
"serverMode": "sockjs",
22+
"watchOptions": Object {},
23+
}
24+
`;
25+
26+
exports[`normalizeOptions existing devServer.filename, existing compiler filename should set correct options 1`] = `
27+
Object {
28+
"clientMode": "sockjs",
29+
"filename": "devserver-bundle.js",
30+
"serverMode": "sockjs",
31+
"watchOptions": Object {},
32+
}
33+
`;
34+
35+
exports[`normalizeOptions existing devServer.filename, no compiler filename should set correct options 1`] = `
36+
Object {
37+
"clientMode": "sockjs",
38+
"filename": "devserver-bundle.js",
39+
"serverMode": "sockjs",
40+
"watchOptions": Object {},
41+
}
42+
`;
43+
44+
exports[`normalizeOptions multi compiler, existing devServer.filename, existing compiler filename should set correct options 1`] = `
45+
Object {
46+
"clientMode": "sockjs",
47+
"filename": "devserver-bundle.js",
48+
"serverMode": "sockjs",
49+
"watchOptions": Object {},
50+
}
51+
`;
52+
53+
exports[`normalizeOptions multi compiler, existing devServer.filename, no compiler filename should set correct options 1`] = `
54+
Object {
55+
"clientMode": "sockjs",
56+
"filename": "devserver-bundle.js",
57+
"serverMode": "sockjs",
58+
"watchOptions": Object {},
59+
}
60+
`;
61+
62+
exports[`normalizeOptions multi compiler, no devServer.filename, existing compiler filename should set correct options 1`] = `
63+
Object {
64+
"clientMode": "sockjs",
65+
"filename": "mybundle.js",
66+
"serverMode": "sockjs",
67+
"watchOptions": Object {},
68+
}
69+
`;
70+
71+
exports[`normalizeOptions multi compiler, no devServer.filename, no compiler filename should set correct options 1`] = `
72+
Object {
73+
"clientMode": "sockjs",
74+
"filename": "[name].js",
75+
"serverMode": "sockjs",
76+
"watchOptions": Object {},
77+
}
78+
`;
79+
80+
exports[`normalizeOptions no devServer.filename, existing compiler filename should set correct options 1`] = `
81+
Object {
82+
"clientMode": "sockjs",
83+
"filename": "mybundle.js",
84+
"serverMode": "sockjs",
85+
"watchOptions": Object {},
86+
}
87+
`;
88+
89+
exports[`normalizeOptions no devServer.filename, no compiler filename should set correct options 1`] = `
90+
Object {
91+
"clientMode": "sockjs",
92+
"filename": "[name].js",
1993
"serverMode": "sockjs",
2094
"watchOptions": Object {},
2195
}
@@ -24,6 +98,7 @@ Object {
2498
exports[`normalizeOptions no options should set correct options 1`] = `
2599
Object {
26100
"clientMode": "sockjs",
101+
"filename": "[name].js",
27102
"serverMode": "sockjs",
28103
"watchOptions": Object {},
29104
}
@@ -32,6 +107,7 @@ Object {
32107
exports[`normalizeOptions watchOptions should set correct options 1`] = `
33108
Object {
34109
"clientMode": "sockjs",
110+
"filename": "[name].js",
35111
"serverMode": "sockjs",
36112
"watchOptions": Object {
37113
"poll": true,

test/server/utils/__snapshots__/setFilename.test.js.snap

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/server/utils/normalizeOptions.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,126 @@ describe('normalizeOptions', () => {
3737
},
3838
optionsResults: null,
3939
},
40+
{
41+
title: 'no devServer.filename, no compiler filename',
42+
webpackConfig: null,
43+
multiCompiler: false,
44+
options: {},
45+
optionsResults: null,
46+
},
47+
{
48+
title: 'no devServer.filename, existing compiler filename',
49+
webpackConfig: {
50+
output: {
51+
filename: 'mybundle.js',
52+
},
53+
},
54+
multiCompiler: false,
55+
options: {},
56+
optionsResults: null,
57+
},
58+
{
59+
title: 'existing devServer.filename, no compiler filename',
60+
webpackConfig: null,
61+
multiCompiler: false,
62+
options: {
63+
filename: 'devserver-bundle.js',
64+
},
65+
optionsResults: null,
66+
},
67+
{
68+
title: 'existing devServer.filename, existing compiler filename',
69+
webpackConfig: {
70+
output: {
71+
filename: 'mybundle.js',
72+
},
73+
},
74+
multiCompiler: false,
75+
options: {
76+
filename: 'devserver-bundle.js',
77+
},
78+
optionsResults: null,
79+
},
80+
{
81+
title: 'multi compiler, no devServer.filename, no compiler filename',
82+
webpackConfig: null,
83+
multiCompiler: true,
84+
options: {},
85+
optionsResults: null,
86+
},
87+
{
88+
title:
89+
'multi compiler, no devServer.filename, existing compiler filename',
90+
webpackConfig: {
91+
output: {
92+
filename: 'mybundle.js',
93+
},
94+
},
95+
multiCompiler: true,
96+
options: {},
97+
optionsResults: null,
98+
},
99+
{
100+
title:
101+
'multi compiler, existing devServer.filename, no compiler filename',
102+
webpackConfig: null,
103+
multiCompiler: true,
104+
options: {
105+
filename: 'devserver-bundle.js',
106+
},
107+
optionsResults: null,
108+
},
109+
{
110+
title:
111+
'multi compiler, existing devServer.filename, existing compiler filename',
112+
webpackConfig: {
113+
output: {
114+
filename: 'mybundle.js',
115+
},
116+
},
117+
multiCompiler: true,
118+
options: {
119+
filename: 'devserver-bundle.js',
120+
},
121+
optionsResults: null,
122+
},
40123
];
41124

42125
cases.forEach((data) => {
43126
describe(data.title, () => {
44127
let compiler;
45128
beforeAll(() => {
129+
// this will merge webpack configs through a depth of one layer of objects,
130+
// specifically so that the webpack config output object can be merged
131+
const mergeConfigs = (baseConfig, config) => {
132+
Object.keys(config).forEach((key1) => {
133+
if (typeof config[key1] === 'object') {
134+
Object.keys(config[key1]).forEach((key2) => {
135+
if (!baseConfig[key1]) {
136+
baseConfig[key1] = {};
137+
}
138+
baseConfig[key1][key2] = config[key1][key2];
139+
});
140+
} else {
141+
baseConfig[key1] = config[key1];
142+
}
143+
});
144+
};
145+
46146
let webpackConfig;
47147
if (data.multiCompiler) {
48148
webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config');
49149
} else {
50150
webpackConfig = require('../../fixtures/simple-config/webpack.config');
51151
}
52152

153+
if (data.webpackConfig) {
154+
mergeConfigs(
155+
data.multiCompiler ? webpackConfig[0] : webpackConfig,
156+
data.webpackConfig
157+
);
158+
}
159+
53160
compiler = webpack(webpackConfig);
54161
});
55162

0 commit comments

Comments
 (0)