Để tối ưu hiệu suất build của Webpack 5 trong dự án Angular, bạn có thể áp dụng các phương pháp sau:
1. Sử dụng chế độ sản xuất (Production Mode)
Webpack cung cấp chế độ sản xuất (mode: 'production') để tự động kích hoạt các tối ưu hóa như minification, tree-shaking và code splitting. Điều này giúp giảm kích thước bundle và tăng tốc độ tải trang.
module.exports = {
mode: 'production',
};
2. Tree Shaking
Tree Shaking giúp loại bỏ các đoạn mã không sử dụng trong ứng dụng. Đảm bảo rằng bạn sử dụng cú pháp ES6 module (import/export) để Webpack có thể thực hiện tree-shaking hiệu quả.
3. Code Splitting
Chia nhỏ ứng dụng thành các bundle nhỏ hơn bằng cách sử dụng tính năng SplitChunksPlugin. Điều này giúp tải các phần của ứng dụng theo yêu cầu, giảm thời gian tải ban đầu.
module.exports = {
optimization: {
splitChunks: {
chunks: 'all',
},
},
};
4. Lazy Loading trong Angular
Sử dụng Lazy Loading để tải các module chỉ khi cần thiết. Trong Angular, bạn có thể cấu hình lazy loading trong app-routing.module.ts:
const routes: Routes = [
{ path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) },
];
5. Sử dụng DllPlugin và DllReferencePlugin
DllPlugin giúp tách các thư viện ít thay đổi (như Angular core) thành một bundle riêng biệt. Điều này giảm thời gian build lại ứng dụng.
const webpack = require('webpack');
module.exports = {
entry: {
vendor: ['@angular/core', '@angular/common'],
},
plugins: [
new webpack.DllPlugin({
name: '[name]_lib',
path: 'dist/[name]-manifest.json',
}),
],
};
6. Bật Cache
Sử dụng tùy chọn cache để lưu trữ kết quả build, giúp tăng tốc độ build trong các lần tiếp theo.
module.exports = {
cache: {
type: 'filesystem',
},
};
7. Sử dụng thread-loader
thread-loader giúp xử lý các loader nặng (như babel-loader, ts-loader) trong các worker thread, tăng tốc độ build.
module.exports = {
module: {
rules: [
{
test: /\.ts$/,
use: [
'thread-loader',
'ts-loader',
],
},
],
},
};
8. Tối ưu hóa hình ảnh
Sử dụng các plugin như image-webpack-loader để nén và tối ưu hóa hình ảnh.
module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'image-webpack-loader',
options: {
mozjpeg: { progressive: true },
optipng: { enabled: true },
pngquant: { quality: [0.65, 0.90], speed: 4 },
},
},
],
},
],
},
};
9. Giảm kích thước bundle
- Minification: Sử dụng
TerserPlugin để nén JavaScript.
- OptimizeCSSAssetsPlugin: Nén CSS.
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('css-minimizer-webpack-plugin');
module.exports = {
optimization: {
minimize: true,
minimizer: [
new TerserPlugin(),
new OptimizeCSSAssetsPlugin(),
],
},
};
10. Ahead-of-Time Compilation (AoT)
Sử dụng AoT trong Angular để biên dịch trước các template HTML và CSS, giúp giảm kích thước bundle và tăng tốc độ runtime.
ng build --prod --aot
11. Prefetching và Preloading
Sử dụng PreloadAllModules trong Angular để tải trước các module cần thiết.
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule],
})
export class AppRoutingModule {}
12. Sử dụng Service Worker
Angular hỗ trợ Service Worker để cache các tài nguyên tĩnh, giúp cải thiện hiệu suất tải trang.
ng add @angular/pwa
13. Giảm số lượng loader
Chỉ áp dụng loader cho các file cần thiết để giảm thời gian xử lý.
module.exports = {
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
],
},
};
14. Tắt Source Map trong Production
Source map giúp debug nhưng làm tăng kích thước bundle. Tắt nó trong môi trường production.
module.exports = {
devtool: false,
};
Bằng cách áp dụng các phương pháp trên, bạn có thể tối ưu hóa hiệu suất build của Webpack 5 trong dự án Angular, giúp giảm thời gian build và cải thiện trải nghiệm người dùng.