Preload modules before they're needed:
<link rel="modulepreload" href="/js/heavy-module.js">
<script type="module" src="/js/app.js"></script>
✅ Benefits: Faster load times, reduced waterfall effects
Remove unused code during bundling:
// utils.js - exports many functions
export function add(a, b) { return a + b; }
export function multiply(a, b) { return a * b; }
// main.js - only imports what's needed
import { add } from './utils.js';
// multiply() is removed from final bundle!
Requirements:
Load modules only when needed:
// Load heavy feature on user interaction
button.addEventListener('click', async () => {
const { init } = await import('./heavy-feature.js');
init();
});
ES Modules work great with HTTP/2's multiplexing:
Split code into vendor, app, and route chunks:
// vite.config.js
export default {
build: {
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
utils: ['lodash']
}
}
}
}
}
✅ Do
❌ Avoid
Barrel files (index.js re-exports) can silently destroy tree-shaking and inflate bundle sizes:
// utils/index.js - barrel file re-exports everything
export { formatDate } from './date.js';
export { formatCurrency } from './currency.js';
export { heavyChartLib } from './charts.js'; // 200KB!
// consumer.js - only needs formatDate, but...
import { formatDate } from './utils';
// Some bundlers may include ALL exports from the barrel!
Best Practices:
"sideEffects": false in package.jsonimport defer (Stage 3) to defer heavy barrel importsThe import defer proposal (TC39 Stage 3) provides a middle ground between eager static imports and lazy dynamic imports:
// Eager: executes immediately on page load
import { track } from './analytics.js';
// Deferred: loaded but NOT executed until accessed
import defer * as analytics from './analytics.js';
// Dynamic: not loaded at all until called
const analytics = await import('./analytics.js');
// Deferred is ideal for:
// - Modules needed synchronously but not at startup
// - Reducing main thread blocking during page load
// - Features that are likely but not guaranteed to be used