Angular 2: Reduce app size (in addition to bundling/minification)
Solution 1
Now that Angular 2 has gone 2.0.0, there's documentation on Ahead of Time compilation of Angular apps (i.e., from TypeScript + HTML templates to Javascript).
Compilation removes the (template) compiler from Angular 2, reducing the Angular 2 payload by half.
As mentioned by Gunter Zochbauer, tree-shaking with rollup is supported, bringing the bundle size down further.
Tree shaking is now supported for production bundling in the Angular CLI, with AoT compilation support also in development.
With a [email protected] new project without any changes:
ng build --prod
:
3.9K styles.b52d2076048963e7cbfd.bundle.js
183K main.8b778eea5dd35968ef66.bundle.js.gz
805K main.8b778eea5dd35968ef66.bundle.js
ng build --prod --aot
:
3.9K styles.b52d2076048963e7cbfd.bundle.js
99K main.a2eb733289ef46cb798c.bundle.js.gz
452K main.a2eb733289ef46cb798c.bundle.js
Meaning a basic, working app is now at < 100 KB with AoT compilation, minification, tree-shaking, and gzipping.
Solution 2
I use https://tools.pingdom.com for page/css/scripts size checking as they are received from server after a process .
First of all use "gzip" on your server side, before using gzip => 3mb , after gzip 560kb
Secondly ,use this command for your build , "ng build -e=prod --prod --no-sourcemap --aot"
when using "ng build" page size => 560kb ,after using "ng build -e=prod --prod --no-sourcemap --aot" page size => 227kb
I was working on MEAN stack , these helped alot in reducing page size Hope it helps
Comments
-
Harry almost 2 years
I have a small-medium size (~28 KB including just the (TypeScript transpiled) JS + HTML templates) Angular 2 app.
It's based originally on the angular.io quickstart, but now I am bundling/minifying it using JSPM for deployment.
The bundled JS file I get is 2.1 MB, coming down to 449 KB when served with gzip compression.
This is still rather large, and I would like to ask how best to go about reducing the overall size of the app for deployment, to deliver my app in the smallest and most efficient bundle.
Edit: I should mention that I've reduced the uncompressed but minified bundle size to 1.9 MB by importing RxJS operators individually, e.g. with
import 'rxjs/add/operator/map';
; so any size savings I'd be looking for on top of that.Many thanks in advance for your help.
-
AngularM over 7 yearsHow do I use the .gz file instead of the original js file. I made a question for this: stackoverflow.com/questions/41047617/…
-
Harry over 7 yearsHi AngularM, it looks like your separate question has been answered there, but in short, you'll need to configure your web server (usually Apache or Nginx, it looks like you're going through CloudFlare) to serve with gzip. You don't need to configure this in your Angular 2 app, it's all down to the web server and browser to handle.
-
AngularM over 7 yearsWhat's the easiest web server to do and setup. I have no idea how to do any of this
-
user172902 about 7 yearsWouldn't the working app be a total of 425 + 99 + 3.9kb instead? Which is still quite large in a way
-
Harry about 7 yearsNo, you would only need to serve the gzipped main bundle (99K) and not the uncompressed bundle (452K), so it would be 99K for the main bundle plus less than 3.9K for the styles if they were also gzipped.