41

Angular CLI creates vendor.js and I don't know Why and What is the use of it?? Size of this file is about 3.2MB for a new app!!

Does this file contains Angular 6 Javascript Source?

Don't you think this is big file for loading on internet on low speed connections?

Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93

6 Answers6

41

This file includes all libraries that you added into your project. If you build your app on production mode the file size will be smaller.

ng build --prod
gunr2171
  • 16,104
  • 25
  • 61
  • 88
molikh
  • 1,274
  • 13
  • 24
35

Try

ng build --prod --aot --vendor-chunk --common-chunk --delete-output-path --buildOptimizer

I reduced my vender.**.js fromm 12mb to 2mb

DV Singh
  • 1,038
  • 11
  • 16
9

Instead of decreasing it you can remove the file completely By specifying the --build-optimizer flag, the cli will disable this file from the build output.

The CLI will now bundle the vendor code into the main.js bundle, which will also enable uglification to reduce the size.

So you will see a small increase in the size of the main.js bundle which is minimal in comparison to the size of the vendor chunks

Nanotron
  • 554
  • 5
  • 16
6

You may also want to update your build script in package.json to generate a prod build by default. I ran into this deploying to Heroku, since it runs 'npm build' automatically. By default, 'npm build' runs the following script:

ng build

If you update it to

ng build --prod

in package.json, then Heroku/AWS/Azure will create a production build on deployment instead.

Evan Kleiner
  • 329
  • 3
  • 12
5

At the angular.json configuration file you need to change the values:

 "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "aot": true,
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "namedChunks": false,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true
edyrkaj
  • 169
  • 3
  • 3
1

In case you are using C# as backend with the default template, make sure to actually use the production configuration from your angular.json:

...
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build-prod" />
...

Since I switched to a newer angular version, --prod was no longer supported and I incorrectly assumed that ng build would default to production... Furthermore I had to adapt the production configuration as described by @edyrkaj

You could change the package.json as well by adding all those options to the build command, but I prefer the angular.json approach since it's a lot nicer to read and maintain in my opinion

Thiemo
  • 107
  • 1
  • 8