'How to decrease size of vendor.js in angular 2,4,6,7,8,9,10?
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?
Solution 1:[1]
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
Solution 2:[2]
Try
ng build --prod --aot --vendor-chunk --common-chunk --delete-output-path --buildOptimizer
I reduced my vender.**.js fromm 12mb to 2mb
Solution 3:[3]
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
Solution 4:[4]
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.
Solution 5:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gunr2171 |
Solution 2 | DV Singh |
Solution 3 | |
Solution 4 | Evan Kleiner |
Solution 5 | edyrkaj |