diff --git a/lib/utils/createConfig.js b/lib/utils/createConfig.js index 0211561235..3acae98545 100644 --- a/lib/utils/createConfig.js +++ b/lib/utils/createConfig.js @@ -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; } diff --git a/lib/utils/normalizeOptions.js b/lib/utils/normalizeOptions.js index 443d2df94a..35fac4896f 100644 --- a/lib/utils/normalizeOptions.js +++ b/lib/utils/normalizeOptions.js @@ -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(); @@ -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; diff --git a/test/server/utils/__snapshots__/createConfig.test.js.snap b/test/server/utils/__snapshots__/createConfig.test.js.snap index 5549367189..cea83a193e 100644 --- a/test/server/utils/__snapshots__/createConfig.test.js.snap +++ b/test/server/utils/__snapshots__/createConfig.test.js.snap @@ -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, diff --git a/test/server/utils/__snapshots__/normalizeOptions.test.js.snap b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap index 0658e08b4b..045e664fc0 100644 --- a/test/server/utils/__snapshots__/normalizeOptions.test.js.snap +++ b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap @@ -7,6 +7,7 @@ Object { "/path/to/dist1", "/path/to/dist2", ], + "filename": "[name].js", "serverMode": "sockjs", "watchOptions": Object {}, } @@ -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 {}, } @@ -24,6 +98,7 @@ Object { exports[`normalizeOptions no options should set correct options 1`] = ` Object { "clientMode": "sockjs", + "filename": "[name].js", "serverMode": "sockjs", "watchOptions": Object {}, } @@ -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, diff --git a/test/server/utils/createConfig.test.js b/test/server/utils/createConfig.test.js index 67431598c4..f6cc344093 100644 --- a/test/server/utils/createConfig.test.js +++ b/test/server/utils/createConfig.test.js @@ -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, { diff --git a/test/server/utils/normalizeOptions.test.js b/test/server/utils/normalizeOptions.test.js index c1f66f4939..62facbf013 100644 --- a/test/server/utils/normalizeOptions.test.js +++ b/test/server/utils/normalizeOptions.test.js @@ -37,12 +37,112 @@ 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'); @@ -50,6 +150,13 @@ describe('normalizeOptions', () => { webpackConfig = require('../../fixtures/simple-config/webpack.config'); } + if (data.webpackConfig) { + mergeConfigs( + data.multiCompiler ? webpackConfig[0] : webpackConfig, + data.webpackConfig + ); + } + compiler = webpack(webpackConfig); });