From 2d6e5c6f4ed967368a81742bf347e39f24ee16c8 Mon Sep 17 00:00:00 2001 From: Alexander Akait Date: Wed, 11 Nov 2020 17:24:16 +0300 Subject: [PATCH] fix: respect `stats` from the config for webpack@4 (#2098) --- packages/webpack-cli/lib/webpack-cli.js | 13 ++++++++- test/stats/watch/multi-webpack.config.js | 10 +++++++ test/stats/watch/src/index.js | 1 + test/stats/watch/stats-and-watch.test.js | 35 ++++++++++++++++++++++++ test/stats/watch/webpack.config.js | 4 +++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/stats/watch/multi-webpack.config.js create mode 100644 test/stats/watch/src/index.js create mode 100644 test/stats/watch/stats-and-watch.test.js create mode 100644 test/stats/watch/webpack.config.js diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index f195e18de90..327216f3c08 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -666,11 +666,22 @@ class WebpackCLI { process.exit(2); } } else { - logger.raw(`${stats.toString(foundStats)}`); + const printedStats = stats.toString(foundStats); + + // Avoid extra empty line when `stats: 'none'` + if (printedStats) { + logger.raw(`${stats.toString(foundStats)}`); + } } }; compiler = this.createCompiler(options, callback); + + // TODO webpack@4 return Watching and MultiWathing instead Compiler and MultiCompiler, remove this after drop webpack@4 + if (compiler && compiler.compiler) { + compiler = compiler.compiler; + } + return Promise.resolve(); } } diff --git a/test/stats/watch/multi-webpack.config.js b/test/stats/watch/multi-webpack.config.js new file mode 100644 index 00000000000..7d116eda8b8 --- /dev/null +++ b/test/stats/watch/multi-webpack.config.js @@ -0,0 +1,10 @@ +module.exports = [ + { + watch: true, + stats: 'none', + }, + { + watch: true, + stats: 'none', + }, +]; diff --git a/test/stats/watch/src/index.js b/test/stats/watch/src/index.js new file mode 100644 index 00000000000..8340384261d --- /dev/null +++ b/test/stats/watch/src/index.js @@ -0,0 +1 @@ +console.log('TEST'); \ No newline at end of file diff --git a/test/stats/watch/stats-and-watch.test.js b/test/stats/watch/stats-and-watch.test.js new file mode 100644 index 00000000000..907ea00148b --- /dev/null +++ b/test/stats/watch/stats-and-watch.test.js @@ -0,0 +1,35 @@ +'use strict'; + +const { runWatch, isWebpack5 } = require('../../utils/test-utils'); + +describe('stats and watch', () => { + it('should not log stats with the "none" value from the configuration', async () => { + const { stderr, stdout } = await runWatch(__dirname, ['-c', './webpack.config.js', '--color']); + + expect(stdout).toContain('[webpack-cli] Compilation starting...'); + expect(stdout).toContain('[webpack-cli] Compilation finished'); + expect(stdout).toContain('[webpack-cli] watching files for updates...'); + expect(stderr).toBeFalsy(); + }); + + it('should not log stats with the "none" value from the configuration and multi compiler mode', async () => { + const { stderr, stdout } = await runWatch(__dirname, ['-c', './multi-webpack.config.js', '--color']); + + expect(stdout).toContain('[webpack-cli] Compilation starting...'); + expect(stdout).toContain('[webpack-cli] Compilation finished'); + expect(stdout).toContain('[webpack-cli] watching files for updates...'); + expect(stderr).toBeFalsy(); + }); + + it('should log stats with the "normal" value in arguments', async () => { + const { stderr, stdout } = await runWatch(__dirname, ['-c', './webpack.config.js', '--stats', 'normal', '--color']); + + const output = isWebpack5 ? 'successfully' : 'main.js'; + + expect(stdout).toContain('[webpack-cli] Compilation starting...'); + expect(stdout).toContain('[webpack-cli] Compilation finished'); + expect(stdout).toContain('[webpack-cli] watching files for updates...'); + expect(stdout).toContain(output); + expect(stderr).toBeFalsy(); + }); +}); diff --git a/test/stats/watch/webpack.config.js b/test/stats/watch/webpack.config.js new file mode 100644 index 00000000000..e7e1987f001 --- /dev/null +++ b/test/stats/watch/webpack.config.js @@ -0,0 +1,4 @@ +module.exports = { + watch: true, + stats: 'none', +};