Search code examples
angularangular2-aotangular-compiler-cli

AOT compiler compiles ngfactory files


I'm new to AOT compiler.

I compile all of the files to a 'compiled' directory using this option:

"angularCompilerOptions": {
    "genDir": "compiled",
    "skipMetadataEmit" : true
}

But when I run 'ngc' it tries to also compile the newly created .ngfactory files and throws typing errors on them.

I tried to remove the 'compiled' directory and re-compile the code but it happened again.

I also tried to add "exclude" key in the tsconfig-aot.json file (like I have in typescript compiler) and add the 'compiled' directory but that didn't help.

Any ideas?

==== Edit ====

Some more info that might help, I've tried to set the 'genDir' to a directory outside of the application directory and then I got an error saying that the compiler can't find the module 'app.module.ngfactory'.

From my understanding this means that it tries to compile the '.ngfactory' files because I changed the main.ts from:

import { AppModule } from "./components/app.module";

platformBrowserDynamic().bootstrapModule(AppModule);

to:

import { AppModuleNgFactory  } from "../compiled/client/components/app.module.ngfactory";

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

and that might cause the compiler to compile the app.module.ngfactory and from there it moves to the other imports in 'app.module.ngfactory' and tries to compile all '.ngfactory' files.

Does it make sense? I changed the 'main.ts' file exactly as google shows at: https://angular.io/docs/ts/latest/cookbook/aot-compiler.html


Solution

  • It was my bad.

    The compiler shows errors on the ngfactory files but it does not mean it compiled them.

    The errors are related to the original component's ts file.

    I had to fix the errors in the ts files and it solved the issues.

    It was hard for me to understand that even though the application worked in JIT, the AOT compile things different and is more strict and might show some errors that I need to handle and didn't matter in JIT.