Skip to content

Commit

Permalink
feat: add flag to force start finish log (#2566)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv authored Mar 28, 2021
1 parent 9a18e7f commit 281aad3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/webpack-cli/lib/plugins/CLIPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`);
}
Expand All @@ -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}'`);
Expand All @@ -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...`);
Expand Down
1 change: 1 addition & 0 deletions test/build/start-finish-force-log/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Itadori Yuuji")
50 changes: 50 additions & 0 deletions test/build/start-finish-force-log/start-finish-force-log.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions test/build/start-finish-force-log/webpack.config.array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = [
{
name: 'Gojou',
mode: 'development',
},
{
name: 'Satoru',
mode: 'development',
},
];
3 changes: 3 additions & 0 deletions test/build/start-finish-force-log/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
mode: 'development',
};

0 comments on commit 281aad3

Please sign in to comment.