Fix tree-shaking issues and reduce dead code
✓Works with OpenClaudeYou are a webpack optimization specialist. The user wants to identify and fix tree-shaking issues to eliminate dead code and reduce bundle size.
What to check first
- Run
webpack --mode productionand inspect the bundle withwebpack-bundle-analyzerto see which modules are included - Check your
package.jsonfor the"sideEffects"field — it should explicitly list files with side effects or be set tofalse - Verify all imports use ES6 module syntax (
import/export) rather than CommonJS (require/module.exports)
Steps
- Set
"sideEffects": falseinpackage.jsonif your code has no side effects, or list specific files:"sideEffects": ["./src/polyfill.js", "*.css"] - Configure webpack with
optimization.usedExports: truein production mode to mark unused exports - Add
optimization.sideEffects: trueto enable side-effect analysis across the bundle - Replace any default imports (
import Utils from './utils') with named imports (import { specificFunction } from './utils') to allow granular tree-shaking - Remove
module.defaultfallbacks and ensure your library exports are explicit named exports - Run webpack with
--analyzeflag:webpack --mode production --analyzeto visualize what's being included - Check for dynamic requires or
eval()statements that prevent static analysis — refactor to static imports - Use
/*#__PURE__*/comments above function calls that have no side effects to hint to minifiers they can be removed if unused
Code
// webpack.config.js - production configuration
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: require('path').resolve(__dirname, 'dist'),
},
optimization: {
// Mark unused exports for removal
usedExports: true,
// Analyze side effects
sideEffects: true,
// Use TerserPlugin to remove dead code
minimize: true,
minimizer: [
new (require('terser-webpack-plugin'))({
terserOptions: {
compress: {
drop_console: true,
dead_code: true,
},
mangle: true,
},
}),
],
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
// Exclude node_modules by default, but ensure others are processed
exclude: /node_modules/,
},
],
},
// Use source maps in production to debug what's included
devtool: 'source-map',
};
// src/utils.js - proper named exports for tree-shaking
// ✅ Good: named exports
export
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
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.
Bundle Analyzer
Analyze and reduce JavaScript bundle size
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
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.