The latest release of Angular2 allows for Ahead of time (AOT) compilation, using this code in your app.bootstrap.ts file:
// The browser platform without a compiler
import { platformBrowser } from '@angular/platform-browser';
// The app module factory produced by the static offline compiler
import { AppModuleNgFactory } from './app.module.ngfactory';
// Launch with the app module factory.
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
Angular2 Official Documentation
How can we integrate Webpack and Typescript loaders with Angular2's AOT compiler?
It seems there might not be an option to do so yet, but I'm asking the question on Stack overflow so when it is available, the answer can be easily found.
UPDATE 10/12/16 - I got it working, see my answer below.
tsconfig.json
config/webpack-aot.config.js
I used this webpack config and I did AOT compile with angular2 lazy loading. You can see example app for AOT / JIT compile with angular2 lazy load for production mode and dev mode in this git.
angular2-webpack2-aot
I got it working finally, see my repo Angular2 Webpack2 DotNET Starter
There are several tricks necessary. Note that AOT compilation does not support any
require()
statements in your Angular 2 components. They will need to be converted toimport
statements.First, you need to have a second
tsconfig.json
file, with special options for AOT compilation. I designate this with.aot.json
extension.tsconfig.aot.json :
You'll also need the exact right combination of verions of Angular2.
@angular/core@2.0.2
and@angular/common@2.0.2
did NOT work for me, I had to use2.0.0
for both orngc
failed to compile the AOT files. Here's what I'm using successfully:package.json :
In addition, you'll need a couple nifty webpack loaders, while also allowing webpack to look in the
./src
folder as well as the folder your AOT compiled files are output to. (*.component.ngfactory.ts
)That last part is very important! If you don't tell webpack to include it those folders, it won't work. In this example, the AOT files are output to
/aot-compiled
in the root folder.webpack.common.js
To generate your AOT files, you'll need an NPM script to do it for you
package.json
You'll also need to make your webpack config read the AOT version of
app.bootstrap.ts
- which is different from the JIT version. I differentiate it with.aot.ts
extension so that in production, webpack uses AOT (app.bootstrap.aot.ts
), but in dev mode, it uses JIT withwebpack-dev-server
(app.bootstrap.ts
).Finally, you run
npm run compile:aot
FIRST. Once your AOT files are output to disk, you run your webpack build with eitherwebpack
orwebpack-dev-server
.For a working example, see my repo Angular2 Webpack2 DotNET Starter. It's integrated with .NET Core 1.0, but for those not using .NET, you can still see how Webpack 2 and Angular 2 are configured.