How to decrease size of vendor.js in angular 2,4,6,7,8,9,10,11,12,13,14,15,16?
Asked Answered
T

6

44

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?

Tahmosh answered 10/7, 2018 at 5:38 Comment(0)
D
42

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
Dysteleology answered 10/7, 2018 at 5:45 Comment(2)
After using Prod also my project size is 8Mb which getting more time to load. May i know what to do ??Jennings
you mean files inside dist folder? you can check in that folder which file cause this.Dysteleology
F
36

Try

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

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

Forwarder answered 6/12, 2019 at 8:19 Comment(4)
In Angular 9 , I can't find the vendor-chunk anywhere after running ng build --prodAutocracy
check here: #52180353Forwarder
Didn't change antyhing for meWestberg
Nice. 8mb to 880KbsShoemake
D
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

Denysedenzil answered 13/3, 2019 at 12:44 Comment(3)
I didn't give any difference in our buildDesimone
No difference for me and vendor remainsWestberg
@Desimone then check the version doc. Its automatically done in more recent versions of angularDenysedenzil
B
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.

Biquadrate answered 27/3, 2019 at 19:13 Comment(0)
K
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
Kyat answered 29/4, 2022 at 23:13 Comment(0)
F
2

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

Frederic answered 7/3, 2023 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.