Skip to content

Ivy compiler output of CommonModule is not optimal for tree-shaking #28077

Closed

Description

@alan-agius4

🐞 bug report

Description

When using ivy in compatibility mode, with platformBrowser, the final bundle is not optimal for tree-shaking. As in the final bundle the CommonModule references all module definitions and thus they are marked as used and cannot be dropped by webpack.

var CommonModule = /** @class */ /*@__PURE__*/ (function () {
    function CommonModule() {
    }
    /*@__PURE__*/ _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵsetClassMetadata"](CommonModule, [{
            type: _angular_core__WEBPACK_IMPORTED_MODULE_0__["NgModule"],
            args: [{
                    declarations: [COMMON_DIRECTIVES, COMMON_PIPES],
                    exports: [COMMON_DIRECTIVES, COMMON_PIPES],
                    providers: [
                        { provide: NgLocalization, useClass: NgLocaleLocalization },
                    ]
                }]
        }], function () { return []; }, null);
    CommonModule.ngModuleDef = _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵdefineNgModule"]({ type: CommonModule, bootstrap: [], declarations: [NgClass,
            NgComponentOutlet,
            NgForOf,
            NgIf,
            NgTemplateOutlet,
            NgStyle,
            NgSwitch,
            NgSwitchCase,
            NgSwitchDefault,
            NgPlural,
            NgPluralCase,
            AsyncPipe,
            UpperCasePipe,
            LowerCasePipe,
            JsonPipe,
            SlicePipe,
            DecimalPipe,
            PercentPipe,
            TitleCasePipe,
            CurrencyPipe,
            DatePipe,
            I18nPluralPipe,
            I18nSelectPipe,
            KeyValuePipe], imports: [], exports: [NgClass,
            NgComponentOutlet,
            NgForOf,
            NgIf,
            NgTemplateOutlet,
            NgStyle,
            NgSwitch,
            NgSwitchCase,
            NgSwitchDefault,
            NgPlural,
            NgPluralCase,
            AsyncPipe,
            UpperCasePipe,
            LowerCasePipe,
            JsonPipe,
            SlicePipe,
            DecimalPipe,
            PercentPipe,
            TitleCasePipe,
            CurrencyPipe,
            DatePipe,
            I18nPluralPipe,
            I18nSelectPipe,
            KeyValuePipe] });
    CommonModule.ngInjectorDef = _angular_core__WEBPACK_IMPORTED_MODULE_0__["defineInjector"]({ factory: function CommonModule_Factory(t) { return new (t || CommonModule)(); }, providers: [
            { provide: NgLocalization, useClass: NgLocaleLocalization },
        ], imports: [[COMMON_DIRECTIVES, COMMON_PIPES]] });
    return CommonModule;
}());

🔬 Minimal Reproduction

Use ivy in compatibility mode.

In main.ts

import { enableProdMode } from '@angular/core';
import { platformBrowser } from '@angular/platform-browser';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowser().bootstrapModule(AppModule)
  .catch(err => console.error(err));

In app.module.ts, add BrowserModule

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

🌍 Your Environment

Angular Version: 7.2.0

Anything else relevant?
Been tried with #27848 and angular/angular-cli#13382

Activity

  1. added this to the needsTriage milestone on Jan 11, 2019
  2. filipesilva commented on Jan 22, 2019

    @filipesilva
    Contributor

    Chatted with @alxhub about this, it's on the teams radar and part of the Ivy design.

    Right now this option is always true:

    /**
    * Whether to emit the selector scope values (declarations, imports, exports) inline into the
    * module definition, or to generate additional statements which patch them on. Inline emission
    * does not allow components to be tree-shaken, but is useful for JIT mode.
    */
    emitInline: boolean;

    This option causes the imports to be inlined, and while inlined they are not tree-shakeable. Later on there will be a way for this option to be false and patch the selectors in later.

  3. petebacondarwin commented on Mar 27, 2019

    @petebacondarwin
    Contributor

    Actually that option is not actually implemented at all yet :-)

  4. modified the milestones: needsTriage, Backlog on Apr 2, 2019
  5. added a commit that references this issue on Apr 17, 2019
    9da833a
  6. angular-automatic-lock-bot commented on Sep 14, 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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions