From 8088ef9c2c5bb363ae508981394037f6cf087116 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Tue, 29 Nov 2016 21:50:49 +0000 Subject: [PATCH 1/3] Aot support --- src/ng2/directives/uiSrefStatus.ts | 2 +- src/ng2/providers.ts | 33 ++++++++++++++++---------- src/ng2/uiRouterNgModule.ts | 38 +++++++++++++++++------------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/ng2/directives/uiSrefStatus.ts b/src/ng2/directives/uiSrefStatus.ts index fc96b6342..f390c778a 100644 --- a/src/ng2/directives/uiSrefStatus.ts +++ b/src/ng2/directives/uiSrefStatus.ts @@ -75,7 +75,7 @@ function spreadToSubPaths(basePath: PathNode[], appendPath: PathNode[]): PathNod /** * Given a TransEvt (Transition event: started, success, error) * and a UISref Target State, return a SrefStatus object - * which represents the current status of that Sref: + * which represents the current status of that Sref: * active, activeEq (exact match), entering, exiting * * @internalapi diff --git a/src/ng2/providers.ts b/src/ng2/providers.ts index fc23b0ae7..057c99959 100644 --- a/src/ng2/providers.ts +++ b/src/ng2/providers.ts @@ -6,7 +6,7 @@ * - [quick start repository](http://github.com/ui-router/quickstart-ng2) * * Getting started: - * + * * - Use npm. Add a dependency on latest `ui-router-ng2` * - Import UI-Router classes directly from `"ui-router-ng2"` * @@ -111,9 +111,7 @@ import {NATIVE_INJECTOR_TOKEN} from "ui-router-core"; * Creates a UIRouter instance and configures it for Angular 2, then invokes router bootstrap. * This function is used as an Angular 2 `useFactory` Provider. */ -let uiRouterFactory = ( - location: UIRouterLocation, - injector: Injector) => { +export function uiRouterFactory(location: UIRouterLocation, injector: Injector) { let rootModules: RootModule[] = injector.get(UIROUTER_ROOT_MODULE); let modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN); @@ -167,20 +165,30 @@ let uiRouterFactory = ( return router; }; +export function parentUIViewInjectFactory(r: StateRegistry) { return { fqn: null, context: r.root() } as ParentUIViewInject; } + export const _UIROUTER_INSTANCE_PROVIDERS: Provider[] = [ { provide: UIRouter, useFactory: uiRouterFactory, deps: [UIRouterLocation, Injector] }, { provide: UIRouterLocation, useClass: UIRouterLocation }, - { provide: UIView.PARENT_INJECT, useFactory: (r: StateRegistry) => { return { fqn: null, context: r.root() } as ParentUIViewInject }, deps: [StateRegistry]}, + { provide: UIView.PARENT_INJECT, useFactory: parentUIViewInjectFactory, deps: [StateRegistry]}, ]; +export function fnStateService(r: UIRouter) { return r.stateService; } +export function fnTransitionService(r: UIRouter) { return r.transitionService; } +export function fnUrlMatcherFactory(r: UIRouter) { return r.urlMatcherFactory; } +export function fnUrlRouter(r: UIRouter) { return r.urlRouter; } +export function fnViewService(r: UIRouter) { return r.viewService; } +export function fnStateRegistry(r: UIRouter) { return r.stateRegistry; } +export function fnGlobals(r: any) { return r.globals; } + export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [ - { provide: StateService, useFactory: (r: UIRouter) => r.stateService , deps: [UIRouter]}, - { provide: TransitionService, useFactory: (r: UIRouter) => r.transitionService, deps: [UIRouter]}, - { provide: UrlMatcherFactory, useFactory: (r: UIRouter) => r.urlMatcherFactory, deps: [UIRouter]}, - { provide: UrlRouter, useFactory: (r: UIRouter) => r.urlRouter , deps: [UIRouter]}, - { provide: ViewService, useFactory: (r: UIRouter) => r.viewService , deps: [UIRouter]}, - { provide: StateRegistry, useFactory: (r: UIRouter) => r.stateRegistry , deps: [UIRouter]}, - { provide: Globals, useFactory: (r: UIRouter) => r.globals , deps: [UIRouter]}, + { provide: StateService, useFactory: fnStateService, deps: [UIRouter]}, + { provide: TransitionService, useFactory: fnTransitionService, deps: [UIRouter]}, + { provide: UrlMatcherFactory, useFactory: fnUrlMatcherFactory, deps: [UIRouter]}, + { provide: UrlRouter, useFactory: fnUrlRouter, deps: [UIRouter]}, + { provide: ViewService, useFactory: fnViewService, deps: [UIRouter]}, + { provide: StateRegistry, useFactory: fnStateRegistry, deps: [UIRouter]}, + { provide: Globals, useFactory: fnGlobals, deps: [UIRouter]}, ]; /** @@ -189,4 +197,3 @@ export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [ * @deprecated use [[UIRouterModule.forRoot]] */ export const UIROUTER_PROVIDERS: Provider[] = _UIROUTER_INSTANCE_PROVIDERS.concat(_UIROUTER_SERVICE_PROVIDERS); - diff --git a/src/ng2/uiRouterNgModule.ts b/src/ng2/uiRouterNgModule.ts index 5ac08444c..13d1b5b9e 100644 --- a/src/ng2/uiRouterNgModule.ts +++ b/src/ng2/uiRouterNgModule.ts @@ -9,6 +9,24 @@ import {identity} from "ui-router-core"; import {LocationStrategy, HashLocationStrategy, PathLocationStrategy} from "@angular/common"; import {_UIROUTER_INSTANCE_PROVIDERS, _UIROUTER_SERVICE_PROVIDERS} from "./providers"; +export function makeRootProviders(module: StatesModule): Provider[] { + return [ + { provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true}, + { provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true }, + { provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true }, + ]; +} + +export function makeChildProviders(module: StatesModule): Provider[] { + return [ + { provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true }, + { provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true }, + ]; +} + +export function locationStrategy(useHash) { + return { provide: LocationStrategy, useClass: useHash ? HashLocationStrategy : PathLocationStrategy }; +} /** * Creates UI-Router Modules @@ -75,14 +93,13 @@ export class UIRouterModule { * @returns an `NgModule` which provides the [[UIRouter]] singleton instance */ static forRoot(config: RootModule = {}): ModuleWithProviders { - let locationStrategy = config.useHash ? HashLocationStrategy : PathLocationStrategy; return { ngModule: UIRouterModule, providers: [ _UIROUTER_INSTANCE_PROVIDERS, _UIROUTER_SERVICE_PROVIDERS, - { provide: LocationStrategy, useClass: locationStrategy }, - ...makeProviders(config, true), + locationStrategy(config.useHash), + ...makeRootProviders(config), ] } } @@ -114,25 +131,12 @@ export class UIRouterModule { static forChild(module: StatesModule = {}): ModuleWithProviders { return { ngModule: UIRouterModule, - providers: makeProviders(module, false), + providers: makeChildProviders(module), } } } -/** @hidden */ -function makeProviders(module: StatesModule, forRoot: boolean): Provider[] { - let providers: Provider[] = [module.configClass] - .filter(identity) - .map(configClass => ({ provide: configClass, useClass: configClass })); - - if (forRoot) providers.push({ provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true}); - providers.push({ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true }); - providers.push({ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true }); - - return providers; -} - /** * UI-Router declarative configuration which can be provided to [[UIRouterModule.forRoot]] */ From 6dfc226c93a8fe9f6e061c404e4c8d13e22a8247 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Fri, 16 Dec 2016 20:14:08 +0000 Subject: [PATCH 2/3] Added ngc. --- package.json | 15 ++++++++------- tsconfig.esm.json | 4 ++++ tsconfig.json | 4 ++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 45619dd0d..e0d239594 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "1.0.0-beta.4", "scripts": { "clean": "shx rm -rf lib lib-esm _bundles _doc", - "build": "npm run clean && tsc && tsc -p tsconfig.esm.json && webpack", + "build": "npm run clean && node_modules/.bin/ngc && node_modules/.bin/ngc -p tsconfig.esm.json && webpack", "test": "karma start config/karma.ng2.js", "docs": "typedoc --tsconfig tsconfig.typedoc.json --readme README.md --name 'ui-router-ng2' --theme node_modules/ui-router-typedoc-themes/bin/default --out _doc --external-aliases internalapi,external --navigation-label-globals ui-router-ng2" }, @@ -56,12 +56,13 @@ "@angular/common": "~2.0.0" }, "devDependencies": { - "@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", - "@angular/platform-server": "~2.0.0", + "@angular/common": "^2.3.1", + "@angular/compiler": "^2.3.1", + "@angular/compiler-cli": "^2.3.1", + "@angular/core": "^2.3.1", + "@angular/platform-browser": "^2.3.1", + "@angular/platform-browser-dynamic": "^2.3.1", + "@angular/platform-server": "^2.3.1", "@types/jasmine": "^2.2.34", "@types/jquery": "^1.10.31", "awesome-typescript-loader": "^2.2.4", diff --git a/tsconfig.esm.json b/tsconfig.esm.json index 8c23618db..09165be11 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -12,6 +12,10 @@ "declaration": true, "sourceMap": true }, + "angularCompilerOptions": { + "skipTemplateCodegen": true, + "strictMetadataEmit": true + }, "files": [ "src/ng2.ts" ] diff --git a/tsconfig.json b/tsconfig.json index fea3863fc..26e901080 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,10 @@ "declaration": true, "sourceMap": true }, + "angularCompilerOptions": { + "skipTemplateCodegen": true, + "strictMetadataEmit": true + }, "files": [ "src/ng2.ts" ] From 271ded826422bb5420b695de2f8e685370f3a91c Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Mon, 9 Jan 2017 09:48:07 +0000 Subject: [PATCH 3/3] [AOT] fixed loadNgModule --- src/ng2/lazyLoadNgModule.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ng2/lazyLoadNgModule.ts b/src/ng2/lazyLoadNgModule.ts index 02b802131..384d74f18 100644 --- a/src/ng2/lazyLoadNgModule.ts +++ b/src/ng2/lazyLoadNgModule.ts @@ -41,7 +41,7 @@ export type NgModuleToLoad = string | ModuleTypeCallback; * - Returns the new states array */ export function loadNgModule(moduleToLoad: NgModuleToLoad): (transition: Transition) => Promise { - return function(transition: Transition) { + return (transition: Transition) => { const ng2Injector = transition.injector().get(NATIVE_INJECTOR_TOKEN); const createModule = (factory: NgModuleFactory) => @@ -53,7 +53,7 @@ export function loadNgModule(moduleToLoad: NgModuleToLoad): (transition: Transit return loadModuleFactory(moduleToLoad, ng2Injector) .then(createModule) .then(applyModule); - } + }; } /** @@ -80,7 +80,7 @@ export function loadModuleFactory(moduleToLoad: NgModuleToLoad, ng2Injector: Inj const compileAsync = (moduleType: Type) => compiler.compileModuleAsync(moduleType); - return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync) + return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync); } /**