5

I've been stuck trying to get AOT working with my Webpack app. (@ngtools/webpack)

Can anyone help me at all?

It appears to work, however, compiler.js is still included in the bundle. Also it is still looking for all of my .html files and getting 404 on all of the component templates.

So as far as I can tell it's not really doing the best parts of the AOT?

Can any shine any light here, I'm stuck!!

Thank you!!

JBoothUA
  • 3,040
  • 3
  • 17
  • 46

3 Answers3

4

(I won't tell you how long it took me to get this working.) First of all, are you using Angular 4.x or 5.x? If 4.x you need to use the AotPlugin, if 5.x then use the AngularCompilerPlugin. Also if you are using 5.0 then the compiler is probably finding some errors in your templates that weren't a problem before. Those need to be corrected before it will work. It was a long time even before I could see the errors, I think because of issues in my tsconfig.json file. So make sure that is correct, and is being pointed to correctly by your AngularCompilerPlugin options. Some examples have it in your source directory, I found it was simpler to keep it in the root directory (same as webpack.config.js) and refer to it like this in the AngularCompilerPlugin options:

   tsConfigPath: "./tsconfig.ngtools.json",

Here is a link to the possible plugin options

https://www.npmjs.com/package/@ngtools/webpack

and here are the tsconfig options that worked for me:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist.ngtools",
    "baseUrl": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "module": "es2015",
    "listEmittedFiles": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "files": [
    "./src/app/app.module.ts", /* this includes all dependents */
    "src/polyfills.ts",
    "src/main.ts"
  ]
}

Also note that you don't need or want a separate main.aot.ts file when using ngtools, it will automatically adjust the platform-browser-dynamic version to platform-browser at compile time. Also with ngtools you don't need to reference the AppModuleNgFactory component, this is also handled automatically.

I found it less than helpful that ngtools doesn't output intermediate files to the file system, so debugging is more difficult, but once things are working it is nice to not have to deal with the intermediate files.

Good luck!

ckapilla
  • 1,148
  • 13
  • 25
  • i moved back to 4.1.3 and AotPlugin got a little further. now i am seeing the correct looking bundles. however, i'm still having problems with mkdirp getting caught in infinite loops if I use --optimize-minimize. – JBoothUA Nov 17 '17 at 21:13
  • also if i comment out my file-loader for my images i also get the same infinite loop. have you ever seen this? it has to do with using the absolute path in place of a relative path – JBoothUA Nov 17 '17 at 21:14
  • @ckapilla, do you have any references about "you don't need or want a separate main.aot.ts file when using ngtools, it will automatically adjust the platform-browser-dynamic version to platform-browser at compile time"? This seems to have fixed my problem but it's the first time I've heard about this change. – Coderer Nov 21 '17 at 14:15
  • Also, though this appears to build correctly, I no longer actually have a directory in my filesystem matching the `genDir` param in my `tsconfig.aot.json` -- I swear that this used to be created and populated by AotPlugin (in 4.x). Is this now entirely in-memory, like how the devserver works? If so, that's another big change I never saw mentioned anywhere that made it harder to troubleshoot... – Coderer Nov 21 '17 at 14:19
  • it took me like half a week, but I finally got things on track! thanks for your help!! – JBoothUA Nov 21 '17 at 15:41
  • i'm not sure how, but I'm no longer getting in the infinite loop when I run with --optimize-minimize.. However, with that flag I am now getting this:ERROR in Error: Template parse errors: Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.) It is complaining about an [ngClass]={....} Can you think of any reason that that flag would cause these issues? I definitely want to be able to minimize the code. – JBoothUA Nov 21 '17 at 15:55
  • Without that flag I can finally see the AoT working, and I also noticed that last night, it seems like __genDir is no longer in the bundle with the AngularCompilerPlugin like it was with the AotPlugin. – JBoothUA Nov 21 '17 at 15:57
  • i also seemed to have lost the ability to name my lazy loaded chunks with ?chunkName= but that is pretty minor. mostly just glad to be unstuck after so long! – JBoothUA Nov 21 '17 at 15:59
1

I was able to make a sample angular5 app build in AOT

https://github.com/tomalex0/angularx-aot-jit

I created a sample angular5 app which generates AOT and JIT, might not be in same structure as yours but works

https://github.com/tomalex0/angularx-aot-jit

This commit difference will give better picture of what all i changed while updating to angular5 https://github.com/tomalex0/angularx-aot-jit/commit/1435fddf1a6336f05e63f30062cb4cd2d0ba771f

tsconfig-aot.json for angular5

{
  "compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "mapRoot": "",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2015",
      "dom"
    ],
    "outDir": "aot",
    "skipLibCheck": true,
    "rootDir": "."
  }
}
  1. No longer needs "angularCompilerOptions": {"genDir": "aot" }|
  2. in webpack config make entry as entry: './js/ng2/app/main.jit.ts'
  3. in webpack const { AngularCompilerPlugin } = require('@ngtools/webpack'); and in plugins as new AngularCompilerPlugin({tsConfigPath: './tsconfig-aot.json', entryModule: ...})
tomalex
  • 1,233
  • 6
  • 17
  • 40
0

If a single file is handled incorrectly OR not loaded at all, you'll get an infinite loop on 95% emitting.

UPDATE:: It appears that there is one area where errors will get swallowed up:

reject(new Error('Child compilation failed:\n' + errorDetails));

That is line 57 of @ngtools\webpack\src\resource_loader.js You'll want to put a breakpoint to debug or output that error details, or you'll be stuck on AOT compiling forever.

Just going to leave this in case someone is stuck in the future, basically if you get stuck in an infinite loop you're probably on the right track..

I finally have something working, there were so many issues along the way..

The main thing that bit me that I didn't understand is, I guess, that for AoT, ALL YOUR FILES have to be loaded into webpack

This is the module.rules config that finally got me running...

// Ahead of Time Compilation
[{ test: /\.ts$/, loader: '@ngtools/webpack', exclude: [/\.(spec)\.ts$/] },
// AoT requires all files to be loaded
{ test: /\.html$/, loader: 'html-loader' },
{ test: /\.css$/, loader: 'raw-loader' },
{
    test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
    loader: 'file-loader', options: { name: '[path][name].[ext]' }
}]

I added this loader which basically just puts my images back into the exact same place they exist.. But I guess that causes them to be packaged correctly..

If I do not include a file type (or if there is some sort of error while loading a file see comment below) then I get caught in an infinite loop...

If you run webpack with --progress --profile you will see that it gets stuck on 95% emitting. What's going on behind the scenes is some sort of absolute path vs relative path issue and it gets caught in a loop in mkdirp.

But I guess the trick is for AoT, is to just make sure that you have a loader for EVERY file type that you are using.

JBoothUA
  • 3,040
  • 3
  • 17
  • 46
  • Even worse news... I found that one of my templates had a src=text{{variable}}text and that also caused the same infinite loop.. With no good error.. The line of code was just added or it would have been impossible to find. – JBoothUA Nov 21 '17 at 17:45