diff --git a/packages/webpack-cli/lib/plugins/CLIPlugin.js b/packages/webpack-cli/lib/plugins/CLIPlugin.js index 643088643e5..584d6e21a07 100644 --- a/packages/webpack-cli/lib/plugins/CLIPlugin.js +++ b/packages/webpack-cli/lib/plugins/CLIPlugin.js @@ -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); } @@ -95,9 +95,7 @@ class CLIPlugin { this.setupBundleAnalyzerPlugin(compiler); } - if (this.options.helpfulOutput) { - this.setupHelpfulOutput(compiler); - } + this.setupHelpfulOutput(compiler); } } diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 3a7f744ee81..67201852276 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -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); diff --git a/test/build-errors/errors.test.js b/test/build-errors/errors.test.js index 50a397ebffa..30fac63060d 100644 --- a/test/build-errors/errors.test.js +++ b/test/build-errors/errors.test.js @@ -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); diff --git a/test/build-warnings/warnings.test.js b/test/build-warnings/warnings.test.js index 27422faf42e..8bf676da732 100644 --- a/test/build-warnings/warnings.test.js +++ b/test/build-warnings/warnings.test.js @@ -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) => { diff --git a/test/json/json.test.js b/test/json/json.test.js index 9fd1ef88964..64441e4504a 100644 --- a/test/json/json.test.js +++ b/test/json/json.test.js @@ -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(); @@ -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) => { @@ -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) => { @@ -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(); + }); + }); }); diff --git a/test/json/logging.config.js b/test/json/logging.config.js new file mode 100644 index 00000000000..481fe38bff4 --- /dev/null +++ b/test/json/logging.config.js @@ -0,0 +1,5 @@ +module.exports = { + infrastructureLogging: { + level: 'log', + }, +};