2

UPDATE

I am still wokring on this but now the ngc cli does not give me any errors. However it still does not make my NGFactory files. I have updated eveything to the lastest versions.

tsconfig.js

{
 "compilerOptions": {
   "target": "es5",
   "module": "es2015",
   "moduleResolution": "node",
   "sourceMap": false,
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
   "lib": ["es2015", "dom"],
   "noImplicitAny": true,
   "suppressImplicitAnyIndexErrors": true
 },

 "files": [
   "./typescript/modules/app.module.ts"
   // "./typescript/main.ts" <- see (1)
 ],

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

(1) - revoeing this file, which bootstraps my A2 app, and therefore points to my NgFactory file, means that ngc now runs without any errors.

However it seems to read my app.module file, and make a node_modules file and make lots of factory files for A2 itselfs. But my code, my app.module.ngfactory is not in the 'aot' folder that my tsconfig is pointing to!

Please help!

I am very new to Angular 2, and have been playing around with using webpack to build a bundle file for me. However, this file is over 3mb. So I thought I would look into using System JS (I ask a question about that).

But while looking to that, I found out why my bundle files where so big, as I was using the JIT compiler method to build the code. So I have been trying to get my simple little app to work (starter app from the Anagular site) to work using the AOH compiler to build. But I can not get it to work.

This is my ts config code:

tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2015", "dom"],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },

  "files": [
    "./typescript/modules/app.module.ts",
    "./typescript/main.ts"
  ],

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

My Component code I added the moduleId into the @Component and the window.module = 'aot'; setting into my html file that loads the app. My Component code:

app.components.ts

 import { Component } from '@angular/core';

 @Component({
   moduleId: module.id,
   selector: 'my-app',
   template: `<h1>Hello {{name}}</h1>`
 })

 export class AppComponent { name = 'Angular'; } 

This is my current app module code, unchanged form the example I have used.

app.module.ts

 import { NgModule }      from '@angular/core';
 import { BrowserModule } from '@angular/platform-browser';
 import { AppComponent }  from '../components/app.component';

 @NgModule({
   imports:      [ BrowserModule ],
   declarations: [ AppComponent ],
   bootstrap:    [ AppComponent ]
 })

 export class AppModule { }

Now my main.ts file, this is what seems to be giving me my problems. I have changed the import statment to use the NgFactory, however that does not work at all!

Main.ts

 import { platformBrowser }    from '@angular/platform-browser';
 import { AppModuleNgFactory } from '../aot/app/app.module.ngfactory'; (1)

 platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

 (1) - this file is not generted at all!

So when I run node_modules/.bin/ngc (running in a Ubuntu 16.04 VM), I get the following error:

 Error at /var/www/typescript/main.ts:10:36: 
 Cannot find module './modules/app.ngfactory'. <-- ????????

And yes I have installed @angular/compiler-cli & @angular/platform-server

And yes I did found this question, Angular 2 - Ahead-of-time compilation how to - But when I change my main.ts file to point to another folder, I get the same error....

So what am I doing wrong?

I am also not sure how this plays into building a bundle file with webpack let, has I could not get this to work, I have not gotten that far, but any help would be most welcome.

Thanks.

Community
  • 1
  • 1
C0ol_Cod3r
  • 909
  • 2
  • 15
  • 35

2 Answers2

2

Are you sure that is your correct file path to app.module.ngfactory? I had to copy the path from app.module.ngfactory to get my main.ts to work and it ended up just being the following:

import { AppModuleNgFactory } from './app.module.ngfactory';

Also I just saw this from your post:

(1) - this file is not generted at all!

My app.module.ngfactory wasn't generating because I did not have the right version of typescript. I changed to the following in my package.json file and it was generated:

"awesome-typescript-loader": "3.0.0-beta.17",
"typescript": "2.0.3"
Erica Grant
  • 245
  • 4
  • 12
  • My files are pointing to the right place and the version of typescript is newer than yours @ 2.1.4 and I am using the same version of awesome-typescript-loader. I have gone back to using JIT for now, so that I can learn A2. I am sure that its not A2 but something wrong with my webpack or general config – C0ol_Cod3r Dec 26 '16 at 03:59
  • I have tried a number of different things but my ngfactory file is just missing! And webpack just nto build the file, because its missing - there is something wrong that I am doing but I can not see what it dam well is! – C0ol_Cod3r Dec 31 '16 at 19:50
  • This might not be helpful but I'm not sure if 2.1.4 is stable which is why I stuck with 2.0.3, it has consistently been building ngfactory files for me. – Erica Grant Jan 10 '17 at 18:02
1

This might not be a complete answer but it does, from the limited testing I have done, seems to be working.

I can across this git hub A2 starter project, https://github.com/jbalbes/angular2-aot-webpack. I pull it down into a working test folder and it worked, ngc command created the projects .ngfactory file.

However, this project is using an older version of A2. So I did some testing, I have updated the A2 dependencies in the package.json to 2.4.1. After which it was still working. So I have added in all my packages, thinking that one or more of then has some sort of problem with the ngc complier.

However, nothing seemed to sort it from working, untill I added typescript, to 2.1.4 - then it did not produce any ngfactory files.

Please let me know if any one else is have the some issue with AOT and typescript 2.1.4 (I have also try 2.1.1 and that just gave lots of errors)

C0ol_Cod3r
  • 909
  • 2
  • 15
  • 35