angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

tree-shaking

Open daniele-zurico opened this issue 9 years ago • 55 comments

Bug Report or Feature Request (mark with an x)

- [x ] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.1.1 node: 7.5.0 os: darwin x64

Hello all, I'm struggling with tree shaking because it looks like that it doesn't work properly. after running the follow command: "bundle-report": "ng build --prod --target=production --environment=prod --aot --stats-json && webpack-bundle-analyzer dist/stats.json",

I realised that my vendor is: 2.4 MB and contain stuff that I don't use. RxJs for example take: screen shot 2017-06-14 at 12 38 42

But in my code I use like that: screen shot 2017-06-14 at 12 31 30

I'm doing something wrong or something wrong is in the tree shaking?

daniele-zurico avatar Jun 14 '17 11:06 daniele-zurico

The very same goes for Angular Material. Once upon a time.. it worked.. I just got the Components that I imported but now it's the whole shabang lurking around the vendor.ts.. sadly I do not rly know when it stoped working. (If I remember correctly it was back then when we still got the ngfactorys and styles of the Components in our chunk files)

Demo App to try out: https://github.com/bboehm86/mat-chunk-test

material_bundle

bboehm86 avatar Jun 15 '17 07:06 bboehm86

Can confirm the same for Lodash. Can't tell if it worked on earlier versions, since I used to use full import on lodash. With optimized imports:

import { first } from 'lodash';
import { mapValues } from 'lodash';
// ...

I still get the whole lodash lib loaded in..

screenshot_5

@angular/cli: 1.1.0 node: 7.4.0 os: win32 x64 @angular/*: 4.1.3

dojchek avatar Jun 17 '17 01:06 dojchek

@daniele-zurico @dojchek @bboehm86 I think your issue is duplicate of this: https://github.com/angular/angular-cli/issues/2901

The Angular CLI team said it's webpack issue therefore it should be reported there and track there. (https://github.com/webpack/webpack/issues/2899).

The sad thing is that issue in webpack is open for 10 months now and doesn't seem to go anywhere.

These led me to believe that maybe CLI should be discourage for use in production, as it clearly doesn't do it's job and making really big bundles. I'm also struggling because of that issue as it's impacting performance a lot. If you are using Angular lazy loading ( loadChildren in router) it's even worse because chunks are really big (1 mb each piece of really small modules), that you get 9 Mb bundle of 5 mb vendor and 4 mb of modules (so sum is 9). Without lazy loading it's little better because you have 5-6mb depending on code base, but still quite not what I expect as I'm using really only small part of RxJS and angular material.

galuszkak avatar Jun 18 '17 07:06 galuszkak

I think it's problem with webpack config. I use own process as ngc=>webpack so result is normal image image

delagen avatar Jul 04 '17 21:07 delagen

@Delagen as far I remember problem was with UglifyJS. Is that included in your webpack config?

See relevant threads: https://github.com/webpack/webpack/issues/2867 webpack https://github.com/mishoo/UglifyJS2/issues/1261 UglifyJS2

galuszkak avatar Jul 05 '17 17:07 galuszkak

@galuszkak This diagrams for minified code with uglify, tried to use Closure, but no success. Uglify works

delagen avatar Jul 05 '17 17:07 delagen

I think in webpack config does not added 'module' field to resolve section, this is why modules resolved via main field and es6 modules does not used

delagen avatar Jul 05 '17 20:07 delagen

Ps may be it left for performance? With real tree shaking my project build up to 20!!! minutes with webpack. May be with rollup will be faster

delagen avatar Jul 06 '17 04:07 delagen

Webpack defaults to resolving module first and only main after that, so we don't specify it ourselves.

So I'm trying to test this, and to do so I did npm i source-map-explorer and added this npm script:

"sme": "ng build --prod --sm --output-hashing=none && source-map-explorer ./dist/vendor.bundle.js ./dist/vendor.bundle.js.map"

I use source-map-explorer because webpack-bundle-analyzer seems to not be working with the latest webpack. Vendor chunking is disabled so everything shows in the same bundle for analysis.

Running this script on a brand new project, and zooming in to rxjs and then operator I see four: mergeAll, merge, multicast, share. Total size 2.28kb.

image

Then on main.ts I add import 'rxjs/add/operator/switchMap';, run the script again.

Now there are five operators, the same as before plus switchMap, total size 3.75kb.

image

This to me seems like the tree shaking available in webpack is working correctly - only the operator I asked for was added.

I cannot speak for Material, but they seem to have an issue opened for how their packaging format is amenable to tree shaking: https://github.com/angular/material2/issues/4137.

For the other example provided here (rxjs, lodash), I'd say that your app might not be using the other operators but a third party library is, and so they get included.

filipesilva avatar Jul 17 '17 09:07 filipesilva

@filipesilva RXJS fine image but @angular/* not image

delagen avatar Jul 17 '17 10:07 delagen

@Delagen those are the correct files for @angular/*. If you open their package.json you will see "module": "./@angular/core.es5.js",.

filipesilva avatar Jul 17 '17 10:07 filipesilva

@filipesilva I know But for my pipeline source-map show like this image

delagen avatar Jul 17 '17 10:07 delagen

What is your pipeline?

filipesilva avatar Jul 17 '17 11:07 filipesilva

@filipesilva I manually build my application using ngc compiler, after it normal webpack build with uglify

delagen avatar Jul 17 '17 11:07 delagen

@filipesilva what you mean with that: For the other example provided here (rxjs, lodash), I'd say that your app might not be using the other operators but a third party library The rxjs is the default one as you can see from my screenshot.

daniele-zurico avatar Jul 17 '17 11:07 daniele-zurico

I will still argue that this issue is dupe of: https://github.com/angular/angular-cli/issues/2901

and @filipesilva said it's related to and it's watching it: https://github.com/webpack/webpack/issues/2899

But it's about year and that issue is still there. Webpack people are saying that this is mostly issue with UglifyJS, so I'm quite confused because I'm experiencing this issue also for some time.

galuszkak avatar Jul 17 '17 11:07 galuszkak

@Delagen then that massively depends on your webpack configuration. Internally the CLI uses ngc as well. I can see that you're tracing sourcemaps for Angular libs, whereas the CLI is not, but other than that I have no idea what you are doing in your config. Are you getting smaller builds using your pipeline?

@daniele-zurico I mean that, if your third party libraries use RxJs or Lodash, it will of course be included in the final build even if your app does not use it directly.

filipesilva avatar Jul 17 '17 11:07 filipesilva

@filipesilva also if lodash is not used at all in my code? To be more specific the third party library has a module that use lodash but that module is not imported in my code. Lodash will end up in my build? If it's I don't understand what mean tree shacking

daniele-zurico avatar Jul 17 '17 13:07 daniele-zurico

@daniele-zurico lodash is using CommonJS rather than ES Module, it's not shakable theoretically. You may be talking about some other kind of DCE instead of tree-shaking.

trotyl avatar Jul 17 '17 14:07 trotyl

@filipesilva I use generic config with uglify. It seems some plugin disable tree shaking at all. My config I think allow it, and build time increase up to 20 min, but size is smaller. Looks at angular package sizes in my screenshots.

delagen avatar Jul 17 '17 17:07 delagen