5

When I run my Angular 2 app using JIT everything works fine. But after I switch to AOT my app starts pulling links with additional prefix ("/src/linker/ng_module_factory") for example:

JIT pulls: http://localhost:8080/node_modules/@angular/core/bundles/core.umd.js

AOT pulls: http://localhost:8080/node_modules/@angular/core/bundles/core.umd.js/src/linker/ng_module_factory Where did postfix /src/linker/ng_module_factory come from?

About app. I'm using Java 8 + Spring 4.3.2.RELEASE + Angular 2 + Tomcat 9.0.0.M6.

src
--main
----webapp
------resources
--------app
----------app.component.css
----------app.component.ts
----------app.module.ts
----------main.ts
--------index.html
--------package.json
--------systemjs.config.js
--------tsconfig.json
--------typings.json
------WEB-INF
--------dispatcher-servlet.xml
--------web.xml

package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "typings install"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/common":  "2.0.0",
    "@angular/compiler":  "2.0.0",
    "@angular/core":  "2.0.0",
    "@angular/platform-browser":  "2.0.0",
    "@angular/platform-browser-dynamic":  "2.0.0",

    "systemjs": "0.19.27",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.12",
    "zone.js": "^0.6.23",

    "bootstrap": "^3.3.6"
  },
  "devDependencies": {
    "typescript": "^2.0.2",
    "typings": "^1.0.4"
  },
  "repository": {}
}

index.html

<!DOCTYPE html>
<html>
<head>
  <base href="/">
  <title>MyApp</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="node_modules/core-js/client/shim.min.js"></script>
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>

  <script src="systemjs.config.js"></script>
  <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
</head>

<body>
<my-app>Loading...</my-app>
</body>
</html>

systemjs.config.js

(function (global) {
  System.config({
    paths: {
      'npm:': 'node_modules/'
    },
    map: {
      app: 'app',

      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',

      'rxjs': 'npm:rxjs',
    },
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

typings.json

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759"
  }
}

app.component.ts

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

@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
  `,
  styleUrls: ['app.component.css']
})
export class AppComponent{
    title = 'Title';
}

app.module.ts

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

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

JIT configuration main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);

Next step is compilation using command tsc, building war and deploy.

AOT configuration main.ts

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

Next step is installing compiler npm install @angular/compiler-cli, compilation using command'./node_modules/.bin/ngc', building war and deploy. After ngc command I get files *.ngfactory.ts, *.ngfactory.js, *.metadata.json, *.shim.ts, *.shim.js

vladbrk
  • 93
  • 10
  • did you ever get an answer to this? seeing the same thing my side – Sean Landsman Nov 02 '16 at 10:33
  • I have found answer why angular add prefix - it depends on option "module" in tsconfig.json. It can be "commonjs" or "es2015". Currently I faced new issues "Traceur not found : XHR (404 not found) error." Researching this topic turned out it well-known problem and it look like some problem in Angular 2. We should wait new version and continue researching topic regarding aot. Read this topic https://github.com/angular/angular/issues/11358 maybe you will find something new. – vladbrk Nov 13 '16 at 12:30
  • Also after update Angular2 from 2.0.0 to 2.1.1 some critical bugs have been fixed. But 2.1.2 is unstable, ther is problems with [attr] is null – vladbrk Nov 13 '16 at 12:32
  • thanks - very helpful – Sean Landsman Nov 13 '16 at 15:15

1 Answers1

0

Use angular2-webpack-starter and don't worry

https://github.com/AngularClass/angular2-webpack-starter

vladbrk
  • 93
  • 10