diff --git a/packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js b/packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js index c79c8654a95..0127163c3e3 100644 --- a/packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js @@ -8,6 +8,7 @@ class WebpackCLIPlugin { constructor(options) { this.options = options; } + async apply(compiler) { const compilers = compiler.compilers || [compiler]; @@ -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); } } } diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 807d94c0677..091097dfc64 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -108,7 +108,7 @@ const core = [ { name: 'progress', usage: '--progress', - type: Boolean, + type: [Boolean, String], group: BASIC_GROUP, description: 'Print compilation progress during build', }, diff --git a/test/progress/progress-flag.test.js b/test/progress/progress-flag.test.js index b98aad11e83..51791710922 100644 --- a/test/progress/progress-flag.test.js +++ b/test/progress/progress-flag.test.js @@ -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); }); });