{"id":21575481,"url":"https://github.com/rangle/angular-2-aot-sandbox","last_synced_at":"2025-04-09T10:10:11.379Z","repository":{"id":54549876,"uuid":"71474592","full_name":"rangle/angular-2-aot-sandbox","owner":"rangle","description":"Sandbox for testing Angular 2's AoT specifically what expressions break it","archived":false,"fork":false,"pushed_at":"2021-03-31T07:34:44.000Z","size":113,"stargazers_count":297,"open_issues_count":16,"forks_count":22,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-02T03:41:30.803Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rangle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-20T15:03:07.000Z","updated_at":"2024-07-02T07:26:44.000Z","dependencies_parsed_at":"2022-08-13T19:20:46.994Z","dependency_job_id":null,"html_url":"https://github.com/rangle/angular-2-aot-sandbox","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rangle%2Fangular-2-aot-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rangle%2Fangular-2-aot-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rangle%2Fangular-2-aot-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rangle%2Fangular-2-aot-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rangle","download_url":"https://codeload.github.com/rangle/angular-2-aot-sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018061,"owners_count":21034048,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T12:13:23.028Z","updated_at":"2025-04-09T10:10:11.357Z","avatar_url":"https://github.com/rangle.png","language":"TypeScript","funding_links":[],"categories":["Angular2"],"sub_categories":[],"readme":"# Angular 2 AoT SandBox\nThis repo is used to formalize what we can and cannot do with AoT (through `ngc` or `@ngtools/webpack`).\n\nTo run specific test case like `control`:\n```\nnode sandbox-loader.js control\n```\nThe default is to use `ngc` for AoT, but you can also use `@ngtools/webpack`:\n```\nnode sandbox-loader.js control ngtools\n```\nor JiT:\n```\nnode sandbox-loader.js control jit\n```\nThe bundle files are inside `./dist/`, and to host:\n```\nnpm run start\n```\n## Current Status\n\n|                                  Test                                  |  AoT With `ngc`  |  AoT With `@ngtools/webpack`  |  JiT  |\n| ---------------------------------------------------------------------- | :--------------: | :---------------------------: | :---: |\n| [control](#control-top)                                                |        ✅        |               ✅              |   ✅   |\n| [form-control](#form-control-top)                                      |        ✅        |               ✅              |   ✅   |\n| [func-in-string-config](#func-in-string-config-top)                    |        ✅        |               ✅              |   ✅   |\n| [jquery](#jquery-top)                                                  |        ✅        |               ✅              |   ✅   |\n| [template-variable](#template-variable-top)                            |        ✅        |               ✅              |   ✅   |\n| [template-expression](#template-expression-top)                        |        ✅        |               ✅              |   ✅   |\n| [mut-property-decorator](#mut-property-decorator-top)                  |        ✅        |               ❌              |   ✅   |\n| [nomut-property-decorator](#nomut-property-decorator-top)              |        ✅        |               ❌              |   ✅   |\n| [angular-redux-store](#angular-redux-store-top)                        |        ✅        |               ✅              |   ✅   |\n| [ngrx](#ngrx-top)                                                      |        ✅        |               ✅              |   ✅   |\n| [ngrx-compose](#ngrx-compose-top)                                      |        ✅        |               ✅              |   ✅   |\n| [arrow-function-exports](#arrow-function-exports-top)                  |        ❌        |               ❌              |   ✅   |\n| [default-exports](#default-exports-top)                                |        ❌        |               ❌              |   ✅   |\n| [form-control-error](#form-control-error-top)                          |        ❌        |               ❌              |   ✅   |\n| [func-as-variable-export](#func-as-variable-export-top)                |        ❌        |               ❌              |   ✅   |\n| [func-declaration-export](#func-declaration-export-top)                |        ✅        |               ✅              |   ✅   |\n| [func-in-declarations](#func-in-declarations-top)                      |        ❌        |               ❌              |   ✅   |\n| [func-in-providers](#func-in-providers-top)                            |        ❌        |               ❌              |   ✅   |\n| [func-in-providers-useFactory](#func-in-providers-usefactory-top)      |        ❌        |               ❌              |   ✅   |\n| [func-in-providers-useValue](#func-in-providers-usevalue-top)          |        ❌        |               ❌              |   ✅   |\n| [func-in-routes](#func-in-routes-top)                                  |        ❌        |               ❌              |   ✅   |\n| [interpolated-es6](#interpolated-es6-top)                              |        ❌        |               ❌              |   ✅   |\n| [property-accessors](#property-accessors-top)                          |        ❌        |               ❌              |   ✅   |\n| [private-contentchild](#private-contentchild-top)                      |        ❌        |               ❌              |   ✅   |\n| [private-hostbinding](#private-hostbinding-top)                        |        ❌        |               ❌              |   ✅   |\n| [private-input](#private-input-top)                                    |        ❌        |               ❌              |   ✅   |\n| [private-output](#private-output-top)                                  |        ❌        |               ❌              |   ✅   |\n| [private-property](#private-property-top)                              |        ❌        |               ❌              |   ✅   |\n| [private-viewchild](#private-viewchild-top)                            |        ❌        |               ❌              |   ✅   |\n| [service-with-generic-type-param](#service-with-generic-type-param-top)|        ❌        |               ❌              |   ✅   |\n\n## AoT Do's and Don'ts\nThis section explains the cases listed above, and will show how each of them fails and works.\n\n### arrow-function-exports [:top:](#current-status)\n\nArrow function does not work with AoT when it is passed to an `NgModule`.\n\nDon't:\n```ts\nexport const couterReducer = (state, action: Action) =\u003e {\n  // ...\n}\n```\n\nDo:\n```ts\nexport function counterReducer(state, action: Action) {\n  // ...\n}\n```\n\n### control [:top:](#current-status)\n\nThis is used as a simplest working case.\n\n### default-exports [:top:](#current-status)\nDefault exports are not supported with AoT.\n\nDon't:\n```ts\nexport default class AppComponent {};\n```\nDo:\n```ts\nexport class AppComponent {};\n```\n\n### form-control [:top:](#current-status)\n\nUse `this.helloForm.controls[\"greetingMessage\"]` to retrieve form control is fine.\n\n### form-control-error [:top:](#current-status)\n\nThe syntax `errors?` is not supported by AoT.\n\nDon't:\n```html\n{{helloForm.controls[\"greetingMessage\"].errors?.minlength}}\n```\nDo:\n```html\n{{helloForm.controls[\"greetingMessage\"].hasError(\"minlength\")}}\n```\n\n### func-as-variable-export [:top:](#current-status)\n\nExport function as variable is fine with AoT except when it is passed to an NgModule.\n\nDon't:\n```ts\n//foo.ts\nexport const foo = function foo(state: number) {\n  return \"foo\" + state;\n}\n\n//app.module.ts\n@NgModule({\n  imports: [\n    //...\n    FooModule.provideFoo(foo),\n  ],\n  //...\n})\nexport class AppModule {};\n```\nDo:\n```ts\n//foo.ts\nexport function foo(state: number) {\n  return \"foo\" + state;\n}\n\n//app.module.ts\n@NgModule({\n  imports: [\n    //...\n    FooModule.provideFoo(foo),\n  ],\n  //...\n})\nexport class AppModule {};\n```\n\n### func-declaration-export [:top:](#current-status)\n\nThis is a fixed version of `func-as-variable-export`.\n\n### func-in-declarations [:top:](#current-status)\nDon't:\n```ts\nfunction someLoader() {...}\n@NgModule({\n  //...\n  declarations: someLoader(),\n  //...\n})\nexport class AppModule {};\n```\n\nApply `@angular/router` or other Angular logic to re-implement the same thing.\n\n### func-in-providers [:top:](#current-status)\nPass a result of function to providers is not supported by AoT.\n\nDon't:\n```ts\n//services/service-providers.ts\nimport { AppService } from \"./app.service\";\nconst services = {\n  AppService: AppService\n};\n\nexport function getService(k) {\n  return services[k];\n}\nexport const serviceProviders = Object.keys(services).map(getService);\n\n//app.module.ts\nimport { serviceProviders } from \"./services/service-providers\";\n@NgModule({\n  //...\n  providers: serviceProviders\n})\nexport class AppModule {};\n```\nDo:\n```ts\n//services/service-providers.ts\nimport { AppService } from \"./app.service\";\nexport const serviceProviders = [\n  AppService\n];\n\n//app.module.ts\nimport { serviceProviders } from \"./services/service-providers\";\n@NgModule({\n  //...\n  providers: serviceProviders\n})\nexport class AppModule {};\n```\n\n### func-in-providers-useFactory [:top:](#current-status)\n\nInstead of using function directly, export it first in another module and import it back.\n\nDon't:\n```ts\n@NgModule({\n  //...\n  providers: [\n    { provide: AppService, useFactory: () =\u003e { return { name: \"world test\" }; }},\n  ],\n  //...\n})\nexport class AppModule {};\n\n```\nDo:\n```ts\nimport { randomFactory } from \"./random.factory.ts\"\n@NgModule({\n  //...\n  providers: [\n    { provide: AppService, useFactory: randomFactory }},\n  ],\n  //...\n})\nexport class AppModule {};\n\n```\n\n### func-in-providers-useValue [:top:](#current-status)\n\nThis case only fails when the passed value is generated by a nested function. If the foo inside `foo.ts` is not returning another function, then it will pass. Another solution is to replace useValue with useFactory and set it to use bar instead of barConst.\n\nDon't:\n```ts\n//foo.ts\nexport function foo() { return function() { return {}; }; };\nexport const fooConst = foo();\n\n//bar.ts\nimport { fooConst } from \"./foo\";\nexport function bar() { return fooConst(); }\nexport const barConst = bar();\n\n//app.module.ts\n//...\nimport { barConst } from \"./bar\";\n\n@NgModule({\n  //...\n  providers: [ { provide: AppService, useValue: barConst }]\n})\nexport class AppModule {};\n```\n\nDo:\n```ts\n//foo.ts\nexport function foo() { return {}; };\nexport const fooConst = foo();\n\n//app.module.ts\n//...\nimport { fooConst } from \"./bar\";\n\n@NgModule({\n  //...\n  providers: [ { provide: AppService, useValue: fooConst }]\n})\nexport class AppModule {};\n```\nor\n```ts\n//foo.ts\nexport function foo() { return function() { return {}; }; };\nexport function fooFactory() {\n   return foo();\n}\n//app.module.ts\n//...\nimport { fooFactory } from \"./foo\";\n\n@NgModule({\n  //...\n  providers: [ { provide: AppService, useFactory: fooFactory }]\n})\nexport class AppModule {};\n```\n### func-in-routes [:top:](#current-status)\n\nDirect use of function in route is not supported by AoT. Either avoid using it or export it from other module.\n\nDon't:\n```ts\nfunction random() {\n  return [{\n    path: \"\",\n    component: AppViewComponent\n  }];\n}\nconst SAMPLE_APP_ROUTES: Routes = random();\n```\nDo:\n```ts\nconst SAMPLE_APP_ROUTES: Routes = [{\n  path: \"\",\n  component: AppViewComponent\n}];\n```\nor\n```ts\nimport { random } from \"./random.routes.ts\";\nconst SAMPLE_APP_ROUTES: Routes = random();\n```\n\n### func-in-string-config [:top:](#current-status)\n\nFunction in string configuration is supported by AoT.\n\n### interpolated-es6 [:top:](#current-status)\n\nDon't:\n```ts\n@Component({\n  selector: \"app\",\n  template: `Hello ${1 + 1}`\n})\nexport class AppComponent {};\n```\nDo:\n```ts\n@Component({\n  selector: \"app\",\n  template: `Hello {{value}}`\n})\nexport class AppComponent {\n  value:number = 1 + 1;\n};\n```\n\n### jquery [:top:](#current-status)\n\nTo use jQuery with AoT, one way is to use the `webpack.ProvidePlugin` to provide jquery as global variable; another way is to inject jquery as a service like this:\n```ts\n//...\nconst $ = require(\"jquery\");\nexport function jqueryFactory () {\n  return $;\n}\n@NgModule({\n  //...\n  providers: [\n    { provide: \"JqueryService\", useFactory: jqueryFactory},\n  ],\n  bootstrap: [AppComponent]\n})\nexport class AppModule {};\n```\nNotice that `useValue` here does not work.\n\n### mut-property-decorator [:top:](#current-status)\n\nMutating property decorator is supported by `ngc`, but does not work with `@ngtools/webpack`, because `@ngtools/webpack` explicitly remove all custom decorators. Details can be found here: https://github.com/angular-redux/ng2-redux/issues/236.\n\nDesired effect of this case is that `Hello World 42` instead of `Hello 42` should be displayed.\n### angular-redux [:top:](#current-status)\n\nSetting up basic `angular-redux/store` is fine with AoT. This includes the `@select` decorator, both with `@ngtools/webpack` and raw `ngc`.\n\n### ngrx [:top:](#current-status)\n\nSetting up basic `ngrx` is fine with AoT. This includes their `@Effect` decorator as well.\n\n### ngrx-compose [:top:](#current-status)\n\nDirect use of compose does not work with AoT, it requires a wrapper.\n\nDon't:\n```ts\n//reducers/index.ts\nconst reducers = {\n  counter: fromCounter.counterReducer,\n};\nconst reducer: ActionReducer\u003cState\u003e = compose(storeLogger(), combineReducers)(reducers);\n\n//app.module.ts\n//...\nimport { reducer } from \"./reducers\";\n@NgModule({\n  imports: [\n    BrowserModule,\n    StoreModule.provideStore(reducer)\n  ],\n  //...\n})\nexport class AppModule {}\n```\nDo:\n```ts\n//reducers/index.ts\nconst reducers = {\n  counter: fromCounter.counterReducer,\n};\nconst developmentReducer: ActionReducer\u003cState\u003e = compose(storeLogger(), combineReducers)(reducers);\nexport function reducer(state: any, action: any) {\n  return developmentReducer(state, action);\n};\n\n//app.module.ts\n//...\nimport { reducer } from \"./reducers\";\n@NgModule({\n  imports: [\n    BrowserModule,\n    StoreModule.provideStore(reducer)\n  ],\n  //...\n})\nexport class AppModule {}\n```\n\n### nomut-property-decorator [:top:](#current-status)\n\nNo-mutating property decorator is supported by `ngc`, but does not work with `@ngtools/webpack`, because `@ngtools/webpack` explicitly remove all custom decorators. Details can be found here: https://github.com/angular-redux/ng2-redux/issues/236.\n\nDesired effect of this case is that console should raise errors because we are trying to change a `@ReadOnly` property.\n### private-contentchild [:top:](#current-status)\n\nDon't:\n```ts\nexport class TabComponent {\n  //...\n  @ContentChildren(PaneDirective) private panes: QueryList\u003cPaneDirective\u003e;\n  //...\n}\n```\nDo:\n```ts\nexport class TabComponent {\n  //...\n  /** @internal */\n  @ContentChildren(PaneDirective) panes: QueryList\u003cPaneDirective\u003e;\n  //...\n}\n```\n\n### private-hostbinding [:top:](#current-status)\n\nDon't:\n```ts\nexport class NameDirective {\n  @HostBinding(\"class.world\") private isWorld: boolean = false;\n}\n```\nDo:\n```ts\nexport class NameDirective {\n  /** @internal */\n  @HostBinding(\"class.world\") isWorld: boolean = false;\n}\n```\n\n### private-input [:top:](#current-status)\n\nDon't:\n```ts\nexport class NameComponent {\n @Input() private name: String;\n};\n```\nDo:\n```ts\nexport class NameComponent {\n  /** @internal */\n @Input() name: String;\n};\n```\n\n### private-output [:top:](#current-status)\n\nDon't:\n```ts\nexport class NameComponent {\n @Output() private onClicked = new EventEmitter\u003cboolean\u003e();\n //...\n};\n```\nDo:\n```ts\nexport class NameComponent {\n  /** @internal */\n @Output() onClicked = new EventEmitter\u003cboolean\u003e();\n //...\n};\n```\n\n### private-property [:top:](#current-status)\n\nDon't:\n```ts\nexport class AppComponent {\n  private name: string;\n  constructor() {\n    this.name = 'World';\n  }\n}\n```\nDo:\n```ts\nexport class AppComponent {\n  /** @internal */\n  name: string;\n  constructor() {\n    this.name = 'World';\n  }\n}\n```\n\n### private-viewchild [:top:](#current-status)\n\nDon't:\n```ts\nexport class AppComponent implements AfterViewInit {\n  @ViewChild(ChildDirective) private child: ChildDirective;\n\n  ngAfterViewInit() {\n    console.log(this.child.value);\n  }\n};\n```\nDo:\n```ts\nexport class AppComponent implements AfterViewInit {\n  /** @internal */\n  @ViewChild(ChildDirective) child: ChildDirective;\n\n  ngAfterViewInit() {\n    console.log(this.child.value);\n  }\n};\n```\n### property-accessors [:top:](#current-status)\n\nThe es6 property accessors are not supported by AoT if it is passed to an NgModule.\n\nDon't:\n```ts\nimport { ERROR, WARNING } from \"./definitions\";\n\nexport function handler1() {};\nexport function handler2() {};\nexport const ErrorEventHandlers = {\n  [ERROR]: {\n    handler: handler1\n  },\n  [WARNING]: {\n    handler: handler2\n  }\n};\n```\nDo:\n```ts\nexport function handler1() {};\nexport function handler2() {};\nexport const ErrorEventHandlers = {\n  'ERROR': {\n    handler: handler1\n  },\n  'WARNING': {\n    handler: handler2\n  }\n};\n```\n### service-with-generic-type-param [:top:](#current-status)\nDon't:\n```ts\nexport class AppComponent {\n  greeting: string;\n  constructor(helloService: HelloService\u003cboolean\u003e) {\n    this.greeting = helloService.getHello(true);\n  }\n};\n```\nThe generic type parameter is not supported in Angular, and there is more [detail](https://github.com/angular/angular/issues/11057).\n\n### template-expression [:top:](#current-status)\n\nAssign expression like `greeting + answer` to `template` is supported by AoT.\n\n### template-variable [:top:](#current-status)\n\nAssign variable like `greeting` to `template` is supported by AoT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frangle%2Fangular-2-aot-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frangle%2Fangular-2-aot-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frangle%2Fangular-2-aot-sandbox/lists"}