Title: Use the Angular compiler to compile @sentry/angular · Issue #4644 · getsentry/sentry-javascript · GitHub
Open Graph Title: Use the Angular compiler to compile @sentry/angular · Issue #4644 · getsentry/sentry-javascript
X Title: Use the Angular compiler to compile @sentry/angular · Issue #4644 · getsentry/sentry-javascript
Description: Problem Statement As initially brought up in #3282, our Angular SDK currently causes a compiler error at application build time when TraceDirective or TraceModule are imported and used to trace the initialization spans of (custom) Angula...
Open Graph Description: Problem Statement As initially brought up in #3282, our Angular SDK currently causes a compiler error at application build time when TraceDirective or TraceModule are imported and used to trace the...
X Description: Problem Statement As initially brought up in #3282, our Angular SDK currently causes a compiler error at application build time when TraceDirective or TraceModule are imported and used to trace the...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/4644
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use the Angular compiler to compile @sentry/angular","articleBody":"### Problem Statement\r\n\r\nAs initially brought up in #3282, our Angular SDK currently causes a compiler error at application build time when `TraceDirective` or `TraceModule` are imported and used to trace the initialization spans of (custom) Angular components. While working on this issue it turned out that the fix requires a change to the build procedure of our Angular SDK which will a) fix this problem but b) also allow us to support Angular better in the future. \r\n\r\nCurrently, our Angular SDK is using `tsc` as a compiler like most of our JS SDKs. With `tsc` we can compile our current Angular SDK and it detects and compiles the Angular-specific decorators as (experimental) JS decorators (they are in proposal for JS according to [TS docs](https://www.typescriptlang.org/docs/handbook/decorators.html)). During compilation they are treated like JS decorators in the generated JS. In contrast, the Angular compiler, detects Angular specific decorators and compiles them differently s.t. the information in the decorators about components, directives, pipes, etc. is understood correctly by the NG runtime. Running `ng build` or similar aot compiles projects and libraries to this Angular specific JS that’s understood by the NG runtime. \r\n\r\n### Example\r\n\r\n`TraceDirective` is an Angular directive and hence the `@Directive` decorator is used:\r\n```typescript\r\n@Directive({ selector: '[trace]' })\r\nexport class TraceDirective implements OnInit, AfterViewInit {...}\r\n``` \r\n\r\nThe `tsc`-compiled output treats the `@Directive` like a \"normal\" decorator which is not what Angular expects at application compile time (or runtime):\r\n```javascript\r\nvar TraceDirective = /** @class */ (function () {\r\n ...\r\n tslib_1.__decorate([\r\n core_1.Input('trace')\r\n ], TraceDirective.prototype, \"componentName\", void 0);\r\n TraceDirective = tslib_1.__decorate([\r\n core_1.Directive({ selector: '[trace]' })\r\n ], TraceDirective);\r\n return TraceDirective;\r\n}());\r\nexports.TraceDirective = TraceDirective;\r\n```\r\n\r\nInstead, Angular expects Angular-specific JS syntax that treats the decorators differently. This syntax is even Angular version specific, as for Angular 10 (Ivy Off):\r\n\r\n```javascript\r\nclass TraceDirective {...}\r\nTraceDirective.decorators = [\r\n { type: Directive, args: [{ selector: '[trace]' },] }\r\n];\r\nTraceDirective.propDecorators = {\r\n componentName: [{ type: Input, args: ['trace',] }]\r\n};\r\n```\r\n\r\nor Angular 13:\r\n```js\r\nclass TraceDirective {...}\r\nTraceDirective.ɵfac = i0.ɵɵngDeclareFactory(...);\r\nTraceDirective.ɵdir = i0.ɵɵngDeclareDirective(...);\r\ni0.ɵɵngDeclareClassMetadata(...);\r\n```\r\n\r\n### Solution Brainstorm\r\n\r\nThe solution to this problem is to use the Angular compiler instead of `tsc` to compile our Angular SDK. After a successful PoC implementation, we're currently tracking the changes required for this in PR #4641. Given that this is a rather big change to our build procedure (possibly even breaking), this will be introduced with v7. It should be noted that this change does not only fix the original bug (#3282) but it also enables us to a) support Angular better in the future (e.g. with additional directives/components/etc.) and it aligns the compilation of our SDK with the [recommendations for library creation from the Angular team](https://v10.angular.io/guide/creating-libraries). \r\n\r\nHere's what will change:\r\n* Instead of `tsc` we will use the Angular 10 CLI and `ng-packagr` to build the SDK\r\n * The produced package conforms to the [Angular Package Format (APF) for Angular 10](https://docs.google.com/document/d/1uh2D6XqaGh2yjjXwfF4SrJqWl1MBhMPntlNBBsk6rbw/edit)\r\n * The package will contain the ESM2015, FESM2015 and UMD JS module formats which are all required for Angular 10 libraries.\r\n * In case an Angular application shall support ES5, the angular CLI will downlevel the ESM2015 JS to ES5 at application compile time.\r\n * Newer versions of Angular (11-13) will use `ngcc` or the Angular linker to make the library compatible at application build time.\r\n* Due to the forward compatibility of Angular libraries, this will limit the usage of the SDK to Angular >= 10, as we will be using Angular 10 to build the SDK. This enables compatibility with Angular 10-13. \r\n * So far we have not explicitly stated which Angular versions are supported by our SDK, although our peer dependencies also list Angular 10-13. \r\n * Therefore, people could be using the SDK with earlier versions than Angular 10 which as a result would break their applications if they update to v7. How to procede here remains TBD but one option would be to further offer a `tsc`-compiled \"legacy\" version of the SDK for Angular \u003c10 users which will work but not support `TraceDirective` (and hence `TraceModule`).\r\n* Our Angular SDK documentation should be updated w.r.t. supported Angular versions and the usage of `TraceDirective`.","author":{"url":"https://github.com/Lms24","@type":"Person","name":"Lms24"},"datePublished":"2022-02-25T16:53:16Z","url":"https://github.com/getsentry/sentry-javascript/issues/4644"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3c1118e9-d4c6-c5f1-2254-f936e1580d29 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAE4:CFE11:8F0CB9:C7F211:6B0428AF |
| html-safe-nonce | cd14e8d7a8f752327030b39a3aaf2112c178e62b9e9cff85c2d3a7c827cc170f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUU0OkNGRTExOjhGMENCOTpDN0YyMTE6NkIwNDI4QUYiLCJ2aXNpdG9yX2lkIjoiNTk5NjM5OTc5MzAzMzk4MDA3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 966308ce8e0ade205984a02311d1601b35966af352dca709f774fea3adb59dfd |
| hovercard-subject-tag | issue:1150659674 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/4644/issue_layout |
| twitter:image | https://opengraph.githubassets.com/92f497fe438993f92266bf584e489595b74e42a5f51122c84696a52d37c02763/getsentry/sentry-javascript/issues/4644 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/92f497fe438993f92266bf584e489595b74e42a5f51122c84696a52d37c02763/getsentry/sentry-javascript/issues/4644 |
| og:image:alt | Problem Statement As initially brought up in #3282, our Angular SDK currently causes a compiler error at application build time when TraceDirective or TraceModule are imported and used to trace the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Lms24 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width