diff --git a/.gitignore b/.gitignore index d00c2d9..35660fe 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ database.sqlite node_modules ncp-debug.log npm-debug.log +stats.json diff --git a/package.json b/package.json index 6938384..f0282b2 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/tools/stats.js b/tools/stats.js new file mode 100644 index 0000000..499e395 --- /dev/null +++ b/tools/stats.js @@ -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;