From eedce4247573ae78bd99a53b88eb4cdc896068b6 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Sun, 11 Oct 2020 19:51:38 +0300 Subject: [PATCH] fix: avoid unnecessary stringify --- packages/webpack-cli/lib/utils/Compiler.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/webpack-cli/lib/utils/Compiler.js b/packages/webpack-cli/lib/utils/Compiler.js index 6c46b47c7b4..58924077ce7 100644 --- a/packages/webpack-cli/lib/utils/Compiler.js +++ b/packages/webpack-cli/lib/utils/Compiler.js @@ -78,9 +78,10 @@ class Compiler { process.exitCode = 1; } if (outputOptions.json === true) { - process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + '\n'); + process.stdout.write(stats.toJson(outputOptions) + '\n'); } else if (stats.hash !== lastHash) { lastHash = stats.hash; + if (stats.compilation && stats.compilation.errors.length !== 0) { const errors = stats.compilation.errors; errors.forEach((statErr) => { @@ -88,8 +89,10 @@ class Compiler { statsErrors.push({ name: statErr.message, loc: errLoc }); }); } - const JSONStats = JSON.stringify(stats.toJson(outputOptions), null, 2); + if (typeof outputOptions.json === 'string') { + const JSONStats = stats.toJson(outputOptions); + try { writeFileSync(outputOptions.json, JSONStats); logger.success(`stats are successfully stored as json to ${outputOptions.json}`); @@ -97,6 +100,7 @@ class Compiler { logger.error(err); } } + return this.generateOutput(outputOptions, stats, statsErrors); } }