Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for AoT compilation #1732

Closed
elvirdolic opened this issue Aug 17, 2016 · 41 comments
Closed

Support for AoT compilation #1732

elvirdolic opened this issue Aug 17, 2016 · 41 comments
Assignees
Labels
feature Issue that requests a new feature P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@elvirdolic
Copy link

Are there any plans to support AoT compilation in angular-cli?

@deebloo
Copy link
Contributor

deebloo commented Aug 17, 2016

@elvirdolic i believe AoT compilation is a perquisite to declaring an 1.0 release. So while it is not there right now it will be there as we approach an official release of angular2

@filipesilva filipesilva added feature Issue that requests a new feature command: build P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent labels Aug 23, 2016
@filipesilva
Copy link
Contributor

We definitely plan on having it, but I don't have a timeline to give. It's a big thing on our radar though.

@deebloo
Copy link
Contributor

deebloo commented Aug 28, 2016

@elvirdolic just an fyi I have gotten an angular-cli build working with AOT compilation so it is possible to roll it yourself for now if you want to play around with it.

@aminebizid
Copy link

@deebloo please provide us the steps to make AOT on cli webpack

@razvanmitre
Copy link

@deebloo it would be very useful to me too, and I am sure also to other people. Thanks!

@ghost ghost mentioned this issue Sep 7, 2016
@monfa-red
Copy link

Any luck with AOT?
We're using Angular2 in production, is there a hack to make it work with angular-cli@1.0.0-beta.14?

@ghost
Copy link

ghost commented Sep 15, 2016

Yeah also interested.
Is this possible with the "old" systemjs angular-cli? As I can't upgrade to webpack due to some other issues.

@elvirdolic
Copy link
Author

This repo provides some hacks. Check it out

https://github.com/brandonroberts/router-cli-aot/blob/master/README.md

@aminebizid
Copy link

@elvirdolic this is not webpack

@hansl hansl added this to the RC1 milestone Sep 19, 2016
@hansl hansl assigned hansl and unassigned robwormald Sep 26, 2016
@castamir
Copy link

castamir commented Oct 1, 2016

I've just played with angular-cli all day and I must say that it is AWESOME 💯 ! But I miss the AoT compilation thu. Is there any progress? When can I expect a working prototype? Is it a matter of days or rather weeks? Thanks for your answer.

@elvirdolic
Copy link
Author

Yes in the latest version you can try ng build --prod --aot. But its not officially described in the docs.

@castamir
Copy link

castamir commented Oct 2, 2016

@elvirdolic 👍

I have a question... what is actually happen, when I add --aot flag? In angular.io docs, there is a tutorial for AoT compilation that includes 2 changes, one in tsconfig (for ngc) and second in main.ts (replace platform-browser-dynamic with platform-browser). If I do these changes, build with --aot stop working. If I keep them without any change, --aot works but I think that it still keeps platform-browser-dynamic in the bundle and loads JiT... isn't it?

edit: and if changes in tsconfig.json and main.ts are required, how can I change config for dev and production to use e.g. main.ts or main.aot.ts respectively?

@elvirdolic
Copy link
Author

I think you don't have to change anything. This is the magic of angular-cli you don't have to worry about that. Use the flag and the rest will be done in the background. Aot is still in WIP. Lets wait for a official relase statement maybe there is some config change required.

@k1ng440
Copy link

k1ng440 commented Oct 2, 2016

i am sorry but what is AoT?

@hansl
Copy link
Contributor

hansl commented Oct 2, 2016

@elvirdolic: correct, there is NO config change needed for AoT, and we're making sure that it's backward compatible with your project. Some extreme cases might confuse our tooling so it will error out, but this should only happen if do strange things (such as declaring your classes but exporting them by another name, for example). If you follow the blueprints, and even if you do some code yourself, it should work just fine.

@k1ng440: AoT is ahead-of-time compilation. Look up "Angular 2 AoT" on youtube to see what's it about.

@jordic
Copy link

jordic commented Oct 3, 2016

@hansl is this feature usuable?
Should I expect to run ng serve --aot and all is working? Or should I do something else?

@castamir
Copy link

castamir commented Oct 3, 2016

@jordic ng build --prod --aot and ng serve --prod --aot works in beta.16, but there are still some issues to solve (like infinite server rebuild in ng serve --aot in development mode)

@zackarychapple
Copy link
Contributor

@castamir didn't see the note the mention about infinite rebuild, was too excited about having AOT. Here is the issue for anyone curious #2418

@Delagen
Copy link

Delagen commented Oct 5, 2016

@castamir not works aot with lazy routes

@jordic
Copy link

jordic commented Oct 5, 2016

Ok @castamir. Not sure if this is the place to ask this, but when I launch ng serve --aot I've got this error:

Error encountered resolving symbol values statically. Reference to a non-exported function (position 17:10 in the original .ts file), resolving symbol MemoryBoardComponent

Are there any step I missed, or there is something in my codebase not properly defined/structured? May someone give and advice?

