Bundle Size Analysis
This document explains the bundle size analysis implementation in the Cypress Dashboard project.
Overview
The bundle size analysis feature automatically monitors the size of the client application bundle and ensures that changes don't significantly increase the bundle size without proper justification.
Features
1. Automated CI/CD Analysis
- Trigger: Runs on every pull request that modifies client code
- Comparison: Compares current PR bundle size with main branch
- Thresholds:
- Maximum size increase: 512KB
- Maximum percentage increase: 10%
- Failure: PR fails if thresholds are exceeded
2. Local Development Tools
- Bundle analysis script:
npm run bundle-size
- Interactive visualization:
npm run build:analyze
- Detailed reporting: File-by-file breakdown with optimization suggestions
Implementation Details
GitHub Actions Workflow
The bundle size analysis is implemented in .github/workflows/pr-validation.yml
with three main steps:
- Bundle size analysis: Builds the project and calculates current bundle sizes
- Compare with main branch: Fetches main branch, builds it, and compares sizes
- Bundle size validation: Applies thresholds and fails if exceeded
Local Scripts
client/scripts/analyze-bundle.js
: Node.js script for local bundle analysisclient/vite.config.ts
: Vite configuration with rollup-plugin-visualizerclient/package.json
: Scripts for bundle analysis commands
Usage
For Developers
Local Analysis
# Quick bundle size summary
cd client
npm run bundle-size
# Detailed interactive analysis
npm run build:analyze
# Opens dist/bundle-analysis.html in browser
CI/CD Integration
The analysis runs automatically on pull requests. If your PR fails due to bundle size:
- Review the failure message for specific size increases
- Consider optimization strategies:
- Code splitting with dynamic imports
- Lazy loading of components
- Tree shaking unused dependencies
- Analyzing large dependencies
- Run local analysis to identify large files
- Implement optimizations and test locally
For Reviewers
When reviewing PRs, check the bundle size analysis output:
- ✅ Green: Bundle size decreased or stayed the same
- ⚠️ Yellow: Bundle size increased but within limits
- ❌ Red: Bundle size increased beyond acceptable thresholds
Configuration
Thresholds
Current thresholds can be modified in .github/workflows/pr-validation.yml
:
MAX_SIZE_INCREASE=524288 # 512KB in bytes
MAX_PERCENT_INCREASE=10 # 10% increase
Bundle Analyzer
The interactive bundle analyzer is configured in client/vite.config.ts
:
visualizer({
filename: 'dist/bundle-analysis.html',
open: true,
gzipSize: true,
brotliSize: true,
})
Optimization Strategies
Code Splitting
// Use dynamic imports for large components
const LazyComponent = lazy(() => import('./LargeComponent'));
// Route-based code splitting
const Dashboard = lazy(() => import('./pages/Dashboard'));
Tree Shaking
// Import only what you need
import { debounce } from 'lodash-es';
// Instead of: import _ from 'lodash';
Bundle Analysis
# Identify large dependencies
npm run build:analyze
# Check what's included in your bundle
npm run bundle-size
Troubleshooting
Common Issues
Bundle size increased unexpectedly:
- Check if new dependencies were added
- Verify tree shaking is working correctly
- Look for duplicate dependencies
- Consider if the increase is justified
Analysis script fails:
- Ensure the project builds successfully first
- Check that
dist/
directory exists - Verify Node.js version compatibility
CI/CD analysis fails:
- Check GitHub Actions logs for specific errors
- Verify the main branch builds successfully
- Ensure all dependencies are properly locked
Future Enhancements
Potential improvements to consider:
- Bundle size tracking over time
- Performance budget integration
- Automated optimization suggestions
- Integration with performance monitoring tools
- Bundle composition change detection