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


the latest release of angular2 allows ahead of time (aot) compilation, using code in app.bootstrap.ts file:

// browser platform without compiler import { platformbrowser } '@angular/platform-browser';  // app module factory produced static offline compiler import { appmodulengfactory } './app.module.ngfactory';  // launch app module factory. platformbrowser().bootstrapmodulefactory(appmodulengfactory); 

angular2 official documentation

how can integrate webpack , typescript loaders angular2's aot compiler?

it seems there might not option yet, i'm asking question on stack overflow when available, answer can found.

update 10/12/16 - got working, see answer below.

i got working finally, see repo angular2 webpack2 dotnet starter

there several tricks necessary. note aot compilation not support require() statements in angular 2 components. need converted import statements.

first, need have second tsconfig.json file, special options aot compilation. designate .aot.json extension.

tsconfig.aot.json :

{    "compileroptions": {       "target": "es5",       "emitdecoratormetadata": true,       "experimentaldecorators": true,       "allowsyntheticdefaultimports": false,       "noemithelpers": true,       "pretty": true,       "strictnullchecks": false,       "baseurl": ".",       "sourcemap": true,       "sourceroot": ".",       "lib": [          "es6",          "dom"       ],       "types": [          "lodash",          "hammerjs",          "jasmine",          "node",          "selenium-webdriver",          "source-map",          "uglify-js",          "webpack",          "materialize-css",          "jquery",          "kendo-ui"       ],       "typeroots": [          "./node_modules/@types"       ],       "outdir": "./compiled/src"    },    "exclude": [       "./node_modules",       "./**/*.e2e.ts",       "./**/*.spec.ts",    ],    "awesometypescriptloaderoptions": {       "usewebpacktext": true,       "forkchecker": true,       "usecache": true    },    "compileonsave": false,    "buildonsave": false,    "atom": {       "rewritetsconfig": false    },    "angularcompileroptions": {       "gendir": "./compiled/aot",       "debug": true    } } 

you'll need exact right combination of verions of angular2. @angular/core@2.0.2 , @angular/common@2.0.2 did not work me, had use 2.0.0 both or ngc failed compile aot files. here's i'm using successfully:

package.json :

  "dependencies": {     "@angular/core": "2.0.0",     "@angular/common": "2.0.0",     "@angular/compiler": "2.0.0",     "@angular/compiler-cli": "0.6.2",     "@angular/forms": "^2.0.1",     "@angular/http": "2.0.0",     "@angular/platform-browser": "2.0.0",     "@angular/platform-browser-dynamic": "2.0.0",     "@angular/platform-server": "2.0.0",     "@angular/router": "3.0.0",     "@angular/tsc-wrapped": "0.3.0" } 

in addition, you'll need couple nifty webpack loaders, while allowing webpack in ./src folder folder aot compiled files output to. (*.component.ngfactory.ts)

that last part important! if don't tell webpack include folders, won't work. in example, aot files output /aot-compiled in root folder.

webpack.common.js

  loaders: [      {         test: /\.ts$/,         include: [helpers.paths.approot, helpers.root('./compiled/aot')],         exclude: [/\.(spec|e2e)\.ts$/],         loaders: [            '@angularclass/hmr-loader',            'awesome-typescript-loader',            'angular2-template-loader',            'angular2-router-loader?loader=system',            "angular2-load-children-loader" // loader replaces loadchildren value work aot/jit         ],      },   ] 

to generate aot files, you'll need npm script you

package.json

   "scripts": {       "compile:aot": "./node_modules/.bin/ngc -p ./tsconfig.aot.json",    } 

you'll need make webpack config read aot version of app.bootstrap.ts - different jit version. differentiate .aot.ts extension in production, webpack uses aot (app.bootstrap.aot.ts), in dev mode, uses jit webpack-dev-server (app.bootstrap.ts).

finally, run npm run compile:aot first. once aot files output disk, run webpack build either webpack or webpack-dev-server.

for working example, see repo angular2 webpack2 dotnet starter. it's integrated .net core 1.0, not using .net, can still see how webpack 2 , angular 2 configured.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -