Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI - why does <ng serve> bundle better than <ng build>?

Performing a number of Angular CLI commands in order to build the minified basic project:

ng new project
cd project
ng build --prod

... as a result, I get the generated dist folder with a number of bundles. The largest one is called vendor.[hash].js and it's size about 855kB.

However, if I perform ng serve --prod instead:

ng new project
cd project
ng serve --prod

... I can see the loaded by localhost:4200 vendor's bundle weight is about 300kB using Chrome console.

Why is that happens? Is there a way to achieve the second result without ng serve but rather with ng build?

like image 637
WildDev Avatar asked Dec 31 '16 11:12

WildDev


2 Answers

As was discovered through the comments, you simply compared two different values: the size of the non-gzipped file in the file system, and the size of the gzipped download in the browser console.

like image 115
JB Nizet Avatar answered Oct 17 '22 10:10

JB Nizet


It's not about which one is better.

It's all about when to use build or serve.

  1. build is used for deployment purpose and serve is used for development purpose.
  2. build generates compiled output to /dist directory and serve build artifacts from memory for a faster development experience.

Ref : build vs. serve

like image 42
Vaibhav Avatar answered Oct 17 '22 09:10

Vaibhav