Skip to content

Commit f13346e

Browse files
feat: progress supports string argument (#2000)
1 parent 987e12b commit f13346e

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class WebpackCLIPlugin {
88
constructor(options) {
99
this.options = options;
1010
}
11+
1112
async apply(compiler) {
1213
const compilers = compiler.compilers || [compiler];
1314

@@ -18,11 +19,18 @@ class WebpackCLIPlugin {
1819
let progressPluginExists;
1920

2021
if (compiler.options.plugins) {
21-
progressPluginExists = Boolean(compiler.options.plugins.find((e) => e instanceof ProgressPlugin));
22+
progressPluginExists = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));
2223
}
2324

2425
if (!progressPluginExists) {
25-
new ProgressPlugin().apply(compiler);
26+
if (typeof this.options.progress === 'string' && this.options.progress !== 'profile') {
27+
logger.error(`Invalid ${this.options.progress} value for the progress option. Allowed value is profile.`);
28+
process.exit(2);
29+
}
30+
31+
const isProfile = this.options.progress === 'profile';
32+
33+
new ProgressPlugin({ profile: isProfile }).apply(compiler);
2634
}
2735
}
2836
}

packages/webpack-cli/lib/utils/cli-flags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const core = [
108108
{
109109
name: 'progress',
110110
usage: '--progress',
111-
type: Boolean,
111+
type: [Boolean, String],
112112
group: BASIC_GROUP,
113113
description: 'Print compilation progress during build',
114114
},

test/progress/progress-flag.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,38 @@ const { run } = require('../utils/test-utils');
44

55
describe('progress flag', () => {
66
it('should show progress', () => {
7-
const { stderr, stdout } = run(__dirname, ['--progress']);
7+
const { stderr, stdout, exitCode } = run(__dirname, ['--progress']);
8+
9+
expect(exitCode).toBe(0);
10+
expect(stderr).not.toMatch(/\[webpack\.Progress] \d+ ms setup/);
11+
expect(stderr).toContain('[webpack.Progress] 100%');
12+
expect(stdout).toContain('main.js');
13+
});
14+
15+
it('should support the "profile" value', () => {
16+
const { stderr, stdout, exitCode } = run(__dirname, ['--progress=profile']);
17+
18+
expect(exitCode).toBe(0);
19+
expect(stderr).toMatch(/\[webpack\.Progress] \d+ ms setup/);
820
expect(stderr).toContain('[webpack.Progress] 100%');
921
expect(stdout).toContain('main.js');
1022
});
1123

24+
it('should not support invalid value', () => {
25+
const { stderr, stdout, exitCode } = run(__dirname, ['--progress=unknown']);
26+
27+
expect(exitCode).toBe(2);
28+
expect(stderr).toContain('Invalid unknown value for the progress option. Allowed value is profile.');
29+
expect(stdout).toBeFalsy();
30+
});
31+
1232
it('should not add duplicate plugins', () => {
1333
const { stderr, stdout, exitCode } = run(__dirname, ['-c', 'webpack.progress.config.js', '--progress']);
14-
// Only 1 progress plugin should be applied to the compiler
34+
35+
expect(exitCode).toEqual(0);
1536
expect(stdout.match(/ProgressPlugin/g)).toHaveLength(1);
37+
expect(stderr).not.toMatch(/\[webpack\.Progress] \d+ ms setup/);
1638
expect(stderr).toContain('[webpack.Progress] 100%');
1739
expect(stdout).toContain('main.js');
18-
expect(exitCode).toEqual(0);
1940
});
2041
});

0 commit comments

Comments
 (0)