Angular 2: Reduce app size (in addition to bundling/minification)

天涯浪子 提交于 2019-11-28 10:07:16

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 ng-cli@1.0.0-beta.17 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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!