From 4869904dc60a4e2bf2da2467aeab26b7bca5537c Mon Sep 17 00:00:00 2001 From: Simon Flack Date: Mon, 3 Apr 2017 18:57:48 +0200 Subject: [PATCH] Stream JSON output (--json) This fixes webpack/webpack#4629 JSON.stringify can fail with `RangeError: Invalid string length` when it runs out of memory. See nodejs/node-v0.x-archivei#14170. With this fix, I was able to write out the profile JSON without errors. --- bin/webpack.js | 5 ++++- package.json | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/webpack.js b/bin/webpack.js index 37125533c52..ea43aed5e63 100755 --- a/bin/webpack.js +++ b/bin/webpack.js @@ -334,7 +334,10 @@ function processOptions(options) { process.exit(1); // eslint-disable-line } if(outputOptions.json) { - process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n"); + var stringify = require('JSONStream').stringify('', '', ''); + stringify.pipe(process.stdout); + stringify.write(stats.toJson(outputOptions)); + stringify.end(); } else if(stats.hash !== lastHash) { lastHash = stats.hash; process.stdout.write(stats.toString(outputOptions) + "\n"); diff --git a/package.json b/package.json index a563fa8263e..ce49682e097 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "author": "Tobias Koppers @sokra", "description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.", "dependencies": { + "JSONStream": "^1.3.1", "acorn": "^5.0.0", "acorn-dynamic-import": "^2.0.0", "ajv": "^4.7.0",