Skip to content

Commit 281aad3

Browse files
authored
feat: add flag to force start finish log (#2566)
1 parent 9a18e7f commit 281aad3

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

packages/webpack-cli/lib/plugins/CLIPlugin.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ class CLIPlugin {
4040
setupHelpfulOutput(compiler) {
4141
const pluginName = 'webpack-cli';
4242
const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : '');
43+
const logCompilation = (message) => {
44+
if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG) {
45+
process.stderr.write(message);
46+
} else {
47+
this.logger.log(message);
48+
}
49+
};
4350

4451
const { configPath } = this.options;
4552

4653
compiler.hooks.run.tap(pluginName, () => {
4754
const name = getCompilationName();
4855

49-
this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`);
50-
56+
logCompilation(`Compiler${name ? ` ${name}` : ''} starting... `);
5157
if (configPath) {
5258
this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`);
5359
}
@@ -62,7 +68,7 @@ class CLIPlugin {
6268

6369
const name = getCompilationName();
6470

65-
this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`);
71+
logCompilation(`Compiler${name ? ` ${name}` : ''} starting... `);
6672

6773
if (configPath) {
6874
this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`);
@@ -79,8 +85,7 @@ class CLIPlugin {
7985
(compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => {
8086
const name = getCompilationName();
8187

82-
this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`);
83-
88+
logCompilation(`Compiler${name ? ` ${name}` : ''} finished`);
8489
process.nextTick(() => {
8590
if (compiler.watchMode) {
8691
this.logger.log(`Compiler${name ? `${name}` : ''} is watching files for updates...`);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Itadori Yuuji")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const { run, runWatch, isWebpack5 } = require('../../utils/test-utils');
4+
5+
describe('start finish force log', () => {
6+
it('start finish force log when env is set', () => {
7+
const { exitCode, stderr, stdout } = run(__dirname, [], {
8+
env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true },
9+
});
10+
expect(exitCode).toBe(0);
11+
expect(stderr).toContain('Compiler starting...');
12+
expect(stderr).toContain('Compiler finished');
13+
const output = isWebpack5 ? 'compiled successfully' : 'main.js';
14+
expect(stdout).toContain(output);
15+
});
16+
17+
it('should show name of the config', () => {
18+
const { exitCode, stderr, stdout } = run(__dirname, ['--name', 'log config'], {
19+
env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true },
20+
});
21+
expect(exitCode).toBe(0);
22+
expect(stderr).toContain("Compiler 'log config' starting...");
23+
expect(stderr).toContain("Compiler 'log config' finished");
24+
const output = isWebpack5 ? 'compiled successfully' : 'main.js';
25+
expect(stdout).toContain(output);
26+
});
27+
28+
it('should work with watch', async () => {
29+
const { stderr, stdout } = await runWatch(__dirname, ['watch'], {
30+
env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true },
31+
});
32+
expect(stderr).toContain('Compiler starting...');
33+
expect(stderr).toContain('Compiler finished');
34+
const output = isWebpack5 ? 'compiled successfully' : 'main.js';
35+
expect(stdout).toContain(output);
36+
});
37+
38+
it('should work with multi compiler', () => {
39+
const { exitCode, stderr, stdout } = run(__dirname, ['--config', './webpack.config.array.js'], {
40+
env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true },
41+
});
42+
expect(exitCode).toBe(0);
43+
expect(stderr).toContain("Compiler 'Gojou' starting...");
44+
expect(stderr).toContain("Compiler 'Satoru' starting...");
45+
expect(stderr).toContain("Compiler 'Gojou' finished");
46+
expect(stderr).toContain("Compiler 'Satoru' finished");
47+
const output = isWebpack5 ? 'compiled successfully' : 'main.js';
48+
expect(stdout).toContain(output);
49+
});
50+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = [
2+
{
3+
name: 'Gojou',
4+
mode: 'development',
5+
},
6+
{
7+
name: 'Satoru',
8+
mode: 'development',
9+
},
10+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
mode: 'development',
3+
};

0 commit comments

Comments
 (0)