From 53bb5a75f61c88a1ceb8a673bd86ce54ac852ba2 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 17 Sep 2020 14:13:13 +0530 Subject: [PATCH 1/4] fix: show warning if bail and watch are used together --- packages/webpack-cli/lib/groups/ConfigGroup.js | 6 ++++++ packages/webpack-cli/lib/utils/Compiler.js | 5 +++++ test/bail/bail-and-watch-warning.test.js | 12 ++++++++++++ test/bail/main.js | 1 + test/bail/webpack.config.js | 6 ++++++ test/core-flags/bail-flag.test.js | 8 ++++++++ 6 files changed, 38 insertions(+) create mode 100644 test/bail/bail-and-watch-warning.test.js create mode 100644 test/bail/main.js create mode 100644 test/bail/webpack.config.js diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index ed22885fce9..64cedceb59e 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -127,6 +127,12 @@ class ConfigGroup extends GroupHelper { newOptionsObject['options'] = configOptions; } + //warn the user if bail and watch both are used together + const { bail, watch } = newOptionsObject['options']; + if (bail && watch) { + logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); + } + if (configOptions && configPath.includes('.webpack')) { const currentPath = configPath; const parentContext = dirname(currentPath).split(sep).slice(0, -1).join(sep); diff --git a/packages/webpack-cli/lib/utils/Compiler.js b/packages/webpack-cli/lib/utils/Compiler.js index 20774ddbe9e..4a2c4686e96 100644 --- a/packages/webpack-cli/lib/utils/Compiler.js +++ b/packages/webpack-cli/lib/utils/Compiler.js @@ -117,6 +117,11 @@ class Compiler { return interactive(options, outputOptions); } + //warn the user if bail and watch both are used together + if (this.compiler.options.bail && outputOptions.watch) { + logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); + } + if (this.compiler.compilers) { this.compiler.compilers.forEach((comp, idx) => { this.setUpHookForCompilation(comp, outputOptions, options[idx]); diff --git a/test/bail/bail-and-watch-warning.test.js b/test/bail/bail-and-watch-warning.test.js new file mode 100644 index 00000000000..9700436421d --- /dev/null +++ b/test/bail/bail-and-watch-warning.test.js @@ -0,0 +1,12 @@ +'use strict'; + +const { run } = require('../utils/test-utils'); + +describe('bail and watch warning', () => { + it('should log warning if bail and watch both options are true', () => { + const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js']); + + expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); + expect(stdout).toBeTruthy(); + }); +}); diff --git a/test/bail/main.js b/test/bail/main.js new file mode 100644 index 00000000000..15e0a061dc2 --- /dev/null +++ b/test/bail/main.js @@ -0,0 +1 @@ +console.log('bail and watch warning test'); diff --git a/test/bail/webpack.config.js b/test/bail/webpack.config.js new file mode 100644 index 00000000000..e066755baef --- /dev/null +++ b/test/bail/webpack.config.js @@ -0,0 +1,6 @@ +module.exports = { + entry: './main.js', + mode: 'development', + bail: true, + watch: true, +}; diff --git a/test/core-flags/bail-flag.test.js b/test/core-flags/bail-flag.test.js index 48587933155..d938bdf2609 100644 --- a/test/core-flags/bail-flag.test.js +++ b/test/core-flags/bail-flag.test.js @@ -16,4 +16,12 @@ describe('--bail flag', () => { expect(stderr).toBeFalsy(); expect(stdout).toContain('bail: false'); }); + + it('should log warning if --bail and --watch are used together', () => { + const { stderr, stdout } = run(__dirname, ['--bail', '--watch']); + + expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); + expect(stdout).toContain('bail: true'); + expect(stdout).toContain('watch: true'); + }); }); From 20b246cc280ad45c0a7b3aa982e63f4261ce86ba Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 17 Sep 2020 19:58:18 +0530 Subject: [PATCH 2/4] fix: watch fix: a --- packages/webpack-cli/lib/groups/BasicGroup.js | 3 +++ .../webpack-cli/lib/groups/ConfigGroup.js | 6 ------ packages/webpack-cli/lib/utils/Compiler.js | 8 +++----- .../lib/utils/warnings/bailAndWatchWarning.js | 7 +++++++ test/bail/bail-and-watch-warning.test.js | 11 ++++++++-- test/bail/main.js | 1 - test/bail/multi-webpack.config.js | 20 +++++++++++++++++++ ...ack.config.js => single-webpack.config.js} | 2 +- test/bail/src/first.js | 1 + test/bail/src/second.js | 1 + test/core-flags/bail-flag.test.js | 8 -------- 11 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js delete mode 100644 test/bail/main.js create mode 100644 test/bail/multi-webpack.config.js rename test/bail/{webpack.config.js => single-webpack.config.js} (73%) create mode 100644 test/bail/src/first.js create mode 100644 test/bail/src/second.js diff --git a/packages/webpack-cli/lib/groups/BasicGroup.js b/packages/webpack-cli/lib/groups/BasicGroup.js index de43289985e..cbf8a41806d 100644 --- a/packages/webpack-cli/lib/groups/BasicGroup.js +++ b/packages/webpack-cli/lib/groups/BasicGroup.js @@ -36,6 +36,9 @@ class BasicGroup extends GroupHelper { if (arg === 'name') { options.name = args[arg]; } + if (arg === 'watch') { + options.watch = true; + } if (arg === 'entry') { options[arg] = this.resolveFilePath(args[arg], 'index.js'); if (options[arg].length === 0) { diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index 64cedceb59e..ed22885fce9 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -127,12 +127,6 @@ class ConfigGroup extends GroupHelper { newOptionsObject['options'] = configOptions; } - //warn the user if bail and watch both are used together - const { bail, watch } = newOptionsObject['options']; - if (bail && watch) { - logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); - } - if (configOptions && configPath.includes('.webpack')) { const currentPath = configPath; const parentContext = dirname(currentPath).split(sep).slice(0, -1).join(sep); diff --git a/packages/webpack-cli/lib/utils/Compiler.js b/packages/webpack-cli/lib/utils/Compiler.js index 4a2c4686e96..7b34f078b28 100644 --- a/packages/webpack-cli/lib/utils/Compiler.js +++ b/packages/webpack-cli/lib/utils/Compiler.js @@ -1,5 +1,6 @@ const webpack = require('webpack'); const logger = require('./logger'); +const bailAndWatchWarning = require('./warnings/bailAndWatchWarning'); const { CompilerOutput } = require('./CompilerOutput'); class Compiler { @@ -117,16 +118,13 @@ class Compiler { return interactive(options, outputOptions); } - //warn the user if bail and watch both are used together - if (this.compiler.options.bail && outputOptions.watch) { - logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); - } - if (this.compiler.compilers) { this.compiler.compilers.forEach((comp, idx) => { + bailAndWatchWarning(comp); //warn the user if bail and watch both are used together this.setUpHookForCompilation(comp, outputOptions, options[idx]); }); } else { + bailAndWatchWarning(this.compiler); this.setUpHookForCompilation(this.compiler, outputOptions, options); } diff --git a/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js b/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js new file mode 100644 index 00000000000..0acbe8cfd05 --- /dev/null +++ b/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js @@ -0,0 +1,7 @@ +const logger = require('../logger'); + +module.exports = (compiler) => { + if (compiler.options.bail && compiler.options.watch) { + logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); + } +}; diff --git a/test/bail/bail-and-watch-warning.test.js b/test/bail/bail-and-watch-warning.test.js index 9700436421d..007f7ed2cdd 100644 --- a/test/bail/bail-and-watch-warning.test.js +++ b/test/bail/bail-and-watch-warning.test.js @@ -3,8 +3,15 @@ const { run } = require('../utils/test-utils'); describe('bail and watch warning', () => { - it('should log warning if bail and watch both options are true', () => { - const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js']); + it('should log warning in case of single compiler', () => { + const { stderr, stdout } = run(__dirname, ['-c', 'single-webpack.config.js']); + + expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); + expect(stdout).toBeTruthy(); + }); + + it('should log warning in case of multiple compilers', () => { + const { stderr, stdout } = run(__dirname, ['-c', 'multi-webpack.config.js']); expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); expect(stdout).toBeTruthy(); diff --git a/test/bail/main.js b/test/bail/main.js deleted file mode 100644 index 15e0a061dc2..00000000000 --- a/test/bail/main.js +++ /dev/null @@ -1 +0,0 @@ -console.log('bail and watch warning test'); diff --git a/test/bail/multi-webpack.config.js b/test/bail/multi-webpack.config.js new file mode 100644 index 00000000000..497c9ff6162 --- /dev/null +++ b/test/bail/multi-webpack.config.js @@ -0,0 +1,20 @@ +module.exports = [ + { + output: { + filename: './dist-first.js', + }, + name: 'first', + entry: './src/first.js', + mode: 'development', + }, + { + output: { + filename: './dist-second.js', + }, + name: 'second', + entry: './src/second.js', + mode: 'production', + bail: true, + watch: true, + }, +]; diff --git a/test/bail/webpack.config.js b/test/bail/single-webpack.config.js similarity index 73% rename from test/bail/webpack.config.js rename to test/bail/single-webpack.config.js index e066755baef..132930c4f13 100644 --- a/test/bail/webpack.config.js +++ b/test/bail/single-webpack.config.js @@ -1,5 +1,5 @@ module.exports = { - entry: './main.js', + entry: './src/first.js', mode: 'development', bail: true, watch: true, diff --git a/test/bail/src/first.js b/test/bail/src/first.js new file mode 100644 index 00000000000..fb7e56835c4 --- /dev/null +++ b/test/bail/src/first.js @@ -0,0 +1 @@ +console.log('bail and watch warning test first'); diff --git a/test/bail/src/second.js b/test/bail/src/second.js new file mode 100644 index 00000000000..5b277372189 --- /dev/null +++ b/test/bail/src/second.js @@ -0,0 +1 @@ +console.log('bail and watch warning test second'); diff --git a/test/core-flags/bail-flag.test.js b/test/core-flags/bail-flag.test.js index d938bdf2609..48587933155 100644 --- a/test/core-flags/bail-flag.test.js +++ b/test/core-flags/bail-flag.test.js @@ -16,12 +16,4 @@ describe('--bail flag', () => { expect(stderr).toBeFalsy(); expect(stdout).toContain('bail: false'); }); - - it('should log warning if --bail and --watch are used together', () => { - const { stderr, stdout } = run(__dirname, ['--bail', '--watch']); - - expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); - expect(stdout).toContain('bail: true'); - expect(stdout).toContain('watch: true'); - }); }); From c43b93b0c9db7b77c00d8d8335e449cdc885ce1e Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 20 Sep 2020 08:10:26 +0530 Subject: [PATCH 3/4] tests: update --- test/bail/bail-and-watch-warning.test.js | 7 +++++++ test/bail/third-webpack.config.js | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 test/bail/third-webpack.config.js diff --git a/test/bail/bail-and-watch-warning.test.js b/test/bail/bail-and-watch-warning.test.js index 007f7ed2cdd..088d8717967 100644 --- a/test/bail/bail-and-watch-warning.test.js +++ b/test/bail/bail-and-watch-warning.test.js @@ -16,4 +16,11 @@ describe('bail and watch warning', () => { expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); expect(stdout).toBeTruthy(); }); + + it('should log not log warning if both are not true', () => { + const { stderr, stdout } = run(__dirname, ['-c', 'third-webpack.config.js']); + + expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); + expect(stdout).toBeTruthy(); + }); }); diff --git a/test/bail/third-webpack.config.js b/test/bail/third-webpack.config.js new file mode 100644 index 00000000000..b0b7ac2f33c --- /dev/null +++ b/test/bail/third-webpack.config.js @@ -0,0 +1,5 @@ +module.exports = { + entry: './src/first.js', + mode: 'development', + bail: true, +}; From 685df00aba0327fe76444a799aaae208ee7adb0f Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 20 Sep 2020 20:54:50 +0530 Subject: [PATCH 4/4] chore: add comments --- .../lib/utils/warnings/bailAndWatchWarning.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js b/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js index 0acbe8cfd05..84894469cab 100644 --- a/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js +++ b/packages/webpack-cli/lib/utils/warnings/bailAndWatchWarning.js @@ -1,7 +1,14 @@ const logger = require('../logger'); -module.exports = (compiler) => { +/** + * warn the user if bail and watch both are used together + * @param {Object} webpack compiler + * @returns {void} + */ +const bailAndWatchWarning = (compiler) => { if (compiler.options.bail && compiler.options.watch) { logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); } }; + +module.exports = bailAndWatchWarning;