Code Splitting with Lazy Loading
Code splitting divides your application into smaller chunks that load on demand, reducing the initial download size.
Route-Level Lazy Loading
export const routes: Routes = [
{ path: '', component: HomeComponent },
{
path: 'admin',
loadChildren: () =>
import('./admin/admin.routes').then(m => m.ADMIN_ROUTES),
},
{
path: 'reports',
loadComponent: () =>
import('./reports/reports.component').then(m => m.ReportsComponent),
},
];
Bundle Structure
dist/my-app/browser/
├── main.js # App shell + essential code
├── polyfills.js # Browser polyfills
├── runtime.js # Webpack runtime
├── styles.css # Global styles
├── admin-XXXX.js # Lazy-loaded admin chunk
├── reports-XXXX.js # Lazy-loaded reports chunk
└── assets/
Analyzing Bundle Size
# Build with stats JSON
ng build --stats-json
# Analyze with webpack-bundle-analyzer
npx webpack-bundle-analyzer dist/my-app/browser/stats.json
This produces an interactive treemap showing which modules contribute most to bundle size.
Common Bundle Size Issues
| Issue | Solution |
|---|---|
| Large third-party library | Lazy load or use lighter alternative |
| Duplicate dependencies | Extract to shared chunk |
| Unused imports | Tree shaking (automatic in production) |
| Large images | Optimize and use modern formats |
Vendor Chunk Extraction
Extracting shared vendor code into a separate chunk prevents duplication across lazy-loaded routes.
Default Vendor Chunk
Angular CLI automatically extracts vendor code:
{
"architect": {
"build": {
"options": {
"outputHashing": "all",
"namedChunks": true
}
}
}
}
Manual Chunk Configuration
{
"architect": {
"build": {
"options": {
"commonChunk": true,
"vendorChunk": true
}
}
}
}
Preloading Strategy
import { provideRouter, withPreloading } from '@angular/router';
import { PreloadAllModules } from '@angular/router';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes, withPreloading(PreloadAllModules)),
],
};
Verifying Code Splitting
# Build for production
ng build --configuration production
# Check the dist folder for separate chunks
dist/my-app/browser/
├── main.js
├── admin-XXXX.js
└── reports-XXXX.js
Each lazy-loaded route produces a separate chunk file.
Quiz
1. What does route-level lazy loading do?
2. How do you analyze Angular bundle composition?
3. What is the benefit of extracting vendor chunks?
Flashcards
Question
What does lazy loading do?
Click to reveal answer
Answer
Splits features into separate bundles loaded on demand.
Question
How to analyze bundles?
Click to reveal answer
Answer
ng build --stats-json + webpack-bundle-analyzer
Question
Benefit of vendor chunks?
Click to reveal answer
Answer
Prevents duplication, enables better caching.
Revision Notes
Key Takeaways
- 1. Route-level lazy loading splits features into separate chunks
- 2. webpack-bundle-analyzer visualizes bundle composition
- 3. Vendor chunk extraction prevents duplication
- 4. Preloading strategies warm critical chunks
Interview Tips
- • Explain code splitting and bundle structure
- • Be ready to describe bundle analysis tools
- • Mention vendor chunk extraction benefits
Cheat Sheet
Code Splitting Cheat Sheet
Lazy load routes:
loadChildren: () => import('./admin/admin.routes').then(m => m.ADMIN_ROUTES)
Analyze bundles:
ng build --stats-json
npx webpack-bundle-analyzer dist/my-app/browser/stats.json
Preload all:
provideRouter(routes, withPreloading(PreloadAllModules))