How to decrease prod bundle size?

后端 未结 14 1339
Happy的楠姐
Happy的楠姐 2020-11-28 01:31

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

14条回答
  •  攒了一身酷
    2020-11-28 01:39

    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.

提交回复
热议问题