When building modern frontend apps, performance is non-negotiable. And one of the biggest culprits of slow load times? Oversized JavaScript bundles.
Enter webpack-bundle-analyzer
β a tool that helps you understand, visualize, and reduce the weight of your bundles.
π§ What is webpack-bundle-analyzer
?
It's a Webpack plugin that generates an interactive treemap visualization of your bundle content. It shows you which packages, files, and modules are taking up the most space β often revealing surprises.
π¦ Installation
If you're using npm:
npm install --save-dev webpack-bundle-analyzer
Or with Yarn:
yarn add --dev webpack-bundle-analyzer
βοΈ Basic setup
In your webpack.config.js
, import and add the plugin:
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
module.exports = {
plugins: [
new BundleAnalyzerPlugin()
]
};
By default, this will start a local server on build and open the report in your browser.
πΌοΈ What you'll see
You'll get an interactive treemap that shows:
- Each file inside your bundles
- Relative size of each module
- Which third-party libraries dominate your build
You can hover over any section to see exact byte size.
π Common findings
Some common surprises when running the analyzer:
- β
Accidentally importing all of
lodash
instead of specific functions - β
Including large date libraries like
moment.js
- β Shipping unused dependencies
- β Duplicates from multiple versions of the same package
π§ Optional configuration
You can customize how the analyzer runs:
new BundleAnalyzerPlugin({
analyzerMode: 'static', // Generates a report.html file
openAnalyzer: false, // Doesnβt open automatically
reportFilename: 'bundle-report.html',
});
Other modes:
server
(default) β opens in browserstatic
β generates an HTML filedisabled
β disables it completely
π§Ή How to reduce bundle size (once you analyze)
- Tree-shake libraries (e.g.,
lodash-es
instead oflodash
) - Use dynamic imports to lazy-load code
- Split vendor and app code with
SplitChunksPlugin
- Remove unused code and dependencies
- Replace heavy libraries (e.g.,
moment.js
βdayjs
)
β Conclusion
webpack-bundle-analyzer
is a must-have tool for frontend developers who care about performance. It gives you a clear visual understanding of what you're shipping to users β and how to make it smaller.
Every time you install a new dependency or refactor large components, use this tool to validate your bundle health. Itβs simple, powerful, and will save your users seconds.