Last active
May 4, 2022 21:04
-
-
Save smikula/0e85fd9662f020f6cd45c98ee89ebd8a to your computer and use it in GitHub Desktop.
Reading a BIG stats.json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let JSONStream = require('jsonstream'); | |
| let fs = require('fs'); | |
| let es = require('event-stream'); | |
| function readStatsJson(path) { | |
| return new Promise((resolve, reject) => { | |
| let parsedObject = {}; | |
| let stream = fs | |
| .createReadStream(path, { encoding: 'utf8' }) | |
| .pipe(JSONStream.parse('$*')) | |
| .pipe( | |
| es.mapSync(data => { | |
| parsedObject[data.key] = data.value; | |
| }) | |
| ); | |
| stream.on('close', () => { | |
| resolve(parsedObject); | |
| }); | |
| stream.on('error', reject); | |
| }); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When webpack's stats get really big, it isn't possible to read in the file as a single string. (You'll get
RangeError: Invalid string length.) This is a solution to read and parse the file via a stream. Given how big the stats are, you'll probably need to give node more memory (e.g.--max-old-space-size=4096).Note that you can't write an overlarge stats object with JSONStream, because internally it still tries to use the native
JSON.stringifyon the object. For that, the only thing I found that works is BFJ.