Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 - Main bundle file is too large on the development server

We are developing an application using Angular 5. After running the ng build --prod --aot and deployed the dist folder to the dev server the main bundle produced 2.2MB on initial load. It's already small compared to 8MB vendor bundle size (when running ng build --prod) but still, we need to lower the size of the main bundle. Is there any way to lower the size of the main bundle?

network

like image 731
Eugene Avatar asked Apr 06 '18 07:04

Eugene


People also ask

How to reduce the bundle size in Angular?

Bundle reduction tip #1: Use tree shaking Depending on the size of the library you're utilizing, you can drastically cut down on your bundle size. As a bonus, you don't need to do anything to do tree shaking in your Angular application – it's been a part of Angular Ivy since Angular 8.

How big should an Angular bundle be?

js under 500 KB, on average 250KB. If your bundle size largely exceed those numbers, you may need to be aware. If your bundle size is under this number, you may still want to optimize it further.

Why is my Angular project so large?

Angular is a very opinionated and robust framework. As a result, Angular apps generally have bigger bundle sizes when compared to apps written using other JavaScript frameworks like React. The Angular framework comes with a lot of useful libraries already included within it, such as RxJs and Zone.

Why is vendor JS so large?

Firstly, vendor bundles are huge simply because Angular 2 relies on a lot of libraries. Minimum size for Angular 2 app is around 500KB (250KB in some cases, see bottom post). Tree shaking is properly used by angular-cli .


1 Answers

You can reduce the bundle size by adding build-optimizer flag to your build:

ng build --prod --build-optimizer

Try running the above command. For more info visit: https://angular.io/guide/deployment

AFAIK you need not have to include --aot flag when building using --prod. Production build is aot by default.

Updating to the latest version of angular is also a solution for this. Since latest version often comes with more optimized build procedures.

like image 176
Gautam Krishna R Avatar answered Sep 20 '22 04:09

Gautam Krishna R