Skip to content

Commit

Permalink
Replace bfj module with custom stats.json writer
Browse files Browse the repository at this point in the history
  • Loading branch information
th0r committed Nov 6, 2020
1 parent ee6c7a9 commit e9bdf60
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
31 changes: 0 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"dependencies": {
"acorn": "^8.0.4",
"acorn-walk": "^8.0.0",
"bfj": "^7.0.2",
"chalk": "^4.1.0",
"commander": "^6.2.0",
"ejs": "^3.1.5",
Expand Down
11 changes: 2 additions & 9 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bfj = require('bfj');
const path = require('path');
const mkdir = require('mkdirp');
const {bold} = require('chalk');

const Logger = require('./Logger');
const viewer = require('./viewer');
const utils = require('./utils');
const {writeStats} = require('./statsUtils');

class BundleAnalyzerPlugin {
constructor(opts = {}) {
Expand Down Expand Up @@ -83,14 +83,7 @@ class BundleAnalyzerPlugin {
mkdir.sync(path.dirname(statsFilepath));

try {
await bfj.write(statsFilepath, stats, {
space: 2,
promises: 'ignore',
buffers: 'ignore',
maps: 'ignore',
iterables: 'ignore',
circular: 'ignore'
});
await writeStats(stats, statsFilepath);

this.logger.info(
`${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}`
Expand Down
84 changes: 84 additions & 0 deletions src/statsUtils.js
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));
});
}

0 comments on commit e9bdf60

Please sign in to comment.