Skip to content

Commit

Permalink
feat: progress supports string argument (#2000)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 30, 2020
1 parent 987e12b commit f13346e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
12 changes: 10 additions & 2 deletions packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class WebpackCLIPlugin {
constructor(options) {
this.options = options;
}

async apply(compiler) {
const compilers = compiler.compilers || [compiler];

Expand All @@ -18,11 +19,18 @@ class WebpackCLIPlugin {
let progressPluginExists;

if (compiler.options.plugins) {
progressPluginExists = Boolean(compiler.options.plugins.find((e) => e instanceof ProgressPlugin));
progressPluginExists = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));
}

if (!progressPluginExists) {
new ProgressPlugin().apply(compiler);
if (typeof this.options.progress === 'string' && this.options.progress !== 'profile') {
logger.error(`Invalid ${this.options.progress} value for the progress option. Allowed value is profile.`);
process.exit(2);
}

const isProfile = this.options.progress === 'profile';

new ProgressPlugin({ profile: isProfile }).apply(compiler);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const core = [
{
name: 'progress',
usage: '--progress',
type: Boolean,
type: [Boolean, String],
group: BASIC_GROUP,
description: 'Print compilation progress during build',
},
Expand Down
27 changes: 24 additions & 3 deletions test/progress/progress-flag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,38 @@ const { run } = require('../utils/test-utils');

describe('progress flag', () => {
it('should show progress', () => {
const { stderr, stdout } = run(__dirname, ['--progress']);
const { stderr, stdout, exitCode } = run(__dirname, ['--progress']);

expect(exitCode).toBe(0);
expect(stderr).not.toMatch(/\[webpack\.Progress] \d+ ms setup/);
expect(stderr).toContain('[webpack.Progress] 100%');
expect(stdout).toContain('main.js');
});

it('should support the "profile" value', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--progress=profile']);

expect(exitCode).toBe(0);
expect(stderr).toMatch(/\[webpack\.Progress] \d+ ms setup/);
expect(stderr).toContain('[webpack.Progress] 100%');
expect(stdout).toContain('main.js');
});

it('should not support invalid value', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--progress=unknown']);

expect(exitCode).toBe(2);
expect(stderr).toContain('Invalid unknown value for the progress option. Allowed value is profile.');
expect(stdout).toBeFalsy();
});

it('should not add duplicate plugins', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['-c', 'webpack.progress.config.js', '--progress']);
// Only 1 progress plugin should be applied to the compiler

expect(exitCode).toEqual(0);
expect(stdout.match(/ProgressPlugin/g)).toHaveLength(1);
expect(stderr).not.toMatch(/\[webpack\.Progress] \d+ ms setup/);
expect(stderr).toContain('[webpack.Progress] 100%');
expect(stdout).toContain('main.js');
expect(exitCode).toEqual(0);
});
});

0 comments on commit f13346e

Please sign in to comment.