You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 4, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 4, 2025. It is now read-only.
Purify doesn't work with NgModuleFactory in non-webpack environment #102
Since it's too obvious I don't think there're any needs for a repo of reproduction.
Example input:
/** * @fileoverview This file is generated by the Angular template compiler. * Do not edit. * @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride} *//* tslint:disable */import*asi0from'@angular/core';import*asi1from'@angular/material';import*asi2from'@angular/cdk';import*asi3from'@angular/platform-browser';import*asi4from'@angular/common';import*asi5from'@angular/forms';import*asi6from'@angular/http';exportvarMdCoreModuleNgFactory=i0.ɵcmf(i1.MdCoreModule,[],function(_l){returni0.ɵmod([i0.ɵmpd(512,i0.ComponentFactoryResolver,i0.ɵCodegenComponentFactoryResolver,[[8,[]],[3,i0.ComponentFactoryResolver],i0.NgModuleRef]),i0.ɵmpd(6144,i2.DIR_DOCUMENT,null,[i3.DOCUMENT]),i0.ɵmpd(4608,i2.Directionality,i2.Directionality,[[2,i2.DIR_DOCUMENT]]),i0.ɵmpd(4608,i2.Platform,i2.Platform,[]),i0.ɵmpd(5120,i1.ScrollDispatcher,i1.ɵe,[[3,i1.ScrollDispatcher],i0.NgZone,i2.Platform]),i0.ɵmpd(5120,i1.ViewportRuler,i1.ɵc,[[3,i1.ViewportRuler],i1.ScrollDispatcher]),i0.ɵmpd(4608,i1.ScrollStrategyOptions,i1.ScrollStrategyOptions,[i1.ScrollDispatcher,i1.ViewportRuler]),i0.ɵmpd(4608,i2.MdMutationObserverFactory,i2.MdMutationObserverFactory,[]),i0.ɵmpd(5120,i1.OverlayContainer,i1.ɵa,[[3,i1.OverlayContainer]]),i0.ɵmpd(4608,i1.ɵt,i1.ɵt,[i1.ViewportRuler]),i0.ɵmpd(4608,i1.Overlay,i1.Overlay,[i1.ScrollStrategyOptions,i1.OverlayContainer,i0.ComponentFactoryResolver,i1.ɵt,i0.ApplicationRef,i0.Injector,i0.NgZone]),i0.ɵmpd(4608,i4.NgLocalization,i4.NgLocaleLocalization,[i0.LOCALE_ID]),i0.ɵmpd(4608,i2.InteractivityChecker,i2.InteractivityChecker,[i2.Platform]),i0.ɵmpd(4608,i2.FocusTrapFactory,i2.FocusTrapFactory,[i2.InteractivityChecker,i2.Platform,i0.NgZone]),i0.ɵmpd(5120,i2.LiveAnnouncer,i2.LIVE_ANNOUNCER_PROVIDER_FACTORY,[[3,i2.LiveAnnouncer],[2,i2.LIVE_ANNOUNCER_ELEMENT_TOKEN],i2.Platform]),i0.ɵmpd(512,i1.CompatibilityModule,i1.CompatibilityModule,[]),i0.ɵmpd(512,i2.BidiModule,i2.BidiModule,[]),i0.ɵmpd(256,i1.MATERIAL_SANITY_CHECKS,true,[]),i0.ɵmpd(512,i1.MdCommonModule,i1.MdCommonModule,[[2,i3.DOCUMENT],[2,i1.MATERIAL_SANITY_CHECKS]]),i0.ɵmpd(512,i1.MdLineModule,i1.MdLineModule,[]),i0.ɵmpd(512,i2.PlatformModule,i2.PlatformModule,[]),i0.ɵmpd(512,i1.ScrollDispatchModule,i1.ScrollDispatchModule,[]),i0.ɵmpd(512,i1.MdRippleModule,i1.MdRippleModule,[]),i0.ɵmpd(512,i2.ObserveContentModule,i2.ObserveContentModule,[]),i0.ɵmpd(512,i2.PortalModule,i2.PortalModule,[]),i0.ɵmpd(512,i1.OverlayModule,i1.OverlayModule,[]),i0.ɵmpd(512,i4.CommonModule,i4.CommonModule,[]),i0.ɵmpd(512,i2.A11yModule,i2.A11yModule,[]),i0.ɵmpd(512,i1.MdSelectionModule,i1.MdSelectionModule,[]),i0.ɵmpd(512,i1.MdOptionModule,i1.MdOptionModule,[]),i0.ɵmpd(512,i1.MdCoreModule,i1.MdCoreModule,[])]);});
We don't have a version of purify for tools other than webpack at the moment. I'll go into detail why below, but bear in mind I agree there should be a way to support other build chains.
Purify is essentially annotating the webpack module loader functions with /*@__PURE__*/ comments for UglifyJS (for es5). Currently we do this for some known imports (ɵccf/ɵcmf/ɵcrt/NgModuleFactory) plus a list of imports added by the Prefix functions transform via a comment (/** PURE_IMPORTS_START tslib, angular_core PURE_IMPORTS_END */).
In the UglifyJS side, it only knows about whether a function or getter is pure. It does not support pure classes, or pure imports, or pure anything else. Just functions.
It's important to recognize that this works only because of the interplay of the webpack module loader and UglifyJS: in webpack imports are function calls, we mark those functions as pure, uglify sees that function as pure and can drop it.
In your example you have ESM imports, not function calls. Even we added a comment to it, uglify would not do anything as it's not a function call.
So this approach can only work for module loaders that convert imports into function calls.
That's why the webpack pure module proposal is important. It gets the concept of a pure module into the module loader. I hope this will gain traction in other module loaders because otherwise it's not really possible to address the concept of pure module optimization.
Neither actually. What's needed is better dead code elimination in the various tool chains but this is unfortunately a non-trivial task. This is why tools like this and the webpack module side effect tagging proposal exist; they atleast help fill the void.
In your example you have ESM imports, not function calls.
Actually it is, I mean the:
exportvarMdCoreModuleNgFactory=/* Add pure annotation here */i0.ɵcmf(i1.MdCoreModule,[],/* ... */);
It would be same with or without webpack.
Using a function call here making it unable to be tree-shake, so it would leave all ModuleNgFactorys intact, but in Angular only the NgFactory for entry modules are used.
Activity
filipesilva commentedon Aug 15, 2017
We don't have a version of purify for tools other than webpack at the moment. I'll go into detail why below, but bear in mind I agree there should be a way to support other build chains.
Purify is essentially annotating the webpack module loader functions with
/*@__PURE__*/comments for UglifyJS (for es5). Currently we do this for some known imports (ɵccf/ɵcmf/ɵcrt/NgModuleFactory) plus a list of imports added by the Prefix functions transform via a comment (/** PURE_IMPORTS_START tslib, angular_core PURE_IMPORTS_END */).This is needed because webpack does not have the notion of a pure module. But there is a feature in development to transmit that information (https://github.com/webpack/webpack/tree/feature/pure-module/examples/pure-module) in the package itself.
In the UglifyJS side, it only knows about whether a function or getter is pure. It does not support pure classes, or pure imports, or pure anything else. Just functions.
It's important to recognize that this works only because of the interplay of the webpack module loader and UglifyJS: in webpack imports are function calls, we mark those functions as pure, uglify sees that function as pure and can drop it.
In your example you have ESM imports, not function calls. Even we added a comment to it, uglify would not do anything as it's not a function call.
So this approach can only work for module loaders that convert imports into function calls.
That's why the webpack pure module proposal is important. It gets the concept of a pure module into the module loader. I hope this will gain traction in other module loaders because otherwise it's not really possible to address the concept of pure module optimization.
trotyl commentedon Aug 15, 2017
So I guess
material2still needs to separate the bundles as discussed in angular/components#4137 , or wouldmaterial2be also webpack-only as well?clydin commentedon Aug 15, 2017
Neither actually. What's needed is better dead code elimination in the various tool chains but this is unfortunately a non-trivial task. This is why tools like this and the webpack module side effect tagging proposal exist; they atleast help fill the void.
trotyl commentedon Aug 22, 2017
@filipesilva
Actually it is, I mean the:
It would be same with or without webpack.
Using a function call here making it unable to be tree-shake, so it would leave all
ModuleNgFactorys intact, but in Angular only theNgFactoryfor entry modules are used.alexeagle commentedon Sep 4, 2018
This issue was moved to angular/angular-cli#12090