Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant parse project when providing a service using a token and useFactory #21

Closed
bluEEil opened this issue Oct 26, 2017 · 4 comments
Closed

Comments

@bluEEil
Copy link

bluEEil commented Oct 26, 2017

I started working with ngrev today.
I think its a neat tool which is very important for the angular echo systems.
One issue that I found is that when I try to parse my project as is. I get an error about not allowing to use function. It always occured for all instance in my code where I tried to provide a token with useFactory, heres an example (I can't post the project that i'm working on specifically so ill only post examples):

{ provide: APP_INITIALIZER, useFactory: (configService: ConfigService) => () => configService.load(), deps: [ConfigService], multi: true },

so to overcome it, I just did something like this:
`
export function nothing(): any {}

...

{ provide:
APP_INITIALIZER,
useFactory: nothing,
deps: [ConfigService], multi: true
},`
which made the project parse, but ofc its not a great solution and also this configuration will not be visible in the ngrev output graph.

@mgechev
Copy link
Owner

mgechev commented Oct 26, 2017

Thanks for giving it a try!

The problem is in the arrow function, you cannot use them in provider declaration because the context is not statically inferable.

As I've mentioned in the readme, the project needs to be compatible with the AoT compilation of the framework.

@mgechev mgechev closed this as completed Oct 26, 2017
@bluEEil
Copy link
Author

bluEEil commented Oct 26, 2017

If thats the case, could you give me some direction in how I should write such code to make it work without arrow functions? I tried doing something like moving the function into an exported function in the same file, but I still got the error.
It doesn't make much sense if there is no simple way to provide with useFactory and use AoT in the same time right?

@mgechev
Copy link
Owner

mgechev commented Oct 26, 2017

You can use traditional function expression:

{
  provide: APP_INITIALIZER,
  useFactory: function (configService: ConfigService) {
    return function () {
      configService.load();
    }
  },
  deps: [ConfigService],
  multi: true
}

@bluEEil
Copy link
Author

bluEEil commented Oct 26, 2017

I see, ill give it a try, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants