Skip to content

Commit

Permalink
Generate stats.json for Webpack Analyser (http://webpack.github.io/an…
Browse files Browse the repository at this point in the history
…alyse/)

[closes #790]
  • Loading branch information
langpavel committed Feb 14, 2017
1 parent 7bbe94c commit 4bd3f3f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ database.sqlite
node_modules
ncp-debug.log
npm-debug.log
stats.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
"deploy": "babel-node tools/run deploy",
"render": "babel-node tools/run render",
"serve": "babel-node tools/run runServer",
"start": "babel-node tools/run start"
"start": "babel-node tools/run start",
"stats": "babel-node tools/run stats"
}
}
47 changes: 47 additions & 0 deletions tools/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* React Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2014-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import webpack from 'webpack';
import { writeFile } from 'fs';
import { open } from 'openurl';
import webpackConfig from './webpack.config';

const WEBPACK_ANALYSER_URL = 'http://webpack.github.io/analyse/';
/**
* Creates application stats for client bundle.
*/
function clientStats() {
return new Promise((resolve, reject) => {
const clientConfig = {
...webpackConfig[0],
profile: true,
};

webpack(clientConfig).run((err, stats) => {
if (err) {
reject(err);
return;
}

const statsJson = JSON.stringify(stats.toJson(), null, 2);
writeFile('stats.json', statsJson, 'utf-8', (saveErr) => {
if (saveErr) {
console.error(saveErr.message);
reject(saveErr);
return;
}
console.log(`Now you can load \`stats.json\` into page at ${WEBPACK_ANALYSER_URL}`);
open(WEBPACK_ANALYSER_URL);
resolve(stats);
});
});
});
}

export default clientStats;

0 comments on commit 4bd3f3f

Please sign in to comment.