diff --git a/packages/webpack-cli/lib/utils/zero-config.js b/packages/webpack-cli/lib/utils/zero-config.js index c7655b0aea1..696a8357ffc 100644 --- a/packages/webpack-cli/lib/utils/zero-config.js +++ b/packages/webpack-cli/lib/utils/zero-config.js @@ -18,7 +18,7 @@ function getConfigurations(options, outputOptions) { const { mode } = options; const defaultConfigType = getEnvFromOptionsAndMode(mode, outputOptions); const defaultConfig = require(`./${defaultConfigType}-config`)(options, outputOptions); - const newConfig = merge(defaultConfig, options); + const newConfig = merge(options, defaultConfig); newConfig.mode = defaultConfigType; const isEntryObject = newConfig.entry && newConfig.entry instanceof Object; diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 9c6fa362b14..5de6462ddec 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -246,7 +246,7 @@ class WebpackCLI extends GroupHelper { * @private\ * @returns {void} */ - _handForcedDefaults() { + _handleForcedDefaults() { if (this.outputConfiguration.defaults) { const wrappedConfig = require('./utils/zero-config')(this.compilerConfiguration, this.outputConfiguration); this.compilerConfiguration = this.checkDefaults(wrappedConfig.options, this.outputConfiguration); @@ -270,7 +270,7 @@ class WebpackCLI extends GroupHelper { .then(() => this._handleGroupHelper(this.advancedGroup)) .then(() => this._handleGroupHelper(this.statsGroup)) .then(() => this._handleGroupHelper(this.helpGroup)) - .then(() => this._handForcedDefaults()); + .then(() => this._handleForcedDefaults()); } async processArgs(args, cliOptions) { diff --git a/test/defaults/with-config/default-with-config.test.js b/test/defaults/with-config/default-with-config.test.js new file mode 100644 index 00000000000..0884d36101a --- /dev/null +++ b/test/defaults/with-config/default-with-config.test.js @@ -0,0 +1,31 @@ +'use strict'; +const { stat } = require('fs'); +const { resolve } = require('path'); +const { run } = require('../../utils/test-utils'); + +describe('output flag defaults with config', () => { + it('should use default entry if config entry file is not present', done => { + const { stdout, stderr } = run(__dirname, ['--defaults'], false); + // Should use the output dir specified in the config + expect(stdout).toContain('./index.js'); + // Should not throw because of unknown entry in config since it will pickup the default entry + expect(stderr).toBeFalsy(); + stat(resolve(__dirname, './dist/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); + it('should aggressively apply defaults when config flag is used', done => { + const { stdout, stderr } = run(__dirname, ['--defaults', '--config', './webpack.config.js'], false); + // Should use the output dir specified in the config + expect(stdout).toContain('./index.js'); + // Should not throw because of unknown entry in config since it will pickup the default entry + expect(stderr).toBeFalsy(); + stat(resolve(__dirname, './dist/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +}); diff --git a/test/defaults/with-config-and-entry/default-with-config.test.js b/test/defaults/with-config/default-with-merge.test.js similarity index 62% rename from test/defaults/with-config-and-entry/default-with-config.test.js rename to test/defaults/with-config/default-with-merge.test.js index bdd79d733f8..0f38ec88fba 100644 --- a/test/defaults/with-config-and-entry/default-with-config.test.js +++ b/test/defaults/with-config/default-with-merge.test.js @@ -3,14 +3,14 @@ const { stat } = require('fs'); const { resolve } = require('path'); const { run } = require('../../utils/test-utils'); -describe('output flag defaults with config', () => { - it.skip('should use default entry if config entry file is not present', done => { - const { stdout, stderr } = run(__dirname, ['--defaults'], false); +describe('defaults flag used with merge flag', () => { + it('should use default config when merge flag is supplied', done => { + const { stdout, stderr } = run(__dirname, ['--defaults', '-m', './webpack.config.js'], false); // Should use the output dir specified in the config expect(stdout).toContain('./index.js'); // Should not throw because of unknown entry in config since it will pickup the default entry expect(stderr).toBeFalsy(); - stat(resolve(__dirname, './binary/a.bundle.js'), (err, stats) => { + stat(resolve(__dirname, './dist/main.js'), (err, stats) => { expect(err).toBe(null); expect(stats.isFile()).toBe(true); done(); diff --git a/test/defaults/with-config-and-entry/index.js b/test/defaults/with-config/index.js similarity index 100% rename from test/defaults/with-config-and-entry/index.js rename to test/defaults/with-config/index.js diff --git a/test/defaults/with-config-and-entry/webpack.config.js b/test/defaults/with-config/webpack.config.js similarity index 100% rename from test/defaults/with-config-and-entry/webpack.config.js rename to test/defaults/with-config/webpack.config.js diff --git a/test/defaults/without-config-and-entry/default-without-config.test.js b/test/defaults/without-config/default-without-config.test.js similarity index 87% rename from test/defaults/without-config-and-entry/default-without-config.test.js rename to test/defaults/without-config/default-without-config.test.js index 7e6a1b2b171..62bd6a1a7b1 100644 --- a/test/defaults/without-config-and-entry/default-without-config.test.js +++ b/test/defaults/without-config/default-without-config.test.js @@ -4,7 +4,7 @@ const { resolve } = require('path'); const { run } = require('../../utils/test-utils'); describe('output flag defaults without config', () => { - it('should throw if the entry file is not present', done => { + it('should throw if the default entry file is not present', done => { const { stdout } = run(__dirname, ['--defaults'], false); expect(stdout).toContain("Error: Can't resolve './index.js' in");