From 42119d89023a37bbcea72b5b7dc089a72eaf5bd5 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 14 Mar 2020 23:02:31 +0530 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20default=20flag=20beha?= =?UTF-8?q?vior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #1322 --- packages/webpack-cli/lib/utils/zero-config.js | 2 +- packages/webpack-cli/lib/webpack-cli.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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..c88fdccdfa1 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -246,11 +246,13 @@ class WebpackCLI extends GroupHelper { * @private\ * @returns {void} */ - _handForcedDefaults() { + _handleForcedDefaults() { + console.log(this.outputConfiguration) if (this.outputConfiguration.defaults) { const wrappedConfig = require('./utils/zero-config')(this.compilerConfiguration, this.outputConfiguration); this.compilerConfiguration = this.checkDefaults(wrappedConfig.options, this.outputConfiguration); } + console.log(this.compilerConfiguration) } /** @@ -270,7 +272,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) { From e34e67cc239c72bc519443cdfc8b07cca4b5ef53 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 14 Mar 2020 23:56:19 +0530 Subject: [PATCH 2/5] =?UTF-8?q?chore:=20=F0=9F=A4=96=20rm=20logs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-cli/lib/webpack-cli.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index c88fdccdfa1..5de6462ddec 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -247,12 +247,10 @@ class WebpackCLI extends GroupHelper { * @returns {void} */ _handleForcedDefaults() { - console.log(this.outputConfiguration) if (this.outputConfiguration.defaults) { const wrappedConfig = require('./utils/zero-config')(this.compilerConfiguration, this.outputConfiguration); this.compilerConfiguration = this.checkDefaults(wrappedConfig.options, this.outputConfiguration); } - console.log(this.compilerConfiguration) } /** From 5a0e515da01d35191d7e2542da11661430eba472 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sun, 15 Mar 2020 00:16:06 +0530 Subject: [PATCH 3/5] =?UTF-8?q?chore:=20=F0=9F=A4=96=20enable=20default=20?= =?UTF-8?q?flags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../with-config-and-entry/default-with-config.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/defaults/with-config-and-entry/default-with-config.test.js b/test/defaults/with-config-and-entry/default-with-config.test.js index bdd79d733f8..fffb543b9ab 100644 --- a/test/defaults/with-config-and-entry/default-with-config.test.js +++ b/test/defaults/with-config-and-entry/default-with-config.test.js @@ -4,13 +4,13 @@ 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 => { + 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, './binary/a.bundle.js'), (err, stats) => { + stat(resolve(__dirname, './dist/main.js'), (err, stats) => { expect(err).toBe(null); expect(stats.isFile()).toBe(true); done(); From 93df6c979dda617f98055b5abc2611f6d2b13a1b Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sun, 15 Mar 2020 11:44:20 +0530 Subject: [PATCH 4/5] fix: folder structure of default tests --- .../default-with-config.test.js | 12 ++++++++++++ .../{with-config-and-entry => with-config}/index.js | 0 .../webpack.config.js | 0 .../default-without-config.test.js | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-) rename test/defaults/{with-config-and-entry => with-config}/default-with-config.test.js (55%) rename test/defaults/{with-config-and-entry => with-config}/index.js (100%) rename test/defaults/{with-config-and-entry => with-config}/webpack.config.js (100%) rename test/defaults/{without-config-and-entry => without-config}/default-without-config.test.js (87%) diff --git a/test/defaults/with-config-and-entry/default-with-config.test.js b/test/defaults/with-config/default-with-config.test.js similarity index 55% rename from test/defaults/with-config-and-entry/default-with-config.test.js rename to test/defaults/with-config/default-with-config.test.js index fffb543b9ab..0884d36101a 100644 --- a/test/defaults/with-config-and-entry/default-with-config.test.js +++ b/test/defaults/with-config/default-with-config.test.js @@ -16,4 +16,16 @@ describe('output flag defaults with config', () => { 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/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"); From 9403ceaf79c97d276c1cc3c885c1622308b9f097 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sun, 15 Mar 2020 11:44:42 +0530 Subject: [PATCH 5/5] test: add test with merge flag --- .../with-config/default-with-merge.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/defaults/with-config/default-with-merge.test.js diff --git a/test/defaults/with-config/default-with-merge.test.js b/test/defaults/with-config/default-with-merge.test.js new file mode 100644 index 00000000000..0f38ec88fba --- /dev/null +++ b/test/defaults/with-config/default-with-merge.test.js @@ -0,0 +1,19 @@ +'use strict'; +const { stat } = require('fs'); +const { resolve } = require('path'); +const { run } = require('../../utils/test-utils'); + +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, './dist/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +});