tree-shaking
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:

But in my code I use like that:

I'm doing something wrong or something wrong is in the tree shaking?
@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.
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 I cannot unsubscribe you myself, but you can click on this button on the side of the issue in github:

@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.
Thank you for taking the time to do it @Delagen 👍
@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
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).
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?
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.
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
Scope hoisting is available in 1.3.0-beta.1. Both should be available in 1.3.0 final.
and what about my actual command to build in production mode?
I'm not sure what you mean, can you rephrase that?
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?
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.
This is what I don't understand (look on the vendor):
ng build --prod

ng serve --prod

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.
but is the opposite ... I was expecting that but: ng serve (vendor) --> 239Kb ng build (vendor) -->790Kb
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 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?