I have a simple app, initialized by angular-cli
.
It display some pages relative to 3 routes. I have 3 components. On one of this page I use lodash
Another way to reduce bundle, is to serve GZIP instead of JS. We went from 2.6mb to 543ko.
https://httpd.apache.org/docs/2.4/mod/mod_deflate.html
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
.
Do not include .map
files, because used only for debugging. Moreover, if you use hot replacement module, remove it to lighten vendor.
To pack for production, I personnaly use Webpack (and angular-cli relies on it too), because you can really configure everything
for optimization or debugging.
If you want to use Webpack
, I agree it is a bit tricky a first view, but see tutorials on the net, you won't be disappointed.
Else, use angular-cli
, which get the job done really well.
Using Ahead-of-time compilation is mandatory to optimize apps, and shrink Angular 2 app to 250KB.
Here is a repo I created (github.com/JCornat/min-angular) to test minimal Angular bundle size, and I obtain 384kB. I am sure there is easy way to optimize it.
Talking about big apps, using the AngularClass/angular-starter configuration, the same as in the repo above, my bundle size for big apps (150+ components) went from 8MB (4MB without map files) to 580kB.
If you are using Angular 8+ and you want to reduce the size of the bundle you can use Ivy. Ivy comes as the default view engine in Angular 9 Just go to src/tsconfig.app.json and add the angularCompilerOptions parameter, for example:
{
"extends": ...,
"compilerOptions":...,
"exclude": ...,
/* add this one */
"angularCompilerOptions": {
"enableIvy": true
}
}
Its works 100% ng build --prod --aot --build-optimizer --vendor-chunk=true
Use latest angular cli version and use command ng build --prod --build-optimizer It will definitely reduce the build size for prod env.
This is what the build optimizer does under the hood:
The build optimizer has two main jobs. First, we are able to mark parts of your application as pure,this improves the tree shaking provided by the existing tools, removing additional parts of your application that aren’t needed.
The second thing the build optimizer does is to remove Angular decorators from your application’s runtime code. Decorators are used by the compiler, and aren’t needed at runtime and can be removed. Each of these jobs decrease the size of your JavaScript bundles, and increase the boot speed of your application for your users.
Note : One update for Angular 5 and up, the ng build --prod
automatically take care of above process :)
Taken from the angular docs v9 (https://angular.io/guide/workspace-config#alternate-build-configurations):
By default, a production configuration is defined, and the ng build command has --prod option that builds using this configuration. The production configuration sets defaults that optimize the app in a number of ways, such as bundling files, minimizing excess whitespace, removing comments and dead code, and rewriting code to use short, cryptic names ("minification").
Additionally you can compress all your deployables with @angular-builders/custom-webpack:browser builder where your custom webpack.config.js looks like that:
module.exports = {
entry: {
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash].js'
},
plugins: [
new CompressionPlugin({
deleteOriginalAssets: true,
})
]
};
Afterwards you will have to configure your web server to serve compressed content e.g. with nginx you have to add to your nginx.conf:
server {
gzip on;
gzip_types text/plain application/xml;
gzip_proxied no-cache no-store private expired auth;
gzip_min_length 1000;
...
}
In my case the dist folder shrank from 25 to 5 mb after just using the --prod in ng build and then further shrank to 1.5mb after compression.