Skip to content

Libraries sometimes depend on tsickle decorator downleveling #30586

Closed

Description

@devversion

🐞 bug report

Affected Package

The issue is caused by package @angular/....

@angular/compiler-cli / @angular/bazel

Is this a regression?

No

Description

Angular applications which are built with NGC but aren't setting up tsickle for decorator downleveling (e.g. don't set the annotationsAs: "static fields"), can't be rendered on the server if DOM-specific types are referenced on class members which are decorated.

e.g.

@Directive(...)
export class MyDirective {
  @Input() triggerElement: HTMLElement;
}

This results in the following ES5 JavaScript output if tsickle is not enabled

    __decorate([
        core_1.Input(),
        __metadata("design:type", HTMLElement)
    ], MyDirective.prototype, "triggerElement");

Notice how the __decorate call tries to access HTMLELement from a global context. This causes an exception when the following file is executed in NodeJS since DOM APIs are not available. We mock most of the DOM APIs with domino but providing these mocks globally so that such failures can be avoided is not common right now.

Building the project with tsickle fixes the problem automatically as decorators are downleveled into a static field while tsickle also discards the type reference to HTMLElement (as it is not relevant for Angular).

MyDirective.propDecorators = {
  triggerElement: [{ type: core_1.Input }]
};

TL;DR:

  1. Everything currently works as expected within the CLI since it provides optimizations that removes the decorators anyway when using AOT mode or build-optimizer.
  2. It becomes an issue if a project is not built with the CLI / and does not use build-optimizer. Previously tsickle was part of the NGC workflow by default.. so it now causes unexpected failures. Needs documentation? See: 29761ea

How is Angular Bazel affected?

With Bazel it is not possible to opt-in for tsickle's decorator downleveling. Tsickle can be enabled by passing the bazelOptions: {tsickle: true}, but that comes with more tsickle transformations which aren't desired. e.g. type imports are rewritten to goog.requireType.

🔬 Minimal Reproduction

https://github.com/devversion/ngc-without-tsickle

🔥 Exception or Error

__metadata("design:type", HTMLElement)
                                  ^
ReferenceError: HTMLElement is not defined

🌍 Your Environment

Angular Version:

Latest: 8.0.0-rc.4

Anything else relevant?

Activity

  1. changed the title [-]Libraries depended on tsickle decorator downleveling[/-] [+]Libraries sometimes depend on tsickle decorator downleveling[/+] on May 21, 2019
  2. added
    area: core
    on May 21, 2019
  3. added this to the needsTriage milestone on May 21, 2019
  4. added
    hotlist: components team
    area: bazel
    on May 21, 2019
  5. Splaktar commented on May 21, 2019

    @Splaktar
    Contributor

    Is this work relevant here? angular/angular-cli#14473. Maybe it only helps in the constructor case?

  6. alan-agius4 commented on May 22, 2019

    @alan-agius4
    Contributor

    Just an addition to this, I remember that @alexeagle & @vikerman had a discussion with the Google TypeScript team and it was brought up that outside of Google unless using Closure there shouldn't be the need for tsickle. However seeing the latest issues, the above and angular/angular-cli#14473 maybe it is.

    Tsickle will be re-introduced when generating libraries with CLI, however of course users might still opt-out if they want to use resolveJsonModule for example see angular/tsickle#922. the main reason for this not to go in was not to increase the scope of tsickle.

  7. devversion commented on May 22, 2019

    @devversion
    MemberAuthor

    This issue is mostly about projects not using the CLI but depending on libraries which no longer aren't transformed by tsickle. Tsickle previously was part of the default workflow with ngc and now it no longer is enabled by default.

    The components repository still opts-in for tsickle decorator downleveling but we aren't able to do this when switching to Bazel (as mentioned in the issue description).

    To be clear: Yes. In my opinion tsickle always did more than just making output closure-compatible. It had the nice side-effect of decorator downleveling for Angular / and making it SSR compatible.

  8. alexeagle commented on May 22, 2019

    @alexeagle
    Contributor

    History: when we first needed decorator downleveling around Angular 2 timeframe, TS didn't have CustomTransformers. tsickle was the closest thing available so we added the functionality there.

    Now, it doesn't make sense. tsickle should be "ts to closure" (that's where the name derives: ts2cl) and the decorator downleveling should be a transpile step for all Angular code. I wish it was baked right into tsc/ngc using CustomTransformer but I guess putting it in the webpack layer via angular/angular-cli#14473 is our short-term answer to keep forwardRef working in target=es2015

  9. devversion commented on May 22, 2019

    @devversion
    MemberAuthor

    Totally agreed that tsickle should be TypeScript to Closure only. Also agreed that ideally the decorator downleveling would be part of NGC.. Similarly to how ngtsc already handles the decorator downleveling.

    I'm not sure if it's worth doing anything to make it work for non-CLI applications in the meanwhile since we still have the opt-in for tsickle right now (which could stay until ngtsc is the default AFAIK). Though what we miss right now is a temporary solution to make this work with Bazel. There is no way to only opt into the decorator downleveling from tsickle.

  10. clydin commented on May 22, 2019

    @clydin
    Member

    The design:type metadata is a result of the TS emitDecoratorMetadata option, which is only needed in JIT to support dependency injection and is separate from decorator downleveling/removal. If JIT mode is not required then the TS option can be removed. (relevant TS code)

    Regardless, the custom transformer introduced in @ngtools/webpack for 8.0 should ideally be moved into ngc proper (with additional refactoring and cleanup -- the code was intended to minimize risk by leveraging the existing Tsickle code as much as possible). However, the transformer would also need to be present in the CLI as the Angular compiler is not used in JIT mode.

  11. clydin commented on May 22, 2019

    @clydin
    Member

    Also of interest is that TypeScript has an alternative format for the metadata that uses arrow functions. However, it is disabled via a hardcoded constant. https://github.com/microsoft/TypeScript/blob/master/src/compiler/transformers/ts.ts#L1659

  12. trotyl commented on May 23, 2019

    @trotyl
    Contributor

    The design:type metadata is a result of the TS emitDecoratorMetadata option, which is only needed in JIT to support dependency injection and is separate from decorator downleveling/removal. If JIT mode is not required then the TS option can be removed.

    @clydin Even some transformer rewrites the parameter type to a value production, TypeScript will still remove its import, resulting in broken code, which happens in angular/tsickle#635.

  13. clydin commented on May 23, 2019

    @clydin
    Member

    The root cause of this issue is the use of the TypeScript emitDecoratorMetadata option. Tsickle is only relevant here because it removes the decorators. This effectively disables the option as there is no "decorator metadata" to emit without the decorators. Further, no transform of the parameter types is necessary outside of providing JIT mode support. As such, if JIT mode support is not required then this problem is effectively void.

  14. 29 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: bazelarea: compilerfreq3: highhotlist: components teamtype: bug/fix

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions