Skip to content

Commit

Permalink
Use JSONStream and createWriteStream() to output large json stats obj…
Browse files Browse the repository at this point in the history
…ects

with Better tests
  • Loading branch information
Ryan Albrecht authored and ryan953 committed Nov 2, 2017
1 parent e6fbbe7 commit 972ee30
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"views"
],
"dependencies": {
"JSONStream": "^1.3.1",
"acorn": "^5.1.1",
"chalk": "^1.1.3",
"commander": "^2.9.0",
Expand Down
20 changes: 11 additions & 9 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const fs = require('fs');
const path = require('path');
const mkdir = require('mkdirp');
const { bold } = require('chalk');

const { streamObjectToJson } = require('./fileUtils');
const Logger = require('./Logger');
const viewer = require('./viewer');

Expand Down Expand Up @@ -61,15 +60,18 @@ class BundleAnalyzerPlugin {
});
}

generateStatsFile(stats) {
async generateStatsFile(stats) {
const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);

mkdir.sync(path.dirname(statsFilepath));

fs.writeFileSync(
statsFilepath,
JSON.stringify(stats, null, 2)
);
try {
await streamObjectToJson(statsFilepath, stats);
} catch (err) {
this.logger.error(
`${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}.`,
JSON.stringify(err, null, "\n") // eslint-disable-line quotes
);
return;
}

this.logger.info(
`${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}`
Expand Down
29 changes: 29 additions & 0 deletions src/fileUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const path = require('path');
const mkdir = require('mkdirp');
const JSONStream = require('JSONStream');

module.exports = {
streamObjectToJson
};

async function streamObjectToJson(filepath, data) {
await new Promise((resolve, reject) => {
mkdir.sync(path.dirname(filepath));

const transformStream = JSONStream.stringifyObject();
const writableStream = fs.createWriteStream(filepath);
transformStream.pipe(writableStream);
transformStream.on('error', (err) => {
reject(err);
});
transformStream.on('end', () => {
writableStream.end();
resolve();
});
Object.keys(data).forEach((key) => {
transformStream.write([key, data[key]]);
});
transformStream.end();
});
}
Empty file added test/BundleAnalyzerPlugin.js
Empty file.
45 changes: 45 additions & 0 deletions test/fileUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const fs = require('fs');
const del = require('del');
const path = require('path');

const { streamObjectToJson } = require('../lib/fileUtils');

const OUTPUT_DIR = `${__dirname}/output`;

describe('fileUtils', function () {
before(function () {
del.sync(`${__dirname}/output`);
});

afterEach(function () {
del.sync(`${__dirname}/output`);
});

it('should print objects to files', async function () {
const statsFilename = path.resolve(OUTPUT_DIR, 'stats.json');
await streamObjectToJson(statsFilename, {
string: 'webpack-bundle-analyzer',
list: ['foo', 'bar', 'baz'],
obj: {
name: 'webpack-bundle-analyzer',
foo: 'bar',
baz: null
}
});

await setImmediate(() => {
expect(fs.existsSync(statsFilename)).to.be.true;

const fileContent = fs.readFileSync(statsFilename, 'utf8');
expect(JSON.parse(fileContent)).to.deep.equal({
string: 'webpack-bundle-analyzer',
list: ['foo', 'bar', 'baz'],
obj: {
name: 'webpack-bundle-analyzer',
foo: 'bar',
baz: null
}
});
});
});
});
13 changes: 12 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
# yarn lockfile v1


JSONStream@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.1.tgz#707f761e01dae9e16f1bcf93703b78c70966579a"
dependencies:
jsonparse "^1.2.0"
through ">=2.2.7 <3"

abbrev@1:
version "1.1.0"
resolved "https://registry.npmjs.org/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f"
Expand Down Expand Up @@ -2871,6 +2878,10 @@ jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"

jsonparse@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"

jsonpointer@^4.0.0:
version "4.0.1"
resolved "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz#4fd92cb34e0e9db3c89c8622ecf51f9b978c6cb9"
Expand Down Expand Up @@ -4840,7 +4851,7 @@ through2@~0.2.3:
readable-stream "~1.1.9"
xtend "~2.1.1"

through@^2.3.6:
"through@>=2.2.7 <3", through@^2.3.6:
version "2.3.8"
resolved "https://registry.npmjs.org/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"

Expand Down

0 comments on commit 972ee30

Please sign in to comment.