Skip to content

Commit

Permalink
refactor(server): switched to normalize options
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Jul 23, 2019
1 parent 93d6ca3 commit 7e37db5
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 65 deletions.
8 changes: 8 additions & 0 deletions lib/utils/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/

function normalizeOptions(compiler, options) {
const firstWpOpt = compiler.compilers
? compiler.compilers[0].options
: compiler.options;

// Setup default value
options.contentBase =
options.contentBase !== undefined ? options.contentBase : process.cwd();
Expand All @@ -22,6 +26,10 @@ function normalizeOptions(compiler, options) {
if (!options.watchOptions) {
options.watchOptions = {};
}

if (!options.filename && firstWpOpt.output && firstWpOpt.output.filename) {
options.filename = firstWpOpt.output.filename;
}
}

module.exports = normalizeOptions;
13 changes: 0 additions & 13 deletions lib/utils/setFilename.js

This file was deleted.

3 changes: 0 additions & 3 deletions lib/utils/updateCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
const webpack = require('webpack');
const addEntries = require('./addEntries');
const getSocketClientPath = require('./getSocketClientPath');
const setFilename = require('./setFilename');

function updateCompiler(compiler, options) {
setFilename(compiler, options);

if (options.inline !== false) {
const findHMRPlugin = (config) => {
if (!config.plugins) {
Expand Down
76 changes: 76 additions & 0 deletions test/server/utils/__snapshots__/normalizeOptions.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"/path/to/dist1",
"/path/to/dist2",
],
"filename": "[name].js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
Expand All @@ -16,6 +17,79 @@ exports[`normalizeOptions contentBase string should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"contentBase": "/path/to/dist",
"filename": "[name].js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions existing devServer.filename, existing compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "devserver-bundle.js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions existing devServer.filename, no compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "devserver-bundle.js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions multi compiler, existing devServer.filename, existing compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "devserver-bundle.js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions multi compiler, existing devServer.filename, no compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "devserver-bundle.js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions multi compiler, no devServer.filename, existing compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "mybundle.js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions multi compiler, no devServer.filename, no compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "[name].js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions no devServer.filename, existing compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "mybundle.js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
`;

exports[`normalizeOptions no devServer.filename, no compiler filename should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "[name].js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
Expand All @@ -24,6 +98,7 @@ Object {
exports[`normalizeOptions no options should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "[name].js",
"serverMode": "sockjs",
"watchOptions": Object {},
}
Expand All @@ -32,6 +107,7 @@ Object {
exports[`normalizeOptions watchOptions should set correct options 1`] = `
Object {
"clientMode": "sockjs",
"filename": "[name].js",
"serverMode": "sockjs",
"watchOptions": Object {
"poll": true,
Expand Down
49 changes: 0 additions & 49 deletions test/server/utils/__snapshots__/setFilename.test.js.snap

This file was deleted.

107 changes: 107 additions & 0 deletions test/server/utils/normalizeOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,126 @@ describe('normalizeOptions', () => {
},
optionsResults: null,
},
{
title: 'no devServer.filename, no compiler filename',
webpackConfig: null,
multiCompiler: false,
options: {},
optionsResults: null,
},
{
title: 'no devServer.filename, existing compiler filename',
webpackConfig: {
output: {
filename: 'mybundle.js',
},
},
multiCompiler: false,
options: {},
optionsResults: null,
},
{
title: 'existing devServer.filename, no compiler filename',
webpackConfig: null,
multiCompiler: false,
options: {
filename: 'devserver-bundle.js',
},
optionsResults: null,
},
{
title: 'existing devServer.filename, existing compiler filename',
webpackConfig: {
output: {
filename: 'mybundle.js',
},
},
multiCompiler: false,
options: {
filename: 'devserver-bundle.js',
},
optionsResults: null,
},
{
title: 'multi compiler, no devServer.filename, no compiler filename',
webpackConfig: null,
multiCompiler: true,
options: {},
optionsResults: null,
},
{
title:
'multi compiler, no devServer.filename, existing compiler filename',
webpackConfig: {
output: {
filename: 'mybundle.js',
},
},
multiCompiler: true,
options: {},
optionsResults: null,
},
{
title:
'multi compiler, existing devServer.filename, no compiler filename',
webpackConfig: null,
multiCompiler: true,
options: {
filename: 'devserver-bundle.js',
},
optionsResults: null,
},
{
title:
'multi compiler, existing devServer.filename, existing compiler filename',
webpackConfig: {
output: {
filename: 'mybundle.js',
},
},
multiCompiler: true,
options: {
filename: 'devserver-bundle.js',
},
optionsResults: null,
},
];

cases.forEach((data) => {
describe(data.title, () => {
let compiler;
beforeAll(() => {
// this will merge webpack configs through a depth of one layer of objects,
// specifically so that the webpack config output object can be merged
const mergeConfigs = (baseConfig, config) => {
Object.keys(config).forEach((key1) => {
if (typeof config[key1] === 'object') {
Object.keys(config[key1]).forEach((key2) => {
if (!baseConfig[key1]) {
baseConfig[key1] = {};
}
baseConfig[key1][key2] = config[key1][key2];
});
} else {
baseConfig[key1] = config[key1];
}
});
};

let webpackConfig;
if (data.multiCompiler) {
webpackConfig = require('../../fixtures/multi-compiler-config/webpack.config');
} else {
webpackConfig = require('../../fixtures/simple-config/webpack.config');
}

if (data.webpackConfig) {
mergeConfigs(
data.multiCompiler ? webpackConfig[0] : webpackConfig,
data.webpackConfig
);
}

compiler = webpack(webpackConfig);
});

Expand Down

0 comments on commit 7e37db5

Please sign in to comment.