Repository navigation
Description
🐞 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:
- Everything currently works as expected within the CLI since it provides optimizations that removes the decorators anyway when using AOT mode or build-optimizer.
- 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
🔥 Exception or Error
__metadata("design:type", HTMLElement)
^
ReferenceError: HTMLElement is not defined
🌍 Your Environment
Angular Version:
Latest: 8.0.0-rc.4Anything else relevant?
Activity
[-]Libraries depended on tsickle decorator downleveling[/-][+]Libraries sometimes depend on tsickle decorator downleveling[/+]Splaktar commentedon May 21, 2019
Is this work relevant here? angular/angular-cli#14473. Maybe it only helps in the constructor case?
alan-agius4 commentedon May 22, 2019
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
resolveJsonModulefor example see angular/tsickle#922. the main reason for this not to go in was not to increase the scope of tsickle.devversion commentedon May 22, 2019
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 withngcand now it no longer is enabled by default.The components repository still opts-in for
tsickledecorator 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
tsicklealways did more than just making output closure-compatible. It had the nice side-effect of decorator downleveling for Angular / and making it SSR compatible.alexeagle commentedon May 22, 2019
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
devversion commentedon May 22, 2019
Totally agreed that
tsickleshould beTypeScript to Closureonly. Also agreed that ideally the decorator downleveling would be part of NGC.. Similarly to howngtscalready 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-infor 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 fromtsickle.clydin commentedon May 22, 2019
The
design:typemetadata is a result of the TSemitDecoratorMetadataoption, 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/webpackfor 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.clydin commentedon May 22, 2019
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
trotyl commentedon May 23, 2019
@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.
clydin commentedon May 23, 2019
The root cause of this issue is the use of the TypeScript
emitDecoratorMetadataoption. 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.29 remaining items
fix(compiler-cli): downlevel angular decorators to static properties (#…
fix(compiler-cli): downlevel angular decorators to static properties (a…
fix(compiler-cli): downlevel angular decorators to static properties (a…