Analyze and reduce JavaScript bundle size
✓Works with OpenClaudeYou are a JavaScript performance engineer. The user wants to analyze their webpack or bundler output to identify large modules, unused code, and optimization opportunities to reduce bundle size.
What to check first
- Run
npm list webpack webpack-bundle-analyzerto confirm bundle analyzer is installed - Check your
package.jsonscripts to see if a build command exists (e.g.,"build": "webpack") - Verify your webpack config file exists at
webpack.config.jsor in the build tools configuration
Steps
- Install
webpack-bundle-analyzeras a dev dependency:npm install --save-dev webpack-bundle-analyzer - Import the plugin at the top of your webpack config:
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; - Add the plugin to the
pluginsarray in your webpack config withnew BundleAnalyzerPlugin({ analyzerMode: 'static' }) - Run your build command (e.g.,
npm run build) to generate the analysis HTML report - Open the generated
dist/report.htmlin your browser to visualize module sizes as a treemap - Identify the largest modules—look for unexpectedly large libraries or duplicate dependencies
- Use the search feature in the report to find specific packages and their actual gzip size vs parsed size
- Cross-reference large modules with your source code to find optimization targets like lazy loading or code splitting
Code
// webpack.config.js
const path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
],
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: 10,
},
},
},
},
plugins: [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'report.html',
generateStatsFile: true,
statsFilename: 'stats.json',
}),
],
};
Pitfalls
- Gzip vs parsed size confusion: The report shows both—gzip size is what users download, but parsed size impacts runtime. Focus on gzip for download optimization.
- **Duplicate dependencies in node_
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Performance Skills
Other Claude Code skills in the same category — free to download.
Image Optimization
Optimize images for web (format, size, lazy load)
Code Splitting
Implement code splitting and dynamic imports
Caching Strategy
Design and implement caching strategy
Database Query Perf
Optimize database query performance
Render Optimizer
Optimize React re-renders and component performance
Lighthouse Fixer
Fix issues found in Lighthouse audit
Prefetch Setup
Set up resource prefetching and preloading
Tree Shaking Fix
Fix tree-shaking issues and reduce dead code
Want a Performance skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.