- Hands-On Full-Stack Web Development with GraphQL and React
- Sebastian Grebe
- 283字
- 2021-07-02 13:20:43
Analyzing bundle size
People that are trying to use as little bandwidth as possible will want to keep their bundle size low. I recommend that you always keep an eye on this, especially when requiring more modules via npm. In this case, you can quickly end up with a huge bundle size, since npm packages tend to require other npm packages themselves.
To protect us against this, we need a method to analyze the bundle size. Only the production build is worth checking. As previously mentioned, the development build includes React in a development release with source maps and so on.
Thanks to webpack, there is a simple solution for analyzing our bundle. This solution is called webpack-bundle-analyzer, and it does exactly what it sounds like.
Install this with the following:
npm install --save-dev webpack-bundle-analyzer
You then need to add two commands to the scripts object in the package.json:
- "stats": "webpack --profile --json --config webpack.client.build.config.js > stats.json"
- "analyze": "webpack-bundle-analyzer stats.json"
The first command creates a production build as well as a stats.json file in the root folder. This file holds the information we need.
The analyze command spins up the webpack-bundle-analyzer, showing us how our bundle is built together and how big each package that we use is.
Do this as follows:
npm run stats
npm run analyze
You can visually see our bundle and package sizes. Remove unnecessary packages in your projects and see how your bundle is reorganized. You can take an example from the following screenshot:

This diagram looks a lot like WinDirStat which is a software to display the disk usage of your computer. We can identify the packages that make up the majority of our bundle.
- 網(wǎng)絡(luò)云百問(wèn)百答
- 網(wǎng)管員典藏書(shū)架:網(wǎng)絡(luò)管理與運(yùn)維實(shí)戰(zhàn)寶典
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- OpenLayers Cookbook
- Drush User’s Guide
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- 基于性能的保障理論與方法
- 面向5G-Advanced的關(guān)鍵技術(shù)
- 網(wǎng)絡(luò)AI+:2030后的未來(lái)網(wǎng)絡(luò)
- 人人都該都懂的互聯(lián)網(wǎng)思維
- 數(shù)字王國(guó)里的虛擬人:技術(shù)、商業(yè)與法律解讀
- Python API Development Fundamentals
- 區(qū)塊鏈技術(shù)與應(yīng)用:打造分布式商業(yè)新生態(tài)
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見(jiàn)(2018)
- 賽博空間簡(jiǎn)史