Skip to content

Commit 0595603

Browse files
fix: the --progress option is working with --json (#2276)
1 parent 818fc1c commit 0595603

File tree

6 files changed

+96
-27
lines changed

6 files changed

+96
-27
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class CLIPlugin {
7979
apply(compiler) {
8080
this.logger = compiler.getInfrastructureLogger('webpack-cli');
8181

82-
if (this.options.progress && this.options.helpfulOutput) {
82+
if (this.options.progress) {
8383
this.setupProgressPlugin(compiler);
8484
}
8585

@@ -95,9 +95,7 @@ class CLIPlugin {
9595
this.setupBundleAnalyzerPlugin(compiler);
9696
}
9797

98-
if (this.options.helpfulOutput) {
99-
this.setupHelpfulOutput(compiler);
100-
}
98+
this.setupHelpfulOutput(compiler);
10199
}
102100
}
103101

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,10 @@ class WebpackCLI {
12341234
.on('error', handleWriteError)
12351235
.pipe(createWriteStream(options.json))
12361236
.on('error', handleWriteError)
1237-
.on('close', () => logger.success(`stats are successfully stored as json to ${options.json}`));
1237+
// Use stderr to logging
1238+
.on('close', () =>
1239+
process.stderr.write(`[webpack-cli] ${green(`stats are successfully stored as json to ${options.json}`)}\n`),
1240+
);
12381241
}
12391242
} else {
12401243
const printedStats = stats.toString(foundStats);

test/build-errors/errors.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ describe('errors', () => {
3232
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);
3333

3434
expect(exitCode).toBe(1);
35-
expect(stderr).toBeFalsy();
36-
expect(stdout).toContain('stats are successfully stored as json to stats.json');
35+
expect(stderr).toContain('stats are successfully stored as json to stats.json');
36+
expect(stdout).toBeFalsy();
3737

3838
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (error, data) => {
3939
expect(error).toBe(null);

test/build-warnings/warnings.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ describe('warnings', () => {
3333
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);
3434

3535
expect(exitCode).toBe(0);
36-
expect(stderr).toBeFalsy();
37-
expect(stdout).toContain('stats are successfully stored as json to stats.json');
38-
36+
expect(stderr).toContain('stats are successfully stored as json to stats.json');
37+
expect(stdout).toBeFalsy();
3938
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
4039

4140
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (error, data) => {

test/json/json.test.js

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ const { resolve } = require('path');
55

66
const successMessage = 'stats are successfully stored as json to stats.json';
77

8-
describe('json flag', () => {
9-
it('should return valid json', () => {
10-
const { stdout, exitCode } = run(__dirname, ['--json']);
11-
expect(() => JSON.parse(stdout)).not.toThrow();
8+
describe('json', () => {
9+
it('should work and output json stats', () => {
10+
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);
11+
1212
expect(exitCode).toBe(0);
13+
expect(stderr).toBeFalsy();
14+
expect(() => JSON.parse(stdout)).not.toThrow();
1315
expect(JSON.parse(stdout)['hash']).toBeDefined();
1416
});
1517

16-
it('should store json to a file', (done) => {
17-
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json']);
18+
it('should work and store json to a file', (done) => {
19+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);
1820

19-
expect(stdout).toContain(successMessage);
2021
expect(exitCode).toBe(0);
22+
expect(stderr).toContain(successMessage);
23+
expect(stdout).toBeFalsy();
2124
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
25+
2226
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
2327
expect(err).toBe(null);
2428
expect(JSON.parse(data)['hash']).toBeTruthy();
@@ -29,12 +33,31 @@ describe('json flag', () => {
2933
});
3034
});
3135

32-
it('should store json to a file and respect --color flag', (done) => {
33-
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json', '--color']);
36+
it('should work and store json to a file and respect --color flag', (done) => {
37+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--color']);
3438

35-
expect(stdout).toContain(`\u001b[32m${successMessage}`);
3639
expect(exitCode).toBe(0);
40+
expect(stderr).toContain(`\u001b[32m${successMessage}`);
41+
expect(stdout).toBeFalsy();
42+
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
3743

44+
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
45+
expect(err).toBe(null);
46+
expect(JSON.parse(data)['hash']).toBeTruthy();
47+
expect(JSON.parse(data)['version']).toBeTruthy();
48+
expect(JSON.parse(data)['time']).toBeTruthy();
49+
expect(() => JSON.parse(data)).not.toThrow();
50+
done();
51+
});
52+
});
53+
54+
it('should work and store json to a file and respect --no-color', (done) => {
55+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--no-color']);
56+
57+
expect(exitCode).toBe(0);
58+
expect(stderr).not.toContain(`\u001b[32m${successMessage}`);
59+
expect(stderr).toContain(`${successMessage}`);
60+
expect(stdout).toBeFalsy();
3861
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
3962

4063
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
@@ -47,13 +70,31 @@ describe('json flag', () => {
4770
});
4871
});
4972

50-
it('should store json to a file and respect --no-color', (done) => {
51-
const { stdout, exitCode } = run(__dirname, ['--json', 'stats.json', '--no-color']);
73+
it('should work using the "-j" option (alias)', () => {
74+
const { exitCode, stderr, stdout } = run(__dirname, ['-j']);
5275

53-
expect(stdout).not.toContain(`\u001b[32m${successMessage}`);
54-
expect(stdout).toContain(`${successMessage}`);
5576
expect(exitCode).toBe(0);
77+
expect(stderr).toBeFalsy();
78+
expect(() => JSON.parse(stdout)).not.toThrow();
79+
expect(JSON.parse(stdout)['hash']).toBeDefined();
80+
});
81+
82+
it('should work and output json stats with the "--progress" option', () => {
83+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--progress']);
5684

85+
expect(exitCode).toBe(0);
86+
expect(stderr).toContain('webpack.Progress');
87+
expect(() => JSON.parse(stdout)).not.toThrow();
88+
expect(JSON.parse(stdout)['hash']).toBeDefined();
89+
});
90+
91+
it('should work and store json to a file with the "--progress" option', (done) => {
92+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--progress']);
93+
94+
expect(exitCode).toBe(0);
95+
expect(stderr).toContain('webpack.Progress');
96+
expect(stderr).toContain(successMessage);
97+
expect(stdout).toBeFalsy();
5798
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
5899

59100
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
@@ -66,10 +107,33 @@ describe('json flag', () => {
66107
});
67108
});
68109

69-
it('should return valid json with -j alias', () => {
70-
const { stdout, exitCode } = run(__dirname, ['-j']);
71-
expect(() => JSON.parse(stdout)).not.toThrow();
110+
it('should work and output json stats with cli logs', () => {
111+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--config', 'logging.config.js']);
112+
72113
expect(exitCode).toBe(0);
114+
expect(stderr).toContain('Compilation starting');
115+
expect(stderr).toContain('Compilation finished');
116+
expect(() => JSON.parse(stdout)).not.toThrow();
73117
expect(JSON.parse(stdout)['hash']).toBeDefined();
74118
});
119+
120+
it('should work and store json to a file with cli logs', (done) => {
121+
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--config', 'logging.config.js']);
122+
123+
expect(exitCode).toBe(0);
124+
expect(stderr).toContain('Compilation starting');
125+
expect(stderr).toContain('Compilation finished');
126+
expect(stderr).toContain(successMessage);
127+
expect(stdout).toBeFalsy();
128+
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();
129+
130+
readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
131+
expect(err).toBe(null);
132+
expect(JSON.parse(data)['hash']).toBeTruthy();
133+
expect(JSON.parse(data)['version']).toBeTruthy();
134+
expect(JSON.parse(data)['time']).toBeTruthy();
135+
expect(() => JSON.parse(data)).not.toThrow();
136+
done();
137+
});
138+
});
75139
});

test/json/logging.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
infrastructureLogging: {
3+
level: 'log',
4+
},
5+
};

0 commit comments

Comments
 (0)