forked from webpack-contrib/webpack-bundle-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
bfj
module with custom stats.json
writer
- Loading branch information
Showing
4 changed files
with
86 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const {createWriteStream} = require('fs'); | ||
const {Readable} = require('stream'); | ||
|
||
const _ = require('lodash'); | ||
|
||
class StatsSerializeStream extends Readable { | ||
constructor(stats) { | ||
super(); | ||
this._indentLevel = 0; | ||
this._stringifier = this._stringify(stats); | ||
} | ||
|
||
get _indent() { | ||
return ' '.repeat(this._indentLevel); | ||
} | ||
|
||
_read() { | ||
let readMore = true; | ||
|
||
while (readMore) { | ||
const {value, done} = this._stringifier.next(); | ||
|
||
if (done) { | ||
this.push(null); | ||
readMore = false; | ||
} else { | ||
readMore = this.push(value); | ||
} | ||
} | ||
} | ||
|
||
* _stringify(obj) { | ||
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || obj === null) { | ||
yield JSON.stringify(obj); | ||
} else if (_.isArray(obj)) { | ||
yield '['; | ||
this._indentLevel++; | ||
|
||
let isFirst = true; | ||
for (let item of obj) { | ||
if (item === undefined) { | ||
item = null; | ||
} | ||
|
||
yield `${isFirst ? '' : ','}\n${this._indent}`; | ||
yield* this._stringify(item); | ||
isFirst = false; | ||
} | ||
|
||
this._indentLevel--; | ||
yield obj.length ? `\n${this._indent}]` : ']'; | ||
} else if (_.isPlainObject(obj)) { | ||
yield '{'; | ||
this._indentLevel++; | ||
|
||
let isFirst = true; | ||
const entries = Object.entries(obj); | ||
for (const [itemKey, itemValue] of entries) { | ||
if (itemValue === undefined) { | ||
continue; | ||
} | ||
|
||
yield `${isFirst ? '' : ','}\n${this._indent}${JSON.stringify(itemKey)}: `; | ||
yield* this._stringify(itemValue); | ||
isFirst = false; | ||
} | ||
|
||
this._indentLevel--; | ||
yield entries.length ? `\n${this._indent}}` : '}'; | ||
} | ||
} | ||
} | ||
|
||
exports.StatsSerializeStream = StatsSerializeStream; | ||
exports.writeStats = writeStats; | ||
|
||
async function writeStats(stats, filepath) { | ||
return new Promise((resolve, reject) => { | ||
new StatsSerializeStream(stats) | ||
.on('end', resolve) | ||
.on('error', reject) | ||
.pipe(createWriteStream(filepath)); | ||
}); | ||
} |