/tree-shake - Optimize Tree Shaking
Improve tree shaking effectiveness to reduce bundle size.
Steps
- Analyze the current build configuration for tree shaking settings
- Check package.json for sideEffects field configuration
- Identify imports that prevent tree shaking: namespace imports, CommonJS requires
- Find barrel files (index.ts re-exports) that bundle entire modules
- Check for packages that do not support tree shaking (no ES modules)
- Convert namespace imports to named imports where possible
- Add sideEffects: false to package.json if all modules are pure
- Configure the bundler to mark specific files as side-effect-free
- Replace non-tree-shakeable packages with tree-shakeable alternatives
- Split barrel files into direct imports for large modules
- Rebuild and measure the size difference
- Report: modules eliminated, size reduction, remaining issues
Rules
- Never mark files with side effects as side-effect-free
- Verify that tree shaking does not remove needed code
- Prefer named imports over namespace imports for better tree shaking
- Check that all internal packages have proper ESM exports
- Do not break the public API of packages when restructuring exports
- Test the application thoroughly after tree shaking changes
- Document any sideEffects configuration with comments explaining why