Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: progress supports string argument #2000

Merged
merged 5 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 profile option. Allowed value is profile.`);
alexander-akait marked this conversation as resolved.
Show resolved Hide resolved
process.exit(2);
}

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

new ProgressPlugin({ profile: isProfile }).apply(compiler);
Comment on lines +31 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if an invalid value is provided webpack --progress=invalid. Should we throw error ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, please review again

}
}
}
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('[webpack-cli] Invalid unknown value for the profile option. Allowed value is profile.');
alexander-akait marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});