Skip to content

Purify doesn't work with NgModuleFactory in non-webpack environment #12090

Closed

Description

@alexeagle

From @trotyl on August 13, 2017 9:14

Relates to angular/components#4137 .

Currently purify plugin only supports hard-coded webpack regex in https://github.com/angular/devkit/blob/master/packages/angular_devkit/build_optimizer/src/purify/purify.ts#L52-L60, and it cannot work with any non-webpack tools.

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 * as i0 from '@angular/core';
import * as i1 from '@angular/material';
import * as i2 from '@angular/cdk';
import * as i3 from '@angular/platform-browser';
import * as i4 from '@angular/common';
import * as i5 from '@angular/forms';
import * as i6 from '@angular/http';
export var MdCoreModuleNgFactory = i0.ɵcmf(i1.MdCoreModule, [], function (_l) {
    return i0.ɵ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, [])]);
});

Copied from original issue: angular/devkit#102

Activity

  1. alexeagle commented on Sep 4, 2018

    @alexeagle
    ContributorAuthor

    From @filipesilva on August 15, 2017 9:59

    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.

  2. alexeagle commented on Sep 4, 2018

    @alexeagle
    ContributorAuthor

    From @trotyl on August 15, 2017 11:0

    So I guess material2 still needs to separate the bundles as discussed in angular/components#4137 , or would material2 be also webpack-only as well?

  3. alexeagle commented on Sep 4, 2018

    @alexeagle
    ContributorAuthor

    From @clydin on August 15, 2017 18:26

    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.

  4. alexeagle commented on Sep 4, 2018

    @alexeagle
    ContributorAuthor

    From @trotyl on August 22, 2017 16:36

    @filipesilva

    In your example you have ESM imports, not function calls.

    Actually it is, I mean the:

    export var MdCoreModuleNgFactory = /* 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.

  5. added this to the Backlog milestone on Sep 4, 2018
  6. clydin commented on Oct 2, 2018

    @clydin
    Member

    The purify plugin has been removed in 7.0.

  7. angular-automatic-lock-bot commented on Sep 8, 2019

    @angular-automatic-lock-bot

    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.

  8. locked and limited conversation to collaborators on Sep 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureLabel used to distinguish feature request from other issues

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions