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?
@ianchi for information on Angular Material tree shaking, check out https://github.com/angular/material2/issues/4137.
@filipesilva I've just tried the new RC of the cli, which includes the optimizer. My gziped app reduced from around 250kb to 185kb, that's about 25% reduction! Great work!!
@ianchi that's great news! Let me know if you experience any problems with it.
I need to do more testing, but so far, it's not working if you eject the project:
Webpack throws the following error:
new PurifyPlugin(),
^
ReferenceError: PurifyPlugin is not defined
at Object.<anonymous> (webpack.config.js:453:9)
Yeah that seems to be missing. You can get it from import { PurifyPlugin } from '@angular-devkit/build-optimizer';.
Tree shaking is working much better with this plugin. Most of the unnecessary ngFactories are gone. So far I only spotted some suspicious code with styles imports that could have been removed, but perhaps it's a Material issue.
var
styles_MdCalendar = [ ".mat-calendar{display:block}.mat-calendar-header{padding:8px 8px 0 8px}.mat-calendar-content{padding:0 8px 8px 8px;outline:0}.mat-calendar-controls{display:flex;padding:5% calc(100% / 14 - 22px) 5% calc(100% / 14 - 22px)}.mat-calendar-spacer{flex:1 1 auto}.mat-calendar-period-button{min-width:0}.mat-calendar-arrow{display:inline-block;width:0;height:0;border-left:5px solid transparent;border-right:5px solid transparent;border-top-width:5px;border-top-style:solid;margin:0 0 0 5px;vertical-align:middle}.mat-calendar-arrow.mat-calendar-invert{transform:rotate(180deg)}[dir=rtl] .mat-calendar-arrow{margin:0 5px 0 0}.mat-calendar-next-button,.mat-calendar-previous-button{position:relative}.mat-calendar-next-button::after,.mat-calendar-previous-button::after{content:'';position:absolute;top:0;left:0;bottom:0;right:0;margin:15.5px;border:0 solid currentColor;border-top-width:2px}[dir=rtl] .mat-calendar-next-button,[dir=rtl] .mat-calendar-previous-button{transform:rotate(180deg)}.mat-calendar-previous-button::after{border-left-width:2px;transform:translateX(2px) rotate(-45deg)}.mat-calendar-next-button::after{border-right-width:2px;transform:translateX(-2px) rotate(45deg)}.mat-calendar-table{border-spacing:0;border-collapse:collapse;width:100%}.mat-calendar-table-header th{text-align:center;padding:0 0 8px 0}.mat-calendar-table-header-divider{position:relative;height:1px}.mat-calendar-table-header-divider::after{content:'';position:absolute;top:0;left:-8px;right:-8px;height:1px}" ],
styles_MdCalendarBody = (index_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__._2({
encapsulation: 2,
styles: styles_MdCalendar,
data: {}
}), [ ".mat-calendar-body{min-width:224px}.mat-calendar-body-label{padding:7.14286% 0 7.14286% 7.14286%;height:0;line-height:0;transform:translateX(-6px);text-align:left}.mat-calendar-body-cell{position:relative;width:14.28571%;height:0;line-height:0;padding:7.14286% 0;text-align:center;outline:0;cursor:pointer}.mat-calendar-body-disabled{cursor:default}.mat-calendar-body-cell-content{position:absolute;top:5%;left:5%;display:flex;align-items:center;justify-content:center;box-sizing:border-box;width:90%;height:90%;border-width:1px;border-style:solid;border-radius:50%}[dir=rtl] .mat-calendar-body-label{padding:0 7.14286% 0 0;transform:translateX(6px);text-align:right}" ])
I'm not sure if I get it quite right, but it seems that the definition of styles_MdCalendar is finished inside the definition of the next style styles_MdCalendarBody as a kind of side effect with the comma operator (invoking the createRendererType2 function).
This goes on with different styles, forming a chain where each definition references the previous component, and so cannot be eliminated. I only use one of the last components of the chain, but this seems to pull all the chain to the top, with many unnecessary definitions (which contains very long strings).
I don't know where do Decorators get transformed to this chained construction. I guess the Angular Compiler?
Another question. Is there any option in the CLI to add html minification of the component's templates? Perhaps in the ejected project I can add the loader to the html chain with raw-loader or is it handled internally in the ngtools/webpack-loader?. It would be nice to have it directly in the CLI. This can also shrink the bundle a bit, depending on how 'unoptimized' where the templates in the first place.
Finally, I wanted to also check using source map explorer, but the generated source map (with -sm option) only shows "*" for all the vendor sources. Any hint to make it work, so it also follows the source files in node_modules? I'm using this command:
ng build --prod --vendor-chunk=false --build-optimizer=true -sm
stats.json also seems to be somehow broken, as I cannot explore it with any tool.
@ianchi there is no support for html minification in the CLI no. index.html has it, but it's the only one.
There's a bug with sourcemaps with --bo right now, being tracked in https://github.com/angular/angular-cli/issues/7093.
Unsure what's wrong with stats.json, but it seems related to scope hoisting as well.
As for the Material styles you related, I cannot answer but will ping the appropriate people (@igorminar @kara).
Also, thanks for looking at it so attentively, this is all great feedback!
Perhaps the problem with styles is that the call to createRendererType2 is not annotated as PURE.
This is from the same app, but just disabling the Uglify plugin:
var styles_MdCalendar = ['.mat-calendar{display:block}.mat-calendar-header{padding:8px 8px 0 8px}.mat-calendar-content{padding:0 8px 8px 8px;outline:0}.mat-calendar-controls{display:flex;padding:5% calc(100% / 14 - 22px) 5% calc(100% / 14 - 22px)}.mat-calendar-spacer{flex:1 1 auto}.mat-calendar-period-button{min-width:0}.mat-calendar-arrow{display:inline-block;width:0;height:0;border-left:5px solid transparent;border-right:5px solid transparent;border-top-width:5px;border-top-style:solid;margin:0 0 0 5px;vertical-align:middle}.mat-calendar-arrow.mat-calendar-invert{transform:rotate(180deg)}[dir=rtl] .mat-calendar-arrow{margin:0 5px 0 0}.mat-calendar-next-button,.mat-calendar-previous-button{position:relative}.mat-calendar-next-button::after,.mat-calendar-previous-button::after{content:\'\';position:absolute;top:0;left:0;bottom:0;right:0;margin:15.5px;border:0 solid currentColor;border-top-width:2px}[dir=rtl] .mat-calendar-next-button,[dir=rtl] .mat-calendar-previous-button{transform:rotate(180deg)}.mat-calendar-previous-button::after{border-left-width:2px;transform:translateX(2px) rotate(-45deg)}.mat-calendar-next-button::after{border-right-width:2px;transform:translateX(-2px) rotate(45deg)}.mat-calendar-table{border-spacing:0;border-collapse:collapse;width:100%}.mat-calendar-table-header th{text-align:center;padding:0 0 8px 0}.mat-calendar-table-header-divider{position:relative;height:1px}.mat-calendar-table-header-divider::after{content:\'\';position:absolute;top:0;left:-8px;right:-8px;height:1px}'];
var RenderType_MdCalendar = index_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__["_2" /* ɵcrt */]({ encapsulation: 2, styles: styles_MdCalendar,
data: {} });
In constrast, ngFactories are annotated and thus unused ones get removed:
var MdCalendarNgFactory = /*@__PURE__*/index_ngfactory___WEBPACK_IMPORTED_MODULE_0__angular_core__["_0" /* ɵccf */]('md-calendar', material_es5_MdCalendar, View_MdCalendar_Host_0, { startAt: 'startAt', startView: 'startView',
selected: 'selected', minDate: 'minDate', maxDate: 'maxDate', dateFilter: 'dateFilter' }, { selectedChange: 'selectedChange' }, []);
The comma operator construct, must be some kind of Uglify optimization.
Going back to HTML minification, are there plans to add it to components' templates? Is there a way I can add it in the ejected webpack? I suspect this must be done inside the ngtools/webpack loader, in between the replacement to require the html template file and the compiling of the string.
We fiddled with html processing for images a while ago, and couldn't reach a secure approach so left them as is.
On an ejected config you can probably edit the loader for html (currently raw-loader https://github.com/angular/angular-cli/blob/master/packages/@angular/cli/models/webpack-configs/common.ts#L108).
Thinking about it a bit more, I'm not 100% sure that will work with --aot... but then again --aot shouldn't care, since it will make the factories anyway? @hansl would know.
is possible to eject as well with --bo command? v1.4.0
The option '--bo' is not registered with the eject command. Run ng eject --help for a list of supported options.
@daniele-zurico There are no --bo command at all, only --build-optimizer or --buildOptimizer, see https://github.com/angular/angular-cli/pull/7231 for more information.
Why was this closed? All of the above issues still occur.
@raysuelzer probably it means "WON'T FIX".
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.