voliva / angular2-interceptors

79 stars 20 forks source link

Support AOT #4

Open maxime1992 opened 7 years ago

maxime1992 commented 7 years ago

When I try to build the app with AOT, I get this error message :

Am I missing something or is it just not supported yet ?

Error encountered resolving symbol values statically. Calling function 'provideInterceptorService', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol

voliva commented 7 years ago

Not supported yet, I will look into it

maxime1992 commented 7 years ago

I don't have time right now to help, but this link helped me when I was setting up AOT https://github.com/qdouble/angular-webpack2-starter#aot--donts

maxime1992 commented 7 years ago

@voliva any news ? :)

voliva commented 7 years ago

It's in my TODO list, hopefully I can work soon on it, but it looks like a hard issue.

Meanwhile, you can use the verbose version of the provider: provideInterceptorService was made so it was easier to provide the service without dealing with underlying dependencies.

Should look something like this

providers: [
  LoadingService, // Declare other providers here, specially the ones that are interceptors
  {
    provide: InterceptorService,
    useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions, loadingService:LoadingService) => { // Add it to the factory function
      let service = new InterceptorService(xhrBackend, requestOptions);
      service.addInterceptor(loadingService); // Add your interceptors the pipeline
      service.addInterceptor(new ServerURLInterceptor());
      return service;
    },
    /* IMPORTANT: Add it to the deps array in the same order the useFactory method is declared */
    deps: [XHRBackend, RequestOptions, LoadingService]
  }
]

This workaround should allow AOT. I will try to find a way to make it easier while supporting AOT.

maxime1992 commented 7 years ago

Thanks for looking into that ! In the meantime, I'll try your solution.

jtushar53 commented 7 years ago

@voliva , made some changes to make it work here , i am using ionic 2 rc1,

after googling got this https://github.com/angular/angular/issues/11262 solutions by @vicb this made build without errors.

now the problem is when http.post is made nothing happens and yes i have replaced http with InterceptorService

Update: After debugging, InterceptorService inside constructer is undefined constructor(public http: InterceptorService,public logging: Logging)

voliva commented 7 years ago

I've been working all this morning on this issue and... it's hard. For AoT you can't call any function if it's not exported in the module declaration. This means that I can't even use [].push(), limiting so much the scriptability for dealing with an array of interceptors. I can use these functions in providers, so I considered creating two providers: One that serves the array of interceptors and the InterceptorService, that gets the array of interceptors from the former provider. But I still have an issue: I have to generate the deps array, and I do that by pushing all the dependencies I need from the Interceptors (basically, those that are Services)

For now I can't think how to get untied by that and come up with a solution. A future solution is when Typescript has something like Macros (there's an issue posted for that), where I can set up that deps array before Angular tries to AoT compile.

I will push my work in a separate branch, just to keep it (feature/easyProvideAoT), but it doesn't work yet.

Remember that using the "verbose" provider is AoT-compatible, I just need to update the docs on how to do that.

maxime1992 commented 7 years ago

@voliva thanks for the heads up. I'm using ngrx in my project and there's also a know bug with angular-cli so right now I can't compile with --aot anyway but as soon as they released a patch I'll also give a try to your verbose solution.

kinglionsoft commented 7 years ago

+1