@dudko-av
Copy link

dudko-av commented Oct 5, 2016

@jordic please, try this https://medium.com/@isaacplmann/making-your-angular-2-library-statically-analyzable-for-aot-e1c6f3ebedd5#.l0sjpuwrx

@sebastian-heinz
Copy link

Hello i encountered an issue:

Error encountered resolving symbol values statically. Could not resolve type Window (position 11:39 in the original .ts file), resolving symbol

the window is from lib.d.ts as far as i can tell, this is my tsconfig

  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]

this is the relevant part from my .ts file

@Injectable()
export class SwService {

  private window: Window;

  constructor(@Inject(Window) window: Window) {
    this.window = window;
...

if i only change the Window-type to any inside the constructor parameters it works

@Injectable()
export class SwService {

  private window: Window;

  constructor(@Inject(Window) window: any) {
    this.window = window;

in my module.ts file i have following


  providers: [
    {provide: Window, useValue: window},
...

Any ideas why Window cannot be statically analyzed?

@castamir
Copy link

castamir commented Oct 6, 2016

@sebastian-heinz what does your Window type look like?

this might help

@sebastian-heinz
Copy link

sebastian-heinz commented Oct 7, 2016

@castamir thank you for the headsup, i checked the article, but i still can not wrap my head around how to import the Window-Type from lib.d.ts properly.

if i follow the decleration of Window i get to

lib.d.ts

declare var Window: {
    prototype: Window;
    new(): Window;
}

so i tried to import it: (Before i did not had any import of Window, and the IDE would resolve it to the lib.d.ts file)

import {Window} from "typescript/lib/lib";

but this wont work as its not a module?

s/@angular/router/src/router_state.jsModuleNotFoundError: Module not found: Error: Can't resolve 'typescript/lib/lib' 

Im not sure how to do this, or if its necessary since it work with the any-Type

@filipesilva
Copy link
Contributor

As an update, beta.16 had some support for --aot on ng serve/build, and beta.17 builds on that support. There might still be bugs, which is why we haven't really documented it yet.

@jmsegrev
Copy link

jmsegrev commented Oct 20, 2016

This pr #2470 added support for tsconfig baseUrl, with this you can use absolute path in your imports. It was part of beta.17 release, but in the pr, the autor commented that it did not work with --aot. . I migrated from AngularClass/angular2-webpack-starter, there I had an alias in webpack config, so all my code used absolute path in the imports. It would be great if the aot worked for this cases.

@monfa-red
Copy link

ng build --prod --aot still doesn't generate lazy chunks on 1.0.0-beta.19-3
It's been more than a month that AOT is in you milestones
Should I go try an angular-cli alternative?

@Meligy
Copy link
Contributor

Meligy commented Nov 6, 2016

@monfa-red allow me to give some non-contributor thoughts about your answer.

  • beta.19-3 does not support lazy loading in AoT. This is tracked by Routing not working with AoT #2735, and has a supposed fix in master already (I haven't personally tested it yet). The fix should be available in the next release.

  • Regarding the "more than a month", I won't go into reminding myself that we don't owe those guys anything, etc., but an interesting observation of the commit log makes me think most of the team is working on other things with higher priorities. This is a guess, not information.

  • Should I go try an angular-cli alternative?

    I'd suggest hanging around and seeing if next beta fixes it (or testing master). But if this doesn't work, it becomes your personal choice.

    If a project seed solves your problems better, you can switch to it, and check on CLI in later time. Whether you should, is a decision only you know enough to make.

    This differs from one project to another. For my project, I care about route and component generators to help newbie team more than I care about AoT in the short term so I'm sticking with the CLI and OK for now with what non-AoT prod build gives me. The decision for you depends on the different parameters your project have and it's definitely going to be a trade-off among various parameters.

    One thing worth mentioning is that Angular, the framework and its team, support people using all kinds of seeds. They don't say "either CLI or don't talk to us, report issues, etc". The only situation I know of that's like that, is automatic code change upgrade that migrates breaking changes in Angular 3, which is planned to be in the CLI. But neither Angular 3 nor this support have even started, so, not a thing to worry about for now!

@hansl hansl modified the milestone: RC1 Nov 11, 2016
@Meligy
Copy link
Contributor

Meligy commented Nov 24, 2016

OK. I tried this in beta.21.
https://github.com/Meligy/routing-angular-cli/tree/master

Still having issues with AoT.

ng build --aot

> ng b 10% building modules 5/6 modules 1 active ...de_modules/css-loader/lib/css-base.jsentry.split is not a function                                                                             
TypeError: entry.split is not a function
    at Function.ModuleRoute.fromString (/Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:24:27)
    at /Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:244:34
    at Array.map (native)
    at AotPlugin._processNgModule (/Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:243:14)
    at /Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:217:39

ng build -prod --aot

> ng build -prod --aot                          10% building modules 3/4 modules 1 active ...de_modules/css-loader/lib/css-base.jsentry.split is not a function                                    
TypeError: entry.split is not a function
    at Function.ModuleRoute.fromString (/Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:24:27)
    at /Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:244:34
    at Array.map (native)
    at AotPlugin._processNgModule (/Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:243:14)
    at /Users/Meligy/Code/github/Meligy/routing-angular-cli/node_modules/@ngtools/webpack/src/plugin.js:217:39

ng serve --aot

This one only hangs with the following output

> ng serve --aot 
** NG Live Development Server is running on http://localhost:4200. **                   10% building modules 7/11 modules 4 active ...i/node_modules/webpack/hot/emitter.js(node:205 20% building modules 84/84 modules 0 active         

Remarks

CLI Version

This is tested with angular-cli beta.21 on macOS Sierra with Node v6.9.1 and NPM 4.0.2.

Sample Details

This is the following relevant app-routing.module.ts file:

import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';

import {SimpleRouteComponent} from './simple-route/simple-route.component';
import {BundledModule} from './bundled/bundled.module';

export function loadBundledModule() { return BundledModule; }

export const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    component: SimpleRouteComponent
  },
  {
    path: 'bundled',
    loadChildren: loadBundledModule
  },
  {
    path: 'lazy',
    loadChildren: './lazy/lazy.module#LazyModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: []
})
export class AppRoutingModule { }

The AoT commands above only work when I comment the 2nd and 3rd routes, changing the definition of routes to be:

export const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    component: SimpleRouteComponent
  },
  // {
  //   path: 'bundled',
  //   loadChildren: loadBundledModule
  // },
  // {
  //   path: 'lazy',
  //   loadChildren: './lazy/lazy.module#LazyModule'
  // }
];

Note that this means that even loading other modules directly (not with lazy loading) is also not working with AoT, not just lazy loading.

Update

Coping @hansl because he was the last one I knew of working on this.

@Epotignano
Copy link

@Meligy I'm having exactly the same issue here

@jwuliger
Copy link

This is the same issue I am having and trying to work through. I am using beta 22-1 and the latest beta 23 @master but I cannot get lazy loading to work at all. I have been at this for hours... It does work in beta 21.

@barinovos
Copy link

Hmm.. I am using beta.24 and after running ng build -prod --aot I see no errors, build is completed, but... there is no AoT! main.*.bundle.js still contains all HTML templates and vendor file also contains Compiler code... So actually works completely without any AoT, just like a usual JIT...

@ryzy
Copy link

ryzy commented Dec 21, 2016

FYI, I just upgraded to angular-cli beta24 and Angular 2.4.0 on my test project https://github.com/ryzy/play-ng2-github/ and AoT seems to work perfectly fine. It loads in ~.5s compared to ~1.5s without AoT.

@hansl
Copy link
Contributor

hansl commented Dec 21, 2016

Closing this as obsolete. We've been supporting AOT for a while; bugs should have their own issues in GitHub from now on.

@hansl hansl closed this as completed Dec 21, 2016
@hansl
Copy link
Contributor

hansl commented Dec 21, 2016

@ryzy we had people request that we don't remove decorators. It is using AOT and the Angular compiler isn't included, but the decorators are still there. Working towards the real solution.

@prabh08
Copy link

prabh08 commented Jan 27, 2017

I often use AOT in my angular 2 app. But meanwhile I am facing the issue that as it gives the "Property 'staff_name' does not exist on type 'StaffRentListComponent' in my previous files history. Means it shows deleted code in ngfactory.ts (in AOT bundle folder) file and there is no code in my actual .ts file. Please let me know that how can I resolve this issue.

@ShadowManu
Copy link

@prabh08 This is a topic for another issue. Even more, since it seems a support request, a better fit would be StackOverflow (or gitter, which great people are normally active))

@MasthanRao
Copy link

@ryzy I tried downloading your github project for verifying AoT speed test. But somehow npm install doesnt work because of dependencies. Can you please let me know how do we verify Aot speed test? Do I need to replace platform-browser-dynamic with platform-browser in main.ts file?

@Rolice
Copy link

Rolice commented Mar 24, 2017

It seems that with AOT compilation, especially with Angular CLI, there is no window at the build time. I ended up with an option to implement a separate Window Service, which returns a window instance, to operate with.

Reference: http://stackoverflow.com/questions/34177221/angular2-how-to-inject-window-into-an-angular2-service

PS: Refering to the examples in the SO question above, you can directly implement the getWindow function inside the get property and make it static, i.e.:

import {Injectable} from "@angular/core";

@Injectable()
export class WindowService {
    static get window(): any {
        return window;
    }
}

@Davy-F
Copy link

Davy-F commented Jun 16, 2017

Is the bs-config.aot.ts file now deprecated / not required for aot to work? It's not mentioned in the documentation, but other websites make a reference to it. Nor is it included in the angular-cli.

@Rolice I know that the bs-config.aot.ts file (if is does get used) has an "open": false property. Setting this to true would open a window at build time.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Issue that requests a new feature P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests