Skip to content

ngc/ngtsc codegen should emit tree-shake friendly imports #33796

@filipesilva

Description

@filipesilva

For a module such as:

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 { }

Ivy will emit the following code:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import * as i0 from "@angular/core";
export class AppModule {
}
AppModule.ɵmod = i0.ɵɵdefineNgModule({ type: AppModule, bootstrap: [AppComponent] });
AppModule.ɵinj = i0.ɵɵdefineInjector({ factory: function AppModule_Factory(t) { return new (t || AppModule)(); }, providers: [], imports: [[
            BrowserModule
        ]] });
/*@__PURE__*/ i0.ɵɵsetNgModuleScope(AppModule, { declarations: [AppComponent], imports: [BrowserModule] });
/*@__PURE__*/ i0.ɵsetClassMetadata(AppModule, [{
        type: NgModule,
        args: [{
                declarations: [
                    AppComponent
                ],
                imports: [
                    BrowserModule
                ],
                providers: [],
                bootstrap: [AppComponent]
            }]
    }], null, null);
//# sourceMappingURL=app.module.js.map

The import * as i0 from "@angular/core"; in particular is hard to optimize, as it imports the whole of @angular/core.

Instead it should only emit imports for the used imports:

import {ɵɵdefineNgModule, ɵɵdefineInjector, ɵɵsetNgModuleScope, ɵsetClassMetadata} from "@angular/core";

Alternatively, they could be added to the existing @angular/core import.

@clydin found that TypeScript has done the same for their helpers, for the same reason: microsoft/TypeScript#32742

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compilerIssues related to `ngc`, Angular's template compilerfeatureLabel used to distinguish feature request from other issuesfreq2: medium

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions