Skip to content

Class Decorator prevents tree-shaking #24122

@fischeversenker

Description

@fischeversenker

Command

build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

A library component with a (non-Angular) decorator will not be tree-shaken. The same component, if inside an app, will be properly tree-shaken.

There is a history of issues related to this topic:

Minimal Reproduction

Reproduction repository: https://github.com/fischeversenker/ng-decorator-tree-shaking/tree/274524fde27002e87f1535ef6bbef2cf8088ec8d

The essence:

This library code:

import { Component } from '@angular/core';

/*@__PURE__*/
function noopDecoratorFactory() {
  return /*@__PURE__*/ (target: any) => target;
}

@noopDecoratorFactory()
@Component({
  selector: 'lib-my-library',
  template: `my-library works!`
})
export class MyLibraryComponent {}

turns into this:

let MyLibraryComponent = class MyLibraryComponent {
};
// ...
MyLibraryComponent = __decorate([
    noopDecoratorFactory()
], MyLibraryComponent);

which seems to not be tree-shakeable.

Exception or Error

No response

Your Environment

Angular CLI: 14.2.6
Node: 16.16.0
Package Manager: yarn 1.22.19 
OS: linux x64

Angular: 14.2.7
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.6
@angular-devkit/build-angular   14.2.6
@angular-devkit/core            14.2.6
@angular-devkit/schematics      14.2.6
@angular/cli                    14.2.6
@schematics/angular             14.2.6
ng-packagr                      14.2.2
rxjs                            7.5.7
typescript                      4.7.4

Anything else relevant?

The real-life use case where this adds huge amounts of unused code to our bundles is when using https://github.com/ngneat/until-destroy#use-with-ivy in library components.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions