3

When I use angular2 AoT, I get an error:

 Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 54:17 in the original .ts file), resolving symbol COMPILER_PROVIDERS in

and in my Directive Module, I have code like this:

import { COMPILER_PROVIDERS } from '@angular/compiler';
@NgModule({
/*imports ...*/
providers: [
    COMPILER_PROVIDERS,
]
})

I understand that I should change the COMPILER_PROVIDERS to an exported function, But when I check the source code of @angular/compiler, I find this:

export declare const COMPILER_PROVIDERS: Array<any | Type<any> | {
    [k: string]: any;
} | any[]>;

export declare class RuntimeCompilerFactory implements CompilerFactory {
    private _defaultOptions;
    constructor(defaultOptions: CompilerOptions[]);
    createCompiler(options?: CompilerOptions[]): Compiler;
}

I don't know how the COMPILER_PROVIDERS works, and I don't know how to transfer it to a exported function in my module.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Micarle
  • 131
  • 9

1 Answers1

9

The solution is to not use COMPILER_PROVIDERS any more. Also, you do not need to include JitCompiler in your list of providers.

Instead, use JitCompilerFactory from "@angular/compiler". It is not injectable, so just create a new instance of it yourself like this:

private compiler: Compiler = new JitCompilerFactory([{useDebug: false, useJit: true}]).createCompiler();

The rest works as before, for example following Radim Kohler's excellent answer here.

Community
  • 1
  • 1
Sebastian
  • 5,177
  • 4
  • 30
  • 47
  • Thanks a lot Sebastian, that really helped me! – maxbellec Jan 20 '17 at 08:56
  • 2
    Anyone have working JitCompiletFactory with AOT sample ? When I am using it I'am always getting No NgModule metadata found for DynamicHtmlModule – user2771738 Mar 02 '17 at 07:17
  • 2
    Just wanted to add that the shown Compiler comes from @angular/core, not @angular/compiler like the previous JitCompiler and the factory. Took me a while to find it. – Tarmo Apr 05 '17 at 15:00
  • I removed `COMPILER_PROVIDERS` and implemented `JitCompilerFactory`. But that took me while to notice @Tarmo's comment. And yes It starts working for me like a charm. Need to include `Compiler` from `@angular/core`. – The Hungry Dictator Aug 03 '17 at 05:37