From 281aad3ee4961f1643453eb1a926e88e0b7f019c Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sun, 28 Mar 2021 12:01:39 +0530 Subject: [PATCH] feat: add flag to force start finish log (#2566) --- packages/webpack-cli/lib/plugins/CLIPlugin.js | 15 ++++-- .../build/start-finish-force-log/src/index.js | 1 + .../start-finish-force-log.test.js | 50 +++++++++++++++++++ .../webpack.config.array.js | 10 ++++ .../start-finish-force-log/webpack.config.js | 3 ++ 5 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 test/build/start-finish-force-log/src/index.js create mode 100644 test/build/start-finish-force-log/start-finish-force-log.test.js create mode 100644 test/build/start-finish-force-log/webpack.config.array.js create mode 100644 test/build/start-finish-force-log/webpack.config.js diff --git a/packages/webpack-cli/lib/plugins/CLIPlugin.js b/packages/webpack-cli/lib/plugins/CLIPlugin.js index 363221e3fd6..f4c267441d5 100644 --- a/packages/webpack-cli/lib/plugins/CLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/CLIPlugin.js @@ -40,14 +40,20 @@ class CLIPlugin { setupHelpfulOutput(compiler) { const pluginName = 'webpack-cli'; const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : ''); + const logCompilation = (message) => { + if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG) { + process.stderr.write(message); + } else { + this.logger.log(message); + } + }; const { configPath } = this.options; compiler.hooks.run.tap(pluginName, () => { const name = getCompilationName(); - this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); - + logCompilation(`Compiler${name ? ` ${name}` : ''} starting... `); if (configPath) { this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`); } @@ -62,7 +68,7 @@ class CLIPlugin { const name = getCompilationName(); - this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); + logCompilation(`Compiler${name ? ` ${name}` : ''} starting... `); if (configPath) { this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`); @@ -79,8 +85,7 @@ class CLIPlugin { (compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => { const name = getCompilationName(); - this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`); - + logCompilation(`Compiler${name ? ` ${name}` : ''} finished`); process.nextTick(() => { if (compiler.watchMode) { this.logger.log(`Compiler${name ? `${name}` : ''} is watching files for updates...`); diff --git a/test/build/start-finish-force-log/src/index.js b/test/build/start-finish-force-log/src/index.js new file mode 100644 index 00000000000..ca8f58ee959 --- /dev/null +++ b/test/build/start-finish-force-log/src/index.js @@ -0,0 +1 @@ +console.log("Itadori Yuuji") diff --git a/test/build/start-finish-force-log/start-finish-force-log.test.js b/test/build/start-finish-force-log/start-finish-force-log.test.js new file mode 100644 index 00000000000..3701bb95d08 --- /dev/null +++ b/test/build/start-finish-force-log/start-finish-force-log.test.js @@ -0,0 +1,50 @@ +'use strict'; + +const { run, runWatch, isWebpack5 } = require('../../utils/test-utils'); + +describe('start finish force log', () => { + it('start finish force log when env is set', () => { + const { exitCode, stderr, stdout } = run(__dirname, [], { + env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true }, + }); + expect(exitCode).toBe(0); + expect(stderr).toContain('Compiler starting...'); + expect(stderr).toContain('Compiler finished'); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); + }); + + it('should show name of the config', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--name', 'log config'], { + env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true }, + }); + expect(exitCode).toBe(0); + expect(stderr).toContain("Compiler 'log config' starting..."); + expect(stderr).toContain("Compiler 'log config' finished"); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); + }); + + it('should work with watch', async () => { + const { stderr, stdout } = await runWatch(__dirname, ['watch'], { + env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true }, + }); + expect(stderr).toContain('Compiler starting...'); + expect(stderr).toContain('Compiler finished'); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); + }); + + it('should work with multi compiler', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--config', './webpack.config.array.js'], { + env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true }, + }); + expect(exitCode).toBe(0); + expect(stderr).toContain("Compiler 'Gojou' starting..."); + expect(stderr).toContain("Compiler 'Satoru' starting..."); + expect(stderr).toContain("Compiler 'Gojou' finished"); + expect(stderr).toContain("Compiler 'Satoru' finished"); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); + }); +}); diff --git a/test/build/start-finish-force-log/webpack.config.array.js b/test/build/start-finish-force-log/webpack.config.array.js new file mode 100644 index 00000000000..14738c20f1b --- /dev/null +++ b/test/build/start-finish-force-log/webpack.config.array.js @@ -0,0 +1,10 @@ +module.exports = [ + { + name: 'Gojou', + mode: 'development', + }, + { + name: 'Satoru', + mode: 'development', + }, +]; diff --git a/test/build/start-finish-force-log/webpack.config.js b/test/build/start-finish-force-log/webpack.config.js new file mode 100644 index 00000000000..f2c3976d5d3 --- /dev/null +++ b/test/build/start-finish-force-log/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = { + mode: 'development', +};