How to decrease prod bundle size?

后端 未结 14 1344
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:47

    One thing I wish to share is how imported libraries increase the size of the dist. I had angular2-moment package imported, whereas I could do all the date time formatting I required using the standard DatePipe exported from @angular/common.

    With Angular2-Moment "angular2-moment": "^1.6.0",

    chunk {0} polyfills.036982dc15bb5fc67cb8.bundle.js (polyfills) 191 kB {4} [initial] [rendered] chunk {1} main.e7496551a26816427b68.bundle.js (main) 2.2 MB {3} [initial] [rendered] chunk {2} styles.056656ed596d26ba0192.bundle.css (styles) 69 bytes {4} [initial] [rendered] chunk {3} vendor.62c2cfe0ca794a5006d1.bundle.js (vendor) 3.84 MB [initial] [rendered] chunk {4} inline.0b9c3de53405d705e757.bundle.js (inline) 0 bytes [entry] [rendered]

    After removing Angular2-moment and using DatePipe instead

    chunk {0} polyfills.036982dc15bb5fc67cb8.bundle.js (polyfills) 191 kB {4} [initial] [rendered] chunk {1} main.f2b62721788695a4655c.bundle.js (main) 2.2 MB {3} [initial] [rendered] chunk {2} styles.056656ed596d26ba0192.bundle.css (styles) 69 bytes {4} [initial] [rendered] chunk {3} vendor.e1de06303258c58c9d01.bundle.js (vendor) 3.35 MB [initial] [rendered] chunk {4} inline.3ae24861b3637391ba70.bundle.js (inline) 0 bytes [entry] [rendered]

    Note the vendor bundle has reduced half a Megabyte!

    Point is it is worth checking what angular standard packages can do even if you are already familiar with an external lib.

    0 讨论(0)
  • 2020-11-28 01:50

    I have a angular 5 + spring boot app(application.properties 1.3+) with help of compression(link attached below) was able to reduce the size of main.bundle.ts size from 2.7 MB to 530 KB.

    Also by default --aot and --build-optimizer are enabled with --prod mode you need not specify those separately.

    https://stackoverflow.com/a/28216983/9491345

    0 讨论(0)
  • 2020-11-28 01:52

    If you have run ng build --prod - you shouldn't have vendor files at all.

    If I run just ng build - I get these files:

    The total size of the folder is ~14MB. Waat! :D

    But if I run ng build --prod - I get these files:

    The total size of the folder is 584K.

    One and the same code. I have enabled Ivy in both cases. Angular is 8.2.13.

    So - I guess you didn't add --prod to your build command?

    0 讨论(0)
  • 2020-11-28 01:55

    Update February 2020

    Since this answer got a lot of traction, I thought it would be best to update it with newer Angular optimizations:

    1. As another answerer said, ng build --prod --build-optimizer is a good option for people using less than Angular v5. For newer versions, this is done by default with ng build --prod
    2. Another option is to use module chunking/lazy loading to better split your application into smaller chunks
    3. Ivy rendering engine comes by default in Angular 9, it offers better bundle sizes
    4. Make sure your 3rd party deps are tree shakeable. If you're not using Rxjs v6 yet, you should be.
    5. If all else fails, use a tool like webpack-bundle-analyzer to see what is causing bloat in your modules
    6. Check if you files are gzipped

    Some claims that using AOT compilation can reduce the vendor bundle size to 250kb. However, in BlackHoleGalaxy's example, he uses AOT compilation and is still left with a vendor bundle size of 2.75MB with ng build --prod --aot, 10x larger than the supposed 250kb. This is not out of the norm for angular2 applications, even if you are using v4.0. 2.75MB is still too large for anyone who really cares about performance, especially on a mobile device.

    There are a few things you can do to help the performance of your application:

    1) AOT & Tree Shaking (angular-cli does this out of the box). With Angular 9 AOT is by default on prod and dev environment.

    2) Using Angular Universal A.K.A. server-side rendering (not in cli)

    3) Web Workers (again, not in cli, but a very requested feature)
    see: https://github.com/angular/angular-cli/issues/2305

    4) Service Workers
    see: https://github.com/angular/angular-cli/issues/4006

    You may not need all of these in a single application, but these are some of the options that are currently present for optimizing Angular performance. I believe/hope Google is aware of the out of the box shortcomings in terms of performance and plans to improve this in the future.

    Here is a reference that talks more in depth about some of the concepts i mentioned above:

    https://medium.com/@areai51/the-4-stages-of-perf-tuning-for-your-angular2-app-922ce5c1b294

    0 讨论(0)
  • 2020-11-28 01:55

    This did reduce the size in my case:

    ng build --prod --build-optimizer --optimization.
    

    For Angular 5+ ng-build --prod does this by default. Size after running this command reduced from 1.7MB to 1.2MB, but not enough for my production purpose.

    I work on facebook messenger platform and messenger apps need to be lesser than 1MB to run on messenger platform. Been trying to figure out a solution for effective tree shaking but still no luck.

    0 讨论(0)
  • 2020-11-28 02:00

    Lodash can contribute a bug chunk of code to your bundle depending on how you import from it. For example:

    // includes the entire package (very large)
    import * as _ from 'lodash';
    
    // depending on your buildchain, may still include the entire package
    import { flatten } from 'lodash';
    
    // imports only the code needed for `flatten`
    import flatten from 'lodash-es/flatten'
    

    Personally I still wanted smaller footprints from my utility functions. E.g. flatten can contribute up to 1.2K to your bundle, after minimization. So I've been building up a collection of simplified lodash functions. My implementation of flatten contributes around 50 bytes. You can check it out here to see if it works for you: https://github.com/simontonsoftware/micro-dash

    0 讨论(0)
提交回复
热议问题