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

fix: the --progress option is working with --json #2276

Merged
merged 4 commits into from Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/webpack-cli/lib/plugins/CLIPlugin.js
Expand Up @@ -79,7 +79,7 @@ class CLIPlugin {
apply(compiler) {
this.logger = compiler.getInfrastructureLogger('webpack-cli');

if (this.options.progress && this.options.helpfulOutput) {
if (this.options.progress) {
this.setupProgressPlugin(compiler);
}

Expand All @@ -95,9 +95,7 @@ class CLIPlugin {
this.setupBundleAnalyzerPlugin(compiler);
}

if (this.options.helpfulOutput) {
this.setupHelpfulOutput(compiler);
}
this.setupHelpfulOutput(compiler);
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -1234,7 +1234,10 @@ class WebpackCLI {
.on('error', handleWriteError)
.pipe(createWriteStream(options.json))
.on('error', handleWriteError)
.on('close', () => logger.success(`stats are successfully stored as json to ${options.json}`));
// Use stderr to logging
.on('close', () =>
process.stderr.write(`[webpack-cli] ${green(`stats are successfully stored as json to ${options.json}`)}\n`),
);
}
} else {
const printedStats = stats.toString(foundStats);
Expand Down
4 changes: 2 additions & 2 deletions test/build-errors/errors.test.js
Expand Up @@ -32,8 +32,8 @@ describe('errors', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);

expect(exitCode).toBe(1);
expect(stderr).toBeFalsy();
expect(stdout).toContain('stats are successfully stored as json to stats.json');
expect(stderr).toContain('stats are successfully stored as json to stats.json');
expect(stdout).toBeFalsy();

readFile(resolve(__dirname, 'stats.json'), 'utf-8', (error, data) => {
expect(error).toBe(null);
Expand Down
5 changes: 2 additions & 3 deletions test/build-warnings/warnings.test.js
Expand Up @@ -33,9 +33,8 @@ describe('warnings', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json']);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain('stats are successfully stored as json to stats.json');

expect(stderr).toContain('stats are successfully stored as json to stats.json');
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

readFile(resolve(__dirname, 'stats.json'), 'utf-8', (error, data) => {
Expand Down
98 changes: 81 additions & 17 deletions test/json/json.test.js
Expand Up @@ -5,20 +5,24 @@ const { resolve } = require('path');

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

describe('json flag', () => {
it('should return valid json', () => {
const { stdout, exitCode } = run(__dirname, ['--json']);
expect(() => JSON.parse(stdout)).not.toThrow();
describe('json', () => {
it('should work and output json stats', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json']);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});

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

expect(stdout).toContain(successMessage);
expect(exitCode).toBe(0);
expect(stderr).toContain(successMessage);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(JSON.parse(data)['hash']).toBeTruthy();
Expand All @@ -29,12 +33,31 @@ describe('json flag', () => {
});
});

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

expect(stdout).toContain(`\u001b[32m${successMessage}`);
expect(exitCode).toBe(0);
expect(stderr).toContain(`\u001b[32m${successMessage}`);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(JSON.parse(data)['hash']).toBeTruthy();
expect(JSON.parse(data)['version']).toBeTruthy();
expect(JSON.parse(data)['time']).toBeTruthy();
expect(() => JSON.parse(data)).not.toThrow();
done();
});
});

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

expect(exitCode).toBe(0);
expect(stderr).not.toContain(`\u001b[32m${successMessage}`);
expect(stderr).toContain(`${successMessage}`);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

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

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

expect(stdout).not.toContain(`\u001b[32m${successMessage}`);
expect(stdout).toContain(`${successMessage}`);
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});

it('should work and output json stats with the "--progress" option', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--progress']);

expect(exitCode).toBe(0);
expect(stderr).toContain('webpack.Progress');
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});

it('should work and store json to a file with the "--progress" option', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--progress']);

expect(exitCode).toBe(0);
expect(stderr).toContain('webpack.Progress');
expect(stderr).toContain(successMessage);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

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

it('should return valid json with -j alias', () => {
const { stdout, exitCode } = run(__dirname, ['-j']);
expect(() => JSON.parse(stdout)).not.toThrow();
it('should work and output json stats with cli logs', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--config', 'logging.config.js']);

expect(exitCode).toBe(0);
expect(stderr).toContain('Compilation starting');
expect(stderr).toContain('Compilation finished');
expect(() => JSON.parse(stdout)).not.toThrow();
expect(JSON.parse(stdout)['hash']).toBeDefined();
});

it('should work and store json to a file with cli logs', (done) => {
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--config', 'logging.config.js']);

expect(exitCode).toBe(0);
expect(stderr).toContain('Compilation starting');
expect(stderr).toContain('Compilation finished');
expect(stderr).toContain(successMessage);
expect(stdout).toBeFalsy();
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

readFile(resolve(__dirname, 'stats.json'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(JSON.parse(data)['hash']).toBeTruthy();
expect(JSON.parse(data)['version']).toBeTruthy();
expect(JSON.parse(data)['time']).toBeTruthy();
expect(() => JSON.parse(data)).not.toThrow();
done();
});
});
});
5 changes: 5 additions & 0 deletions test/json/logging.config.js
@@ -0,0 +1,5 @@
module.exports = {
infrastructureLogging: {
level: 'log',
},
};