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

@Delagen it really depends on a lot of things. If you could provide me with your config I can run some benchmarks and see if something could be used in the in CLI that we aren't using already.

filipesilva avatar Jul 17 '17 17:07 filipesilva

unsubscribe me please to many emails thank you

On Mon, Jul 17, 2017 at 11:22 AM, Filipe Silva [email protected] wrote:

@Delagen https://github.com/delagen it really depends on a lot of things. If you could provide me with your config I can run some benchmarks and see if something could be used in the in CLI that we aren't using already.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/6676#issuecomment-315821449, or mute the thread https://github.com/notifications/unsubscribe-auth/AIBfew3Hr2yXHACBXL2AspK3pbMfvcYRks5sO5g8gaJpZM4N5wlw .

bossysmile11 avatar Jul 17 '17 17:07 bossysmile11

@bossysmile11 I cannot unsubscribe you myself, but you can click on this button on the side of the issue in github: image

filipesilva avatar Jul 17 '17 17:07 filipesilva

@filipesilva I cannot provide config, cause it generated with private library at runtime. Simply used loaderoptions plugin with minimize and disable debug options, uglifyjs. Used awesome-typescript-loader as typescript with module Es2015 setting, with ts 2.4.1 ESNext. I try to make demo repo for this.

delagen avatar Jul 17 '17 18:07 delagen

Thank you for taking the time to do it @Delagen 👍

filipesilva avatar Jul 17 '17 19:07 filipesilva

@filipesilva Yes. It's causes that you ignored source-maps in node_modules, so users saw as error.

{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [nodeModules] },

Created sample project. Difference is about 1K

delagen avatar Jul 17 '17 20:07 delagen

I'm glad to hear there is no significant size difference. We did remove the source map support for node_modules, yes, it was negatively affecting performance at one point. There is a feature request for adding them back (https://github.com/angular/angular-cli/issues/5477).

filipesilva avatar Jul 18 '17 09:07 filipesilva

Sorry guys but at that point I got lost....starting from my original issue...do you think that everything is working as expected? I need to do something? or is not doing tree shacking properly?

daniele-zurico avatar Jul 18 '17 14:07 daniele-zurico

Tree shaking, as far as webpack is capable of doing, is working correctly from what I can tell.

https://github.com/angular/angular-cli/pull/6365 implements scope hoisting, which should help as well.

https://github.com/angular/angular-cli/pull/6520 adds a couple of opt-in experimental optimizations.

filipesilva avatar Jul 18 '17 14:07 filipesilva

is that available in 1.2.1? or we need to wait the next release? and last question is that command correct and optimised for build production: ng build --target=production --environment=prod --aot

daniele-zurico avatar Jul 18 '17 14:07 daniele-zurico

Scope hoisting is available in 1.3.0-beta.1. Both should be available in 1.3.0 final.

filipesilva avatar Jul 18 '17 14:07 filipesilva

and what about my actual command to build in production mode?

daniele-zurico avatar Jul 18 '17 14:07 daniele-zurico

I'm not sure what you mean, can you rephrase that?

filipesilva avatar Jul 18 '17 14:07 filipesilva

To build in production mode i'm using this command: ng build --target=production --environment=prod --aot It's the best way or I'm doing something wrong or can I add same extra parameter?

daniele-zurico avatar Jul 18 '17 14:07 daniele-zurico

Just doing ng build --prod does all those things, and a couple more (https://github.com/angular/angular-cli/wiki/build#bundling--tree-shaking). It is the best way imho.

filipesilva avatar Jul 18 '17 14:07 filipesilva

This is what I don't understand (look on the vendor): ng build --prod screen shot 2017-07-18 at 15 59 23

ng serve --prod screen shot 2017-07-18 at 16 01 27

daniele-zurico avatar Jul 18 '17 15:07 daniele-zurico

ng serve --prod also includes live reload functionality from webpack-dev-server, which increases the size of the vendor bundle. It also has gzip enabled while serving.

filipesilva avatar Jul 18 '17 15:07 filipesilva

but is the opposite ... I was expecting that but: ng serve (vendor) --> 239Kb ng build (vendor) -->790Kb

daniele-zurico avatar Jul 18 '17 15:07 daniele-zurico

The greyed out (bigger) number is the content size (after being unzipped), while the black number is the size of the response (what was transferred) itself. Due to gzip, the response is smaller than the content.

In ng build the vendor has a content size of 789KB, while on ng serve the content size is 911KB. But since ng serve uses gzip, it was delivered compressed at 239KB.

You used some server to server the result of ng build, and that server doesn't seem to have gzip enabled. That is why most requests don't have a smaller response size.

filipesilva avatar Jul 18 '17 15:07 filipesilva

@filipesilva the optimizer you referenced seems a great feature. Right now I'm not sure I understand the status of tree-shaking in Angular. I'm using Angular Material, and it brings in a lot of components even if a don't use them anywhere. So I don't understand your comment:

Tree shaking, as far as webpack is capable of doing, is working correctly from what I can tell.

Is this then not a problem of webpack but of the angular compiler, that leaves function calls not marked as pure when creating the factories? For instance, this ends up in my bundled code (comming from index.ngfactory.js):

var MdNativeDateModuleNgFactory = createNgModuleFactory(MdNativeDateModule, (([])), function (_l) {
    return moduleDef([moduleProvideDef(512, ComponentFactoryResolver, CodegenComponentFactoryResolver, [[8, (([]))], [3, ComponentFactoryResolver], NgModuleRef]), moduleProvideDef(4608, DateAdapter, NativeDateAdapter, (([]))), moduleProvideDef(512, NativeDateModule, NativeDateModule, (([]))), moduleProvideDef(512, MdNativeDateModule, MdNativeDateModule, (([]))), moduleProvideDef(256, MD_DATE_FORMATS, MD_NATIVE_DATE_FORMATS, (([])))]);
});

I never used MdNativeDateModule, but how could Webpack/Uglify possibly know if createNgModuleFactory call has side effects or not, so to be able to remove it? If it is not removed, then MdNativeDateModule is kept, which in turns brings in a lot more unused stuff.

Will this be addressed with the optimizer you mentioned?

ianchi avatar Jul 18 '17 19:07 ianchi