From dbceb801763124045cca13df0a67d28e6dac33f2 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 27 Mar 2021 20:09:28 +0530 Subject: [PATCH 1/4] feat: add flag to force start finish log --- packages/webpack-cli/lib/plugins/CLIPlugin.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/webpack-cli/lib/plugins/CLIPlugin.js b/packages/webpack-cli/lib/plugins/CLIPlugin.js index 363221e3fd6..49830c9754f 100644 --- a/packages/webpack-cli/lib/plugins/CLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/CLIPlugin.js @@ -45,8 +45,11 @@ class CLIPlugin { compiler.hooks.run.tap(pluginName, () => { const name = getCompilationName(); - - this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); + if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG_FORCE) { + process.stderr.write(`Compiler${name ? ` ${name}` : ''} starting...`); + } else { + this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); + } if (configPath) { this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`); @@ -78,8 +81,11 @@ class CLIPlugin { (compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => { const name = getCompilationName(); - - this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`); + if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG_FORCE) { + process.stderr.write(`Compiler${name ? ` ${name}` : ''} finished`); + } else { + this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`); + } process.nextTick(() => { if (compiler.watchMode) { From 991a43e4531b5532eb38c41c798576b505ae0187 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 27 Mar 2021 21:07:37 +0530 Subject: [PATCH 2/4] test: add --- packages/webpack-cli/lib/plugins/CLIPlugin.js | 21 +++++------ .../build/start-finish-force-log/src/index.js | 1 + .../start-finish-force-log.test.js | 37 +++++++++++++++++++ .../webpack.config.array.js | 10 +++++ .../start-finish-force-log/webpack.config.js | 3 ++ 5 files changed, 61 insertions(+), 11 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 49830c9754f..f4c267441d5 100644 --- a/packages/webpack-cli/lib/plugins/CLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/CLIPlugin.js @@ -40,17 +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(); - if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG_FORCE) { - process.stderr.write(`Compiler${name ? ` ${name}` : ''} starting...`); - } else { - this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`); - } + logCompilation(`Compiler${name ? ` ${name}` : ''} starting... `); if (configPath) { this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`); } @@ -65,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}'`); @@ -81,12 +84,8 @@ class CLIPlugin { (compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => { const name = getCompilationName(); - if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG_FORCE) { - process.stderr.write(`Compiler${name ? ` ${name}` : ''} finished`); - } else { - 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..9a80e7b3267 --- /dev/null +++ b/test/build/start-finish-force-log/start-finish-force-log.test.js @@ -0,0 +1,37 @@ +'use strict'; + +const { run } = 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'); + expect(stdout).toBeTruthy(); + }); + + 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"); + expect(stdout).toBeTruthy(); + }); + + 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"); + expect(stdout).toBeTruthy(); + }); +}); 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', +}; From c6134cd4f4836aa3d26b27d12c1377c2b8a09db6 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 27 Mar 2021 21:18:39 +0530 Subject: [PATCH 3/4] test: add watch --- .../start-finish-force-log.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 index 9a80e7b3267..5be10680dfe 100644 --- 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 @@ -1,6 +1,6 @@ 'use strict'; -const { run } = require('../../utils/test-utils'); +const { run, runWatch } = require('../../utils/test-utils'); describe('start finish force log', () => { it('start finish force log when env is set', () => { @@ -23,6 +23,15 @@ describe('start finish force log', () => { expect(stdout).toBeTruthy(); }); + 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'); + expect(stdout).toBeTruthy(); + }); + 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 }, From cfccfa97b36b5708b8c2ad5bd39e304bbd887571 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 27 Mar 2021 21:43:04 +0530 Subject: [PATCH 4/4] test: update --- .../start-finish-force-log.test.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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 index 5be10680dfe..3701bb95d08 100644 --- 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 @@ -1,6 +1,6 @@ 'use strict'; -const { run, runWatch } = require('../../utils/test-utils'); +const { run, runWatch, isWebpack5 } = require('../../utils/test-utils'); describe('start finish force log', () => { it('start finish force log when env is set', () => { @@ -10,7 +10,8 @@ describe('start finish force log', () => { expect(exitCode).toBe(0); expect(stderr).toContain('Compiler starting...'); expect(stderr).toContain('Compiler finished'); - expect(stdout).toBeTruthy(); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); }); it('should show name of the config', () => { @@ -20,7 +21,8 @@ describe('start finish force log', () => { expect(exitCode).toBe(0); expect(stderr).toContain("Compiler 'log config' starting..."); expect(stderr).toContain("Compiler 'log config' finished"); - expect(stdout).toBeTruthy(); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); }); it('should work with watch', async () => { @@ -29,7 +31,8 @@ describe('start finish force log', () => { }); expect(stderr).toContain('Compiler starting...'); expect(stderr).toContain('Compiler finished'); - expect(stdout).toBeTruthy(); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); }); it('should work with multi compiler', () => { @@ -41,6 +44,7 @@ describe('start finish force log', () => { expect(stderr).toContain("Compiler 'Satoru' starting..."); expect(stderr).toContain("Compiler 'Gojou' finished"); expect(stderr).toContain("Compiler 'Satoru' finished"); - expect(stdout).toBeTruthy(); + const output = isWebpack5 ? 'compiled successfully' : 'main.js'; + expect(stdout).toContain(output); }); });