Optimizing Bundle Size in Angular
Optimizing bundle size is crucial for improving the performance and load times of your Angular applications. This guide covers various techniques to reduce the bundle size in Angular projects.
Enable Production Mode
Build your Angular application in production mode to enable optimizations like Ahead-of-Time (AOT) compilation, tree-shaking, and minification:
ng build --prod
Use Lazy Loading
Lazy loading helps to load feature modules only when they are needed, reducing the initial bundle size:
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) },
{ path: 'about', loadChildren: () => import('./about/about.module').then(m => m.AboutModule) }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Optimize Third-Party Libraries
Only import the parts of third-party libraries that you need to reduce the bundle size:
// Import specific functions from lodash
import { debounce } from 'lodash-es';
debounce(() => {
console.log('Debounced');
}, 500);
Use Angular CLI Budgets
Configure Angular CLI budgets to enforce size limits on your bundles:
// angular.json
{
...
"projects": {
"my-app": {
...
"architect": {
"build": {
...
"configurations": {
"production": {
...
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
}
}
}
}
}
Remove Unused Code
Analyze your bundle to identify and remove unused code. Use tools like source-map-explorer
:
npm install source-map-explorer
ng build --prod --source-map
npx source-map-explorer dist/my-app/main.*.js
Use Angular Ivy
Ensure you are using Angular Ivy, which provides smaller bundle sizes and faster compilation times:
// tsconfig.app.json
{
...
"angularCompilerOptions": {
"enableIvy": true
}
}
Enable Differential Loading
Angular supports differential loading, which creates separate bundles for modern and legacy browsers:
ng build --prod --output-hashing=all
Use Webpack Bundle Analyzer
Use Webpack Bundle Analyzer to visualize the size of your webpack output files with an interactive zoomable treemap:
npm install --save-dev webpack-bundle-analyzer
// webpack.config.js
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
module.exports = {
...
plugins: [
new BundleAnalyzerPlugin()
]
};
Optimize Images
Optimize images to reduce the overall size of your application:
npm install --save-dev imagemin imagemin-cli
imagemin src/assets/* --out-dir=dist/assets
Key Points
- Build your Angular application in production mode to enable optimizations.
- Use lazy loading to load feature modules only when needed.
- Import only the necessary parts of third-party libraries.
- Configure Angular CLI budgets to enforce size limits on your bundles.
- Analyze your bundle to identify and remove unused code.
- Ensure you are using Angular Ivy for smaller bundle sizes and faster compilation times.
- Enable differential loading to create separate bundles for modern and legacy browsers.
- Use Webpack Bundle Analyzer to visualize the size of your webpack output files.
- Optimize images to reduce the overall size of your application.
Conclusion
Optimizing the bundle size of your Angular application is crucial for improving performance and load times. By implementing techniques such as lazy loading, optimizing third-party libraries, using Angular CLI budgets, removing unused code, enabling Angular Ivy and differential loading, and optimizing images, you can significantly reduce your application's bundle size. Happy coding!