Bundle Size Optimization
Reducing bundle size improves load times, reduces bandwidth usage, and enhances user experience. This guide covers real techniques for optimizing bundle size in Meteor applications.Bundle Visualization
Thebundle-visualizer package provides visual representation of what’s included in the initial client bundle.
Usage
Run with production flag to enable minification:http://localhost:3000/ to see the bundle visualization chart.
What It Shows
- Files and packages in the initial client bundle
- Space occupied by each dependency
- Candidates for dynamic
import()conversion - Inadvertently included packages
Requirements
- Uses
<hash>.stats.jsonfiles from production builds - Requires
standard-minifier-jsor compatible minifier - Must run with
--productionflag
--extra-packages. Do not deploy to production with this package.
Tree Shaking with Rspack
Rspack enables tree shaking to eliminate dead code:Requirements for Tree Shaking
- Use ES modules (import/export)
- Avoid CommonJS where possible
- Enable production mode for minification
Code Splitting
Dynamic Imports
Convert static imports to dynamicimport() for code splitting:
Before:
Split Vendor Chunks
Prevent duplicating libraries across async chunks:node_modules into a stable vendor chunk.
Advanced Split Chunks
For more control, use Rspack’s split chunks configuration:Externalize SWC Helpers
SWC inlines transformation helpers in every file by default, leading to code duplication.Install @swc/helpers
Transform Imports
Rewrite imports to avoid full-package imports:Install SWC Plugin
Configure .swcrc
Delegating Dependencies
Compile Modern Packages with Rspack
Force Rspack to handle modern npm dependencies:Externalize Native Modules
Exclude native/binary code from the bundle:CSS Optimization
Use CSS Loaders
Process CSS with Rspack for automatic optimization:- Minifies CSS in production
- Removes unused styles
- Generates optimized CSS bundles
Less/SCSS Optimization
Replace Meteor Less/SCSS packages with Rspack loaders: Less:Minification
Enable SWC Minifier
Use the faster SWC minifier instead of Terser:- Faster production builds
- Better compression
- Modern optimization techniques
Production-Only Features
Exclude Development Dependencies
Ensure development packages aren’t included in production:Environment-Based Code
Rspack’s DefinePlugin eliminates dead code:Exclude Legacy Builds
For modern browsers only, exclude legacy builds:Add to .meteor/platforms
Package Audit
Identify Large Packages
Use bundle-visualizer to find large dependencies:- Look for unexpectedly large packages
- Check if they’re actually used
- Find lighter alternatives
- Convert to dynamic imports if possible
Common Culprits
- Moment.js - Consider
date-fnsordayjs - Lodash - Use
lodash-esor specific imports - Full icon libraries - Import only needed icons
- Large UI frameworks - Use tree-shakeable alternatives
Best Practices
- Visualize regularly - Run bundle-visualizer periodically
- Use dynamic imports - Lazy load non-critical code
- Enable tree shaking - Use Rspack with ES modules
- Split vendor chunks - Separate stable dependencies
- Transform imports - Avoid full-package imports
- Externalize helpers - Install @swc/helpers
- Optimize CSS - Use Rspack loaders for styles
- Audit dependencies - Remove unused packages
- Modern-only builds - Skip legacy if possible
- Measure impact - Use bundle-visualizer to verify changes