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

initial work for Aot support #13

Merged
merged 4 commits into from
Jan 11, 2017
Merged

Conversation

aitboudad
Copy link
Contributor

No description provided.

function makeProviders(module: StatesModule, forRoot: boolean): Provider[] {
let providers: Provider[] = [module.configClass]
.filter(identity)
.map(configClass => ({ provide: configClass, useClass: configClass }));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way to provide conditional service in AOT, users must define configClass manually

Before:

@NgModule({
  imports: [
    UIRouterModule.forRoot({ configClass: MyUIRouterConfig }),
  ],
})
export class AppModule {}

After:

@NgModule({
  imports: [
    UIRouterModule.forRoot({ configClass: MyUIRouterConfig }),
  ],
  providers: [
     { provide: MyUIRouterConfig, useClass: MyUIRouterConfig }
  ],
})
export class AppModule {}

@kolkov
Copy link

kolkov commented Dec 12, 2016

I try to build this...
image

@aitboudad
Copy link
Contributor Author

@kolkov make sure you are using ui-router-core v1.0

@kolkov
Copy link

kolkov commented Dec 12, 2016

Thanks @aitboudad, but where can I find v1.0?
I use this instructions:
https://github.com/ui-router/ng2/blob/master/CONTRIBUTING.md

@aitboudad
Copy link
Contributor Author

cd ui-router-core
git checkout 1.0.1 -b 1.0

@kolkov
Copy link

kolkov commented Dec 12, 2016

Thanks! Done!
image

@kolkov
Copy link

kolkov commented Dec 12, 2016

I try to build my test app with this builded version:

// Imports for loading & configuring the in-memory web api
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';

import {Ng2StateDeclaration, UIRouterModule, UIView, Category, trace} from "ui-router-ng2";
import {NgModule, SystemJsNgModuleLoader, NgModuleFactoryLoader} from "@angular/core";
import {BrowserModule} from '@angular/platform-browser';

//import {MyRootUIRouterConfig} from "./router.config";
import {AppComponent} from "./app.component";
//import {PublicModule} from "./public/public.module";
//import {PrivateModule} from "./private/private.module";
import {MyUIRouterConfig} from "./core/router.config";
import {CoreModule} from "./core/core.module";

export let MAIN_STATES: Ng2StateDeclaration[] = [
  // The top-level app state.
  // This state fills the root <ui-view></ui-view> (defined in index.html) with the AppComponent
  { name: 'app', component: AppComponent }
];

// Enables tracing (check the console) of:
// - TRANSITION transition start, redirect, success, error, ignored
// - VIEWCONFIG ui-view component creation/destruction and viewconfig de/activation
//trace.enable(Category.TRANSITION, Category.VIEWCONFIG);

@NgModule({
  imports: [
    BrowserModule,
    UIRouterModule.forRoot({
      //states: MAIN_STATES,
      otherwise: { state: 'public.home', params: {} },
      useHash: false,
      configClass: MyUIRouterConfig
    }),
    CoreModule,
    //PublicModule,
    //PrivateModule
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    // Provide a NgModule lazy loading strategy
    { provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader },
    { provide: MyUIRouterConfig, useClass: MyUIRouterConfig }
  ],
  bootstrap: [ UIView ]
})
export class AppModule {}

image

Is it ui-router error or not?

@aitboudad
Copy link
Contributor Author

not sure if it's related, currently I'm using the typescript version not the build one and it's work as expected.

@kolkov
Copy link

kolkov commented Dec 12, 2016

No, I reinstall ui-router-ng2-quickstart and error disappeared.
All work fine! Thanks!
Now I want to try ng build wit AoT...

@kolkov
Copy link

kolkov commented Dec 12, 2016

I can not check the compatibility ((
My angular-cli not working today. Yesterday all is ok, but today nothing helped (
I tried all combination of versions, deinstalling all local and global node_modules etc., but nothing good happened (((
image

@kolkov
Copy link

kolkov commented Dec 13, 2016

I have found what causes this problem in my case:
If I understand correctly, they filter out the character ! from path, but ! is legal character in Windows path.
angular/angular-cli#3529 (comment)

@kolkov
Copy link

kolkov commented Dec 13, 2016

I try to build simple app from angular-cli starter.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

import { UIRouterModule } from 'ui-router-ng2';
import {MyRootUIRouterConfig} from "./router.config";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    UIRouterModule.forRoot({
      otherwise: { state: 'app', params: {} },
      useHash: false,
      configClass: MyRootUIRouterConfig
    }),
  ],
  providers: [
    { provide: MyRootUIRouterConfig, useClass: MyRootUIRouterConfig }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
import {UIRouter} from "ui-router-ng2";
import {Injectable} from "@angular/core";
declare var SystemJS;

/**
 * Create your own configuration class (if necessary) for any root/feature/lazy module.
 *
 * Pass it to the UIRouterModule.forRoot/forChild factory methods as `configClass`.
 *
 * The class will be added to the Injector and instantiate when the module loads.
 */
@Injectable()
export class MyRootUIRouterConfig {
  /** You may inject dependencies into the constructor */
  constructor(uiRouter: UIRouter) {
    // Show the ui-router visualizer
    let vis = window['ui-router-visualizer'];
    vis.visualizer(uiRouter);
  }
}

image

Only this code I add to starter.

UIRouterModule.forRoot({
      otherwise: { state: 'app', params: {} },
      useHash: false,
      configClass: MyRootUIRouterConfig
    }),
  ],
  providers: [
    { provide: MyRootUIRouterConfig, useClass: MyRootUIRouterConfig }
  ],

@tonimoeckel
Copy link

@kolkov did you try to use a export function call? Like this: angular/angular#11262

@kolkov
Copy link

kolkov commented Dec 13, 2016

Is it correct? C:\Users\Andy\AppData\Roaming\npm\node_modules\ui-router-core ->

D:\!Projects\TypeScript\ui-router\ui-router-ng2>npm link ui-router-core
D:\!Projects\TypeScript\ui-router\ui-router-ng2\node_modules\ui-router-core -> C:\Users\Andy\AppData\Roaming\npm\node_modules\ui-router-core -> D:\!Projects\TypeScript\ui-router\ui-router-core

D:\!Projects\TypeScript\ui-router\ui-router-ng2>

@kolkov
Copy link

kolkov commented Dec 13, 2016

@tonimoeckel If I comment UIRouterModule.forRoot my app builds correctly

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    /*UIRouterModule.forRoot({
      otherwise: { state: 'app', params: {} },
      useHash: false,
      configClass: MyRootUIRouterConfig
    }),*/
  ],
  providers: [
    { provide: MyRootUIRouterConfig, useClass: MyRootUIRouterConfig }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

image

@aitboudad
Copy link
Contributor Author

@kolkov you should build using ngc or try importing typescript files from node_modules/
something like

import {UIRouterModule} from ' ../node_modules/ui-router-ng2/src/ng2';

@kolkov
Copy link

kolkov commented Dec 13, 2016

@aitboudad I use ng from angular-cli. It it correct?
If I try `import { UIRouterModule } from '../node_modules/ui-router-ng2/src/ng2';
I have the same error.
image

`

@kolkov
Copy link

kolkov commented Dec 13, 2016

The same:
image

@aitboudad
Copy link
Contributor Author

see https://github.com/aitboudad/ui-router-aot-demo

@kolkov
Copy link

kolkov commented Dec 13, 2016

Is this ok? I see some warnings...
image

@aitboudad
Copy link
Contributor Author

yep, I'll try to fix those warning in the next few days but it should work!

@kolkov
Copy link

kolkov commented Dec 13, 2016

@aitboudad Thanks! Very good news for us! It's work!

@kolkov
Copy link

kolkov commented Dec 13, 2016

How can we rewrite resolve in Starter?

{
      name: 'app.bar.details', url: '/?barId', component: BarDetailsComponent,
       resolve: [
        // Inject the barList (from the parent) and find the correct
        {
          token: 'barDetail',
          deps: ['barList', Transition],
          resolveFn: (barList, trans) =>
            barList.find(item => item.id == trans.params().barId)
        }
      ]
    },

@kolkov
Copy link

kolkov commented Dec 14, 2016

I rewrote my great app using beta.4 and AoT.
Everything works perfectly!
Thanks for all very much!

@qvantor
Copy link

qvantor commented Dec 15, 2016

How soon we could use ui-router without aot problems? Cause now it still a problem

@christopherthielen
Copy link
Member

Wow, great work everyone.

I'm trying to get angular-ui-router release candidate out the door. After that I'll give this PR the attention it deserves!

@hghammoud
Copy link

I can't get it to work. I tried all the proposed solutions in this thread. Nada...

Can you pls rehost the working demo?

@mackelito
Copy link

mackelito commented Dec 28, 2016

Also looking for the solution to this... :/
import { UIRouterModule } from "../../node_modules/ui-router-ng2/ng2";
Is not working for me

@kolkov
Copy link

kolkov commented Dec 29, 2016

@mackelito this worked for me
image

@lexigren
Copy link

@kolkov can you create some kind of demo, of how you made ui-router work with AOT? Thanks.

@mackelito
Copy link

@kolkov so you added the index.d.ts file in "/node_modules/ui-router-ng2/"? or is that in your project dir?

@tonimoeckel
Copy link

tonimoeckel commented Jan 5, 2017

@lexigren I've just created a demo with a working version of ui-router-ng2 with AoT enabled. It is based on the latest "dev" branch of @aitboudad fork (see commit: b649735 )
see: https://github.com/tonimoeckel/ui-router-ng2-aot-demo

@@ -41,7 +41,7 @@ export type NgModuleToLoad = string | ModuleTypeCallback;
* - Returns the new states array
*/
export function loadNgModule(moduleToLoad: NgModuleToLoad): (transition: Transition) => Promise<LazyLoadResult> {
return function(transition: Transition) {
return (transition: Transition) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain why this was necessary? I don't see any this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use function it should be exported, it will not work with AoT when it is passed to an NgModule

@christopherthielen christopherthielen merged commit 45c7341 into ui-router:master Jan 11, 2017
@aitboudad aitboudad deleted the aot branch January 11, 2017 23:49
@amcdnl
Copy link
Contributor

amcdnl commented Jan 12, 2017

This is epic!

@longbowww
Copy link

what is the winning config for all of us angular-cli users now? unfortunately the config of @tonimoeckel wont let cli start, just giving the old error msg Calling function 'UIRouterModule', function calls are not supported.

(not using the aot flag)

@marshall007
Copy link

@christopherthielen can we tag a -beta.4 release with this patch soon? As others have mentioned, this is preventing usage with ng-cli.

@marshall007
Copy link

marshall007 commented Jan 13, 2017

@christopherthielen @aitboudad I tried compiling under ng-cli using 45c7341 and it was initially complaining that all my resolveFn needed to be exported statically. Once I did that, I'm still getting:

ERROR in Error encountered resolving symbol values statically. Calling function 'OpaqueToken', function calls are not supported.
Consider replacing the function or lambda with a reference to an exported function,
  resolving symbol UIROUTER_ROOT_MODULE in ./node_modules/ui-router-ng2/lib/ng2/uiRouterNgModule.d.ts,
  resolving symbol makeRootProviders in ./node_modules/ui-router-ng2/lib/ng2/uiRouterNgModule.d.ts,
  resolving symbol UIRouterModule.forRoot in ./node_modules/ui-router-ng2/lib/ng2/uiRouterNgModule.d.ts,
  resolving symbol AppModule in ./src/app/app.module.ts,
  resolving symbol AppModule in ./src/app/app.module.ts
  • Does this mean going forward, we won't be able to use anonymous functions as our resolveFn?
  • Should AoT be working on master now? Am I missing something or is this still WIP?

[edit]: note, I am not using the --aot flag.

@marshall007
Copy link

See angular/angular-cli#3854 (comment)

Apparently the CLI has issues resolving dependencies when nested node_modules directories are present in the project. In this case, I was using npm link ui-router-ng2 to test the most recent changes. Everything worked as expected once I removed ./node_modules/ui-router-ng2/node_modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.