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): move filename option from createConfig to API #2113

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ function createConfig(config, argv, { port }) {
}
}

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

if (!options.watchOptions && firstWpOpt.watchOptions) {
options.watchOptions = firstWpOpt.watchOptions;
}
Expand Down
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;
45 changes: 0 additions & 45 deletions test/server/utils/__snapshots__/createConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -283,51 +283,6 @@ Object {
}
`;

exports[`createConfig filename option (in devServer config) 1`] = `
Object {
"filename": "[name]-dev-server-bundle.js",
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig filename option (in output config) 1`] = `
Object {
"filename": "[name]-output-bundle.js",
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig filename option (in webpack config) 1`] = `
Object {
"filename": "[name]-bundle.js",
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig historyApiFallback option (in devServer config) 1`] = `
Object {
"historyApiFallback": true,
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
36 changes: 0 additions & 36 deletions test/server/utils/createConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,42 +276,6 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('filename option (in webpack config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
output: { filename: '[name]-bundle.js' },
}),
argv,
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('filename option (in output config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
output: { filename: '[name]-output-bundle.js' },
}),
argv,
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('filename option (in devServer config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { filename: '[name]-dev-server-bundle.js' },
}),
argv,
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('watchOptions option (in output config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
Expand Down
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