Skip to content

Commit e8f2f20

Browse files
fix: use compiler.apply for Progress Plugin (#1772)
* fix: use default webpack handler * chore: update tests Co-authored-by: James George <jamesgeorge998001@gmail.com>
1 parent 0110815 commit e8f2f20

File tree

2 files changed

+4
-28
lines changed

2 files changed

+4
-28
lines changed

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const webpack = require('webpack');
22
const logger = require('./logger');
3-
const { cyanBright, greenBright } = require('colorette');
43
const { CompilerOutput } = require('./CompilerOutput');
5-
const readline = require('readline');
64

75
class Compiler {
86
constructor() {
@@ -18,36 +16,15 @@ class Compiler {
1816

1917
compilation.hooks.beforeRun.tap('webpackProgress', () => {
2018
if (outputOptions.progress) {
21-
process.stdout.write('\n');
22-
const defaultProgressPluginHandler = (percent, msg) => {
23-
percent = Math.floor(percent * 100);
24-
readline.clearLine(process.stdout, 0);
25-
readline.cursorTo(process.stdout, 0, null);
26-
if (percent !== undefined) {
27-
process.stdout.write(' (');
28-
for (let i = 0; i <= 100; i += 10) {
29-
if (i <= percent) {
30-
process.stdout.write(greenBright('#'));
31-
} else {
32-
process.stdout.write('#');
33-
}
34-
}
35-
process.stdout.write(`) ${percent}% : `);
36-
process.stdout.write(`${cyanBright(msg)}`);
37-
if (percent === 100) {
38-
process.stdout.write(`${cyanBright('Compilation completed\n')}`);
39-
}
40-
}
41-
};
4219
if (!progressPluginExists) {
43-
new ProgressPlugin(defaultProgressPluginHandler).apply(compilation);
20+
new ProgressPlugin().apply(compilation);
4421
} else {
4522
if (!progressPluginExists.handler) {
4623
options.plugins = options.plugins.filter((e) => e !== progressPluginExists);
4724
Object.keys(progressPluginExists).map((opt) => {
4825
ProgressPlugin.defaultOptions[opt] = progressPluginExists[opt];
4926
});
50-
new ProgressPlugin(defaultProgressPluginHandler).apply(compilation);
27+
new ProgressPlugin().apply(compilation);
5128
} else {
5229
progressPluginExists.apply(compilation);
5330
}

test/progress/progress-flag.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const { run } = require('../utils/test-utils');
66
describe('progress flag', () => {
77
it('should show progress', () => {
88
const { stderr, stdout } = run(__dirname, ['--progress']);
9-
expect(stderr).toBeFalsy();
10-
expect(stdout).toContain('100%');
11-
expect(stdout).toContain('Compilation completed');
9+
expect(stderr).toContain('[webpack.Progress] 100%');
10+
expect(stdout).toContain('main.js');
1211
});
1312
});

0 commit comments

Comments
 (0)