Webpack, Typescript and Angular2 with Ahead Of Time (AOT) compilation?
NickName:TetraDev Ask DateTime:2016-08-24T05:46:20

Webpack, Typescript and Angular2 with Ahead Of Time (AOT) compilation?

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.

Copyright Notice:Content Author:「TetraDev」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39111198/webpack-typescript-and-angular2-with-ahead-of-time-aot-compilation

Answers
TetraDev 2016-10-12T22:09:39

I got it working finally, see my repo Angular2 Webpack2 DotNET Starter\nThere 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 to import statements.\nFirst, you need to have a second tsconfig.json file, with special options for AOT compilation. I designate this with .aot.json extension.\ntsconfig.aot.json :\n{\n "compilerOptions": {\n "target": "es5",\n "emitDecoratorMetadata": true,\n "experimentalDecorators": true,\n "allowSyntheticDefaultImports": false,\n "noEmitHelpers": true,\n "pretty": true,\n "strictNullChecks": false,\n "baseUrl": ".",\n "sourceMap": true,\n "sourceRoot": ".",\n "lib": [\n "es6",\n "dom"\n ],\n "types": [\n "lodash",\n "hammerjs",\n "jasmine",\n "node",\n "selenium-webdriver",\n "source-map",\n "uglify-js",\n "webpack",\n "materialize-css",\n "jquery",\n "kendo-ui"\n ],\n "typeRoots": [\n "./node_modules/@types"\n ],\n "outDir": "./compiled/src"\n },\n "exclude": [\n "./node_modules",\n "./**/*.e2e.ts",\n "./**/*.spec.ts",\n ],\n "awesomeTypescriptLoaderOptions": {\n "useWebpackText": true,\n "forkChecker": true,\n "useCache": true\n },\n "compileOnSave": false,\n "buildOnSave": false,\n "atom": {\n "rewriteTsconfig": false\n },\n "angularCompilerOptions": {\n "genDir": "./compiled/aot",\n "debug": true\n }\n}\n\nYou'll also need the exact right combination of verions of Angular2. @angular/[email protected] and @angular/[email protected] did NOT work for me, I had to use 2.0.0 for both or ngc failed to compile the AOT files. Here's what I'm using successfully:\npackage.json :\n "dependencies": {\n "@angular/core": "2.0.0",\n "@angular/common": "2.0.0",\n "@angular/compiler": "2.0.0",\n "@angular/compiler-cli": "0.6.2",\n "@angular/forms": "^2.0.1",\n "@angular/http": "2.0.0",\n "@angular/platform-browser": "2.0.0",\n "@angular/platform-browser-dynamic": "2.0.0",\n "@angular/platform-server": "2.0.0",\n "@angular/router": "3.0.0",\n "@angular/tsc-wrapped": "0.3.0"\n}\n\nIn 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)\nThat 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.\nwebpack.common.js\n loaders: [\n {\n test: /\\.ts$/,\n include: [helpers.paths.appRoot, helpers.root('./compiled/aot')],\n exclude: [/\\.(spec|e2e)\\.ts$/],\n loaders: [\n '@angularclass/hmr-loader',\n 'awesome-typescript-loader',\n 'angular2-template-loader',\n 'angular2-router-loader?loader=system',\n "angular2-load-children-loader" // this loader replaces loadChildren value to work with AOT/JIT\n ],\n },\n ]\n\nTo generate your AOT files, you'll need an NPM script to do it for you\npackage.json\n "scripts": {\n "compile:aot": "./node_modules/.bin/ngc -p ./tsconfig.aot.json",\n }\n\nYou'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 with webpack-dev-server (app.bootstrap.ts).\nFinally, you run npm run compile:aot FIRST.\nOnce your AOT files are output to disk, you run your webpack build with either webpack or webpack-dev-server.\nFor 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.",


More about “Webpack, Typescript and Angular2 with Ahead Of Time (AOT) compilation?” related questions

Webpack, Typescript and Angular2 with Ahead Of Time (AOT) compilation?

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 '@

Show Detail

Angular2 SystemJs, Ahead of Time Compilation with Gulp

Can anyone please guide how to use Gulp for Ahead of Time compilation of a Angular2 project that uses SystemJs. I mean to automate all steps mentioned in https://angular.io/docs/ts/latest/cookbook/...

Show Detail

What is AoT(or Ahead-of-Time Compilation) in Angular2?

Ahead-of-Time Compilation(or AoT) is a feature that is provided in Angular2. But I could not find good explanation on official site about it. Could somebody make a clear definition of it?

Show Detail

Is aot (ahead of time) available for mono for android?

I have found this: Is AOT (ahead of time) compilation available (or planned) in mono for android? However this question is old. At mono project page it I see ARM is supported for AOT but nothing...

Show Detail

AOT (Ahead-Of-Time) Compilation with ES5

Is it possible to do AOT compilation in Angular using only ES5? More to the point, can I use the NGTools Webpack plugin with ES5? I know TypeScript is the preferable language of choice for Angular,

Show Detail

Angular2 seed project aot compilation no ngfactory files generated

I have created an angular2 aot seed project based on the official tutorials, but ngc compilation does not generate ngfactory files. Could you help me what can be the reason? Project site: https://

Show Detail

main-aot is missing from the typeScript compilation

I am trying to implement ngc compilation config on my angular/webpack app. I have a tsConfig-aot.json in my root folder like below. { .... "files": [ "src/app/app.module.ts", "src/app...

Show Detail

Vue 2 AOT ahead of time compilation

I'm trying to understand Vue. Coming from angular2, I wanted to know if this documentation from Vuejs 2 is equivalent to angular2's AOT: When using vue-loader or vueify, templates inside *.vue f...

Show Detail

Angular2 AOT Compilation with static external js file

Directed by the answer to my question about Server-side rendering, I'm looking at implementing AOT compilation in my Angular2 app. A bit of background to the problem I'm having: we have a build ru...

Show Detail

Ahead of Time Compilation for Cassandra

I have been trying to find a way to achieve Ahead of Time compilation for the Cassandra service (Which does some JIT Compilation by default). I see that Cassandra has been written in Java and it is...

Show Detail