Skip to content

Commit d0c07dd

Browse files
committed
refactor(server): move filename option from create config to api
1 parent 8738bd1 commit d0c07dd

File tree

7 files changed

+162
-85
lines changed

7 files changed

+162
-85
lines changed

lib/utils/createConfig.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ function createConfig(config, argv, { port }) {
7272
}
7373
}
7474

75-
if (!options.filename && firstWpOpt.output && firstWpOpt.output.filename) {
76-
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
77-
}
78-
7975
if (!options.watchOptions && firstWpOpt.watchOptions) {
8076
options.watchOptions = firstWpOpt.watchOptions;
8177
}

lib/utils/setFilename.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
function setFilename(compiler, options) {
4+
const firstWpOpt = compiler.compilers
5+
? compiler.compilers[0].options
6+
: compiler.options;
7+
8+
if (!options.filename && firstWpOpt.output && firstWpOpt.output.filename) {
9+
options.filename = firstWpOpt.output.filename;
10+
}
11+
}
12+
13+
module.exports = setFilename;

lib/utils/updateCompiler.js

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

1112
function updateCompiler(compiler, options) {
13+
setFilename(compiler, options);
14+
1215
if (options.inline !== false) {
1316
const findHMRPlugin = (config) => {
1417
if (!config.plugins) {

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -283,51 +283,6 @@ Object {
283283
}
284284
`;
285285

286-
exports[`createConfig filename option (in devServer config) 1`] = `
287-
Object {
288-
"filename": "[name]-dev-server-bundle.js",
289-
"hot": true,
290-
"hotOnly": false,
291-
"noInfo": true,
292-
"port": 8080,
293-
"publicPath": "/",
294-
"stats": Object {
295-
"cached": false,
296-
"cachedAssets": false,
297-
},
298-
}
299-
`;
300-
301-
exports[`createConfig filename option (in output config) 1`] = `
302-
Object {
303-
"filename": "[name]-output-bundle.js",
304-
"hot": true,
305-
"hotOnly": false,
306-
"noInfo": true,
307-
"port": 8080,
308-
"publicPath": "/",
309-
"stats": Object {
310-
"cached": false,
311-
"cachedAssets": false,
312-
},
313-
}
314-
`;
315-
316-
exports[`createConfig filename option (in webpack config) 1`] = `
317-
Object {
318-
"filename": "[name]-bundle.js",
319-
"hot": true,
320-
"hotOnly": false,
321-
"noInfo": true,
322-
"port": 8080,
323-
"publicPath": "/",
324-
"stats": Object {
325-
"cached": false,
326-
"cachedAssets": false,
327-
},
328-
}
329-
`;
330-
331286
exports[`createConfig historyApiFallback option (in devServer config) 1`] = `
332287
Object {
333288
"historyApiFallback": true,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`setFilename existing devServer.filename, existing compiler filename should set correct options.filename 1`] = `
4+
Object {
5+
"filename": "devserver-bundle.js",
6+
}
7+
`;
8+
9+
exports[`setFilename existing devServer.filename, no compiler filename should set correct options.filename 1`] = `
10+
Object {
11+
"filename": "devserver-bundle.js",
12+
}
13+
`;
14+
15+
exports[`setFilename multi compiler, existing devServer.filename, existing compiler filename should set correct options.filename 1`] = `
16+
Object {
17+
"filename": "devserver-bundle.js",
18+
}
19+
`;
20+
21+
exports[`setFilename multi compiler, existing devServer.filename, no compiler filename should set correct options.filename 1`] = `
22+
Object {
23+
"filename": "devserver-bundle.js",
24+
}
25+
`;
26+
27+
exports[`setFilename multi compiler, no devServer.filename, existing compiler filename should set correct options.filename 1`] = `
28+
Object {
29+
"filename": "mybundle.js",
30+
}
31+
`;
32+
33+
exports[`setFilename multi compiler, no devServer.filename, no compiler filename should set correct options.filename 1`] = `
34+
Object {
35+
"filename": "[name].js",
36+
}
37+
`;
38+
39+
exports[`setFilename no devServer.filename, existing compiler filename should set correct options.filename 1`] = `
40+
Object {
41+
"filename": "mybundle.js",
42+
}
43+
`;
44+
45+
exports[`setFilename no devServer.filename, no compiler filename should set correct options.filename 1`] = `
46+
Object {
47+
"filename": "[name].js",
48+
}
49+
`;

test/server/utils/createConfig.test.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -276,42 +276,6 @@ describe('createConfig', () => {
276276
expect(config).toMatchSnapshot();
277277
});
278278

279-
it('filename option (in webpack config)', () => {
280-
const config = createConfig(
281-
Object.assign({}, webpackConfig, {
282-
output: { filename: '[name]-bundle.js' },
283-
}),
284-
argv,
285-
{ port: 8080 }
286-
);
287-
288-
expect(config).toMatchSnapshot();
289-
});
290-
291-
it('filename option (in output config)', () => {
292-
const config = createConfig(
293-
Object.assign({}, webpackConfig, {
294-
output: { filename: '[name]-output-bundle.js' },
295-
}),
296-
argv,
297-
{ port: 8080 }
298-
);
299-
300-
expect(config).toMatchSnapshot();
301-
});
302-
303-
it('filename option (in devServer config)', () => {
304-
const config = createConfig(
305-
Object.assign({}, webpackConfig, {
306-
devServer: { filename: '[name]-dev-server-bundle.js' },
307-
}),
308-
argv,
309-
{ port: 8080 }
310-
);
311-
312-
expect(config).toMatchSnapshot();
313-
});
314-
315279
it('watchOptions option (in output config)', () => {
316280
const config = createConfig(
317281
Object.assign({}, webpackConfig, {

test/server/utils/setFilename.test.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict';
2+
3+
const webpack = require('webpack');
4+
const setFilename = require('../../../lib/utils/setFilename');
5+
6+
describe('setFilename', () => {
7+
const cases = [
8+
{
9+
title: 'no devServer.filename, no compiler filename',
10+
webpackFilename: null,
11+
multiCompiler: false,
12+
options: {},
13+
},
14+
{
15+
title: 'no devServer.filename, existing compiler filename',
16+
webpackFilename: 'mybundle.js',
17+
multiCompiler: false,
18+
options: {},
19+
},
20+
{
21+
title: 'existing devServer.filename, no compiler filename',
22+
webpackFilename: null,
23+
multiCompiler: false,
24+
options: {
25+
filename: 'devserver-bundle.js',
26+
},
27+
},
28+
{
29+
title: 'existing devServer.filename, existing compiler filename',
30+
webpackFilename: 'mybundle.js',
31+
multiCompiler: false,
32+
options: {
33+
filename: 'devserver-bundle.js',
34+
},
35+
},
36+
{
37+
title: 'multi compiler, no devServer.filename, no compiler filename',
38+
webpackFilename: null,
39+
multiCompiler: true,
40+
options: {},
41+
},
42+
{
43+
title:
44+
'multi compiler, no devServer.filename, existing compiler filename',
45+
webpackFilename: 'mybundle.js',
46+
multiCompiler: true,
47+
options: {},
48+
},
49+
{
50+
title:
51+
'multi compiler, existing devServer.filename, no compiler filename',
52+
webpackFilename: null,
53+
multiCompiler: true,
54+
options: {
55+
filename: 'devserver-bundle.js',
56+
},
57+
},
58+
{
59+
title:
60+
'multi compiler, existing devServer.filename, existing compiler filename',
61+
webpackFilename: 'mybundle.js',
62+
multiCompiler: true,
63+
options: {
64+
filename: 'devserver-bundle.js',
65+
},
66+
},
67+
];
68+
69+
cases.forEach((data) => {
70+
describe(data.title, () => {
71+
let compiler;
72+
beforeAll(() => {
73+
let webpackConfig;
74+
if (data.multiCompiler) {
75+
// eslint-disable-next-line global-require
76+
webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config');
77+
if (data.webpackFilename) {
78+
webpackConfig[0].output.filename = data.webpackFilename;
79+
}
80+
} else {
81+
// eslint-disable-next-line global-require
82+
webpackConfig = require('../../fixtures/simple-config/webpack.config');
83+
if (data.webpackFilename) {
84+
webpackConfig.output.filename = data.webpackFilename;
85+
}
86+
}
87+
88+
compiler = webpack(webpackConfig);
89+
});
90+
91+
it('should set correct options.filename', () => {
92+
setFilename(compiler, data.options);
93+
expect(data.options).toMatchSnapshot();
94+
});
95+
});
96+
});
97+
});

0 commit comments

Comments
 (0)