1

I have a need for dynamically creating routes and components in Angular. And I have such code block to get components which are defined in module from declarations property;

import { ɵReflectionCapabilities as ReflectionCapabilities } from "@angular/core";
routeFactory(types: any[]) {
    //...
    let annotations,
    declarations: any[] = [];
    for (let type of types) {
        // Here annotations returns [] in AOT Prod Mode
        // When prod mode is disabled it returns DecoratorFactory
        annotations = refCap.annotations(type)[0];
        declarations = declarations.concat(annotations.declarations);
    }
}

I use this function in routeProvider as below;

export function routeProvider(...types: any[]) {
  return {
    ngModule: RouterModule,
    providers: [
      [
        {
          provide: ROUTES,
          multi: true,
          useFactory: routeFactory(types)              
        }
      ]
    ]
  };
}

My actual issue is that I am unable to get annotations when I compile using such settings;

"production": {
                "aot": true,
                "buildOptimizer": true,
                "optimization": true, 
                ...
              }
 

If I disable optimization, then it works fine both in AOT and JIT. refCap.annotations(type)[0]; returns [DecoratorFactory] as I expected. I believe it is because of TerserPlugin which remove unnecessary functions/definitions. I have found some posts but could not get any help;
How can I get metadata for the current module in Angular 2?
Accessing `selector` from within an Angular 2 component
So I would like to know if there is any possibility (hacky or proper) to access annotations and its declarations using ReflectionCapabilities in AOT prod mod.

Angular version: 10.2.3

vvvnn
  • 241
  • 2
  • 10

0 Answers0