From b24074488d507d0110b91798e6f423e298cd0da2 Mon Sep 17 00:00:00 2001 From: Pavel Lang Date: Thu, 16 Feb 2017 03:31:08 +0100 Subject: [PATCH 1/3] Revert "Show message if stats will be generated for dev" This reverts commit 097c1a3bcbd8731e3fa9817dcdf6fcc7fe27620e. --- tools/stats.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tools/stats.js b/tools/stats.js index 859e15177..499e395d5 100644 --- a/tools/stats.js +++ b/tools/stats.js @@ -12,13 +12,7 @@ import { writeFile } from 'fs'; import { open } from 'openurl'; import webpackConfig from './webpack.config'; -const isDebug = !process.argv.includes('--release'); const WEBPACK_ANALYSER_URL = 'http://webpack.github.io/analyse/'; - -if (isDebug) { - console.log('INFO: You should run `yarn stats -- --release` if you want production stats'); -} - /** * Creates application stats for client bundle. */ From 2cb08b9cf8702d6cf1893471478ed6d72f250ba5 Mon Sep 17 00:00:00 2001 From: Pavel Lang Date: Thu, 16 Feb 2017 03:32:04 +0100 Subject: [PATCH 2/3] Revert "Generate stats.json for Webpack Analyser (http://webpack.github.io/analyse/)" This reverts commit 4bd3f3fdaf067842c49af1ce5b2e3188066e7d17. --- .gitignore | 1 - package.json | 3 +-- tools/stats.js | 47 ----------------------------------------------- 3 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 tools/stats.js diff --git a/.gitignore b/.gitignore index 35660fefe..d00c2d975 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,3 @@ database.sqlite node_modules ncp-debug.log npm-debug.log -stats.json diff --git a/package.json b/package.json index f0282b2a9..6938384ed 100644 --- a/package.json +++ b/package.json @@ -208,7 +208,6 @@ "deploy": "babel-node tools/run deploy", "render": "babel-node tools/run render", "serve": "babel-node tools/run runServer", - "start": "babel-node tools/run start", - "stats": "babel-node tools/run stats" + "start": "babel-node tools/run start" } } diff --git a/tools/stats.js b/tools/stats.js deleted file mode 100644 index 499e395d5..000000000 --- a/tools/stats.js +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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; From b26c3bcd19cf18ba4327cf0529588540b0653094 Mon Sep 17 00:00:00 2001 From: Pavel Lang Date: Thu, 16 Feb 2017 03:38:50 +0100 Subject: [PATCH 3/3] Webpack-bundle-analyzer --- .gitignore | 2 ++ package.json | 2 ++ tools/webpack.config.js | 41 +++++++++++++++++++++++++++++++++++++++++ yarn.lock | 37 +++++++++++++++++++++++++++++++++++-- 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d00c2d975..fa0612ce5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ database.sqlite node_modules ncp-debug.log npm-debug.log +stats.json +report.html diff --git a/package.json b/package.json index 6938384ed..2d6dcae03 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ "stylelint-config-standard": "^16.0.0", "url-loader": "^0.5.7", "webpack": "^2.2.1", + "webpack-bundle-analyzer": "^2.3.0", "webpack-dev-middleware": "^1.10.0", "webpack-hot-middleware": "^2.16.1", "write-file-webpack-plugin": "^3.4.2" @@ -205,6 +206,7 @@ "copy": "babel-node tools/run copy", "bundle": "babel-node tools/run bundle", "build": "babel-node tools/run build", + "build:stats": "yarn run build -- --release --analyse", "deploy": "babel-node tools/run deploy", "render": "babel-node tools/run render", "serve": "babel-node tools/run runServer", diff --git a/tools/webpack.config.js b/tools/webpack.config.js index 01572ab76..c438a989d 100644 --- a/tools/webpack.config.js +++ b/tools/webpack.config.js @@ -10,10 +10,26 @@ import path from 'path'; import webpack from 'webpack'; import AssetsPlugin from 'assets-webpack-plugin'; +import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; import pkg from '../package.json'; const isDebug = !process.argv.includes('--release'); const isVerbose = process.argv.includes('--verbose'); +const isAnalyse = process.argv.includes('--analyse') || process.argv.includes('--analyze'); +const port = parseInt(process.env.PORT || '3000', 10); +const analyzerPort = port + 3; + +// Can be `server`, `static` or `disabled`. +// In `server` mode analyzer will start HTTP server to show bundle report. +// In `static` mode single HTML file with bundle report will be generated. +// In `disabled` mode you can use this plugin to just generate Webpack Stats JSON +// file by setting `generateStatsFile` to `true`. +let analyzerMode = 'disabled'; +if (isAnalyse) { + analyzerMode = 'server'; +} else if (!isDebug) { + analyzerMode = 'static'; +} // // Common configuration chunk to be used for both @@ -216,6 +232,31 @@ const clientConfig = { }, }), ], + + new BundleAnalyzerPlugin({ + // See above + analyzerMode, + // Host that will be used in `server` mode to start HTTP server. + analyzerHost: '127.0.0.1', + // Port that will be used in `server` mode to start HTTP server. + analyzerPort, + // Path to bundle report file that will be generated in `static` mode. + // Relative to bundles output directory. + reportFilename: path.resolve(__dirname, '../report.html'), + // Automatically open report in default browser + openAnalyzer: true, + // If `true`, Webpack Stats JSON file will be generated in bundles output directory + generateStatsFile: !isDebug, + // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`. + // Relative to bundles output directory. + statsFilename: path.resolve(__dirname, '../stats.json'), + // Options for `stats.toJson()` method. + // You can exclude sources of your modules from stats file with `source: false` option. + // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21 + statsOptions: null, + // Log level. Can be 'info', 'warn', 'error' or 'silent'. + logLevel: 'info', + }), ], // Choose a developer tool to enhance debugging diff --git a/yarn.lock b/yarn.lock index 944ef011f..0b1fae94c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -46,6 +46,10 @@ acorn@^3.0.4, acorn@^3.1.0: version "3.3.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a" +acorn@^4.0.11: + version "4.0.11" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.11.tgz#edcda3bd937e7556410d42ed5860f67399c794c0" + acorn@^4.0.3, acorn@^4.0.4: version "4.0.10" resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.10.tgz#598ed8bdd4de8b5a7a7fa2f6d2188ebbf9b1f12c" @@ -2110,7 +2114,7 @@ duplexer2@0.0.2: dependencies: readable-stream "~1.1.9" -duplexer@~0.1.1: +duplexer@^0.1.1, duplexer@~0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1" @@ -2174,6 +2178,10 @@ ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" +ejs@^2.5.5: + version "2.5.5" + resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.5.5.tgz#6ef4e954ea7dcf54f66aad2fe7aa421932d9ed77" + electron-to-chromium@^1.2.1: version "1.2.2" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.2.2.tgz#e41bc9488c88e3cfa1e94bde28e8420d7d47c47c" @@ -2714,7 +2722,7 @@ filename-regex@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.0.tgz#996e3e80479b98b9897f15a8a58b3d084e926775" -filesize@^3.2.1: +filesize@^3.2.1, filesize@^3.5.4: version "3.5.4" resolved "https://registry.yarnpkg.com/filesize/-/filesize-3.5.4.tgz#742fc7fb6aef4ee3878682600c22f840731e1fda" @@ -3050,6 +3058,12 @@ growl@1.9.2: version "1.9.2" resolved "https://registry.yarnpkg.com/growl/-/growl-1.9.2.tgz#0ea7743715db8d8de2c5ede1775e1b45ac85c02f" +gzip-size@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-3.0.0.tgz#546188e9bdc337f673772f81660464b389dce520" + dependencies: + duplexer "^0.1.1" + handlebars@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-1.3.0.tgz#9e9b130a93e389491322d975cf3ec1818c37ce34" @@ -4557,6 +4571,10 @@ onetime@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/onetime/-/onetime-1.1.0.tgz#a1f7838f8314c516f05ecefcbc4ccfe04b4ed789" +opener@^1.4.2: + version "1.4.3" + resolved "https://registry.yarnpkg.com/opener/-/opener-1.4.3.tgz#5c6da2c5d7e5831e8ffa3964950f8d6674ac90b8" + openurl@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/openurl/-/openurl-1.1.0.tgz#e2f2189d999c04823201f083f0f1a7cd8903187a" @@ -6718,6 +6736,21 @@ watchpack@^1.2.0: chokidar "^1.4.3" graceful-fs "^4.1.2" +webpack-bundle-analyzer@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/webpack-bundle-analyzer/-/webpack-bundle-analyzer-2.3.0.tgz#0d05e96a43033f7cc57f6855b725782ba61e93a4" + dependencies: + acorn "^4.0.11" + chalk "^1.1.3" + commander "^2.9.0" + ejs "^2.5.5" + express "^4.14.1" + filesize "^3.5.4" + gzip-size "^3.0.0" + lodash "^4.17.4" + mkdirp "^0.5.1" + opener "^1.4.2" + webpack-dev-middleware@^1.10.0: version "1.10.0" resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-1.10.0.tgz#7d5be2651e692fddfafd8aaed177c16ff51f0eb8"