Skip to content

Commit 45c7341

Browse files
feat(AoT): Implement Ahead of Time compilation support
initial work for Aot support
2 parents 0a997a9 + 75cc29b commit 45c7341

File tree

6 files changed

+60
-40
lines changed

6 files changed

+60
-40
lines changed

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "1.0.0-beta.4",
55
"scripts": {
66
"clean": "shx rm -rf lib lib-esm _bundles _doc",
7-
"build": "npm run clean && tsc && tsc -p tsconfig.esm.json && webpack",
7+
"build": "npm run clean && node_modules/.bin/ngc && node_modules/.bin/ngc -p tsconfig.esm.json && webpack",
88
"test": "karma start config/karma.ng2.js",
99
"docs": "typedoc --tsconfig tsconfig.typedoc.json --readme README.md --name 'ui-router-ng2' --theme node_modules/ui-router-typedoc-themes/bin/default --out _doc --internal-aliases internal,coreapi,ng2api --external-aliases internalapi,external --navigation-label-globals ui-router-ng2"
1010
},
@@ -56,12 +56,13 @@
5656
"@angular/common": "^2.0.0"
5757
},
5858
"devDependencies": {
59-
"@angular/common": "^2.0.0",
60-
"@angular/compiler": "^2.0.0",
61-
"@angular/core": "^2.0.0",
62-
"@angular/platform-browser": "^2.0.0",
63-
"@angular/platform-browser-dynamic": "^2.0.0",
64-
"@angular/platform-server": "^2.0.0",
59+
"@angular/common": "^2.3.1",
60+
"@angular/compiler": "^2.3.1",
61+
"@angular/compiler-cli": "^2.3.1",
62+
"@angular/core": "^2.3.1",
63+
"@angular/platform-browser": "^2.3.1",
64+
"@angular/platform-browser-dynamic": "^2.3.1",
65+
"@angular/platform-server": "^2.3.1",
6566
"@types/jasmine": "^2.2.34",
6667
"@types/jquery": "^1.10.31",
6768
"awesome-typescript-loader": "^2.2.4",

src/ng2/lazyLoadNgModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type NgModuleToLoad = string | ModuleTypeCallback;
4141
* - Returns the new states array
4242
*/
4343
export function loadNgModule(moduleToLoad: NgModuleToLoad): (transition: Transition) => Promise<LazyLoadResult> {
44-
return function(transition: Transition) {
44+
return (transition: Transition) => {
4545
const ng2Injector = transition.injector().get(NATIVE_INJECTOR_TOKEN);
4646

4747
const createModule = (factory: NgModuleFactory<any>) =>
@@ -53,7 +53,7 @@ export function loadNgModule(moduleToLoad: NgModuleToLoad): (transition: Transit
5353
return loadModuleFactory(moduleToLoad, ng2Injector)
5454
.then(createModule)
5555
.then(applyModule);
56-
}
56+
};
5757
}
5858

5959
/**
@@ -80,7 +80,7 @@ export function loadModuleFactory(moduleToLoad: NgModuleToLoad, ng2Injector: Inj
8080
const compileAsync = (moduleType: Type<any>) =>
8181
compiler.compileModuleAsync(moduleType);
8282

83-
return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync)
83+
return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync);
8484
}
8585

8686
/**

src/ng2/providers.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* - [quick start repository](http://github.com/ui-router/quickstart-ng2)
77
*
88
* Getting started:
9-
*
9+
*
1010
* - Use npm. Add a dependency on latest `ui-router-ng2`
1111
* - Import UI-Router classes directly from `"ui-router-ng2"`
1212
*
@@ -111,9 +111,7 @@ import {NATIVE_INJECTOR_TOKEN} from "ui-router-core";
111111
* Creates a UIRouter instance and configures it for Angular 2, then invokes router bootstrap.
112112
* This function is used as an Angular 2 `useFactory` Provider.
113113
*/
114-
let uiRouterFactory = (
115-
location: UIRouterLocation,
116-
injector: Injector) => {
114+
export function uiRouterFactory(location: UIRouterLocation, injector: Injector) {
117115

118116
let rootModules: RootModule[] = injector.get(UIROUTER_ROOT_MODULE);
119117
let modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN);
@@ -167,20 +165,30 @@ let uiRouterFactory = (
167165
return router;
168166
};
169167

168+
export function parentUIViewInjectFactory(r: StateRegistry) { return { fqn: null, context: r.root() } as ParentUIViewInject; }
169+
170170
export const _UIROUTER_INSTANCE_PROVIDERS: Provider[] = [
171171
{ provide: UIRouter, useFactory: uiRouterFactory, deps: [UIRouterLocation, Injector] },
172172
{ provide: UIRouterLocation, useClass: UIRouterLocation },
173-
{ provide: UIView.PARENT_INJECT, useFactory: (r: StateRegistry) => { return { fqn: null, context: r.root() } as ParentUIViewInject }, deps: [StateRegistry]},
173+
{ provide: UIView.PARENT_INJECT, useFactory: parentUIViewInjectFactory, deps: [StateRegistry]},
174174
];
175175

176+
export function fnStateService(r: UIRouter) { return r.stateService; }
177+
export function fnTransitionService(r: UIRouter) { return r.transitionService; }
178+
export function fnUrlMatcherFactory(r: UIRouter) { return r.urlMatcherFactory; }
179+
export function fnUrlRouter(r: UIRouter) { return r.urlRouter; }
180+
export function fnViewService(r: UIRouter) { return r.viewService; }
181+
export function fnStateRegistry(r: UIRouter) { return r.stateRegistry; }
182+
export function fnGlobals(r: any) { return r.globals; }
183+
176184
export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [
177-
{ provide: StateService, useFactory: (r: UIRouter) => r.stateService , deps: [UIRouter]},
178-
{ provide: TransitionService, useFactory: (r: UIRouter) => r.transitionService, deps: [UIRouter]},
179-
{ provide: UrlMatcherFactory, useFactory: (r: UIRouter) => r.urlMatcherFactory, deps: [UIRouter]},
180-
{ provide: UrlRouter, useFactory: (r: UIRouter) => r.urlRouter , deps: [UIRouter]},
181-
{ provide: ViewService, useFactory: (r: UIRouter) => r.viewService , deps: [UIRouter]},
182-
{ provide: StateRegistry, useFactory: (r: UIRouter) => r.stateRegistry , deps: [UIRouter]},
183-
{ provide: Globals, useFactory: (r: UIRouter) => r.globals , deps: [UIRouter]},
185+
{ provide: StateService, useFactory: fnStateService, deps: [UIRouter]},
186+
{ provide: TransitionService, useFactory: fnTransitionService, deps: [UIRouter]},
187+
{ provide: UrlMatcherFactory, useFactory: fnUrlMatcherFactory, deps: [UIRouter]},
188+
{ provide: UrlRouter, useFactory: fnUrlRouter, deps: [UIRouter]},
189+
{ provide: ViewService, useFactory: fnViewService, deps: [UIRouter]},
190+
{ provide: StateRegistry, useFactory: fnStateRegistry, deps: [UIRouter]},
191+
{ provide: Globals, useFactory: fnGlobals, deps: [UIRouter]},
184192
];
185193

186194
/**
@@ -189,4 +197,3 @@ export const _UIROUTER_SERVICE_PROVIDERS: Provider[] = [
189197
* @deprecated use [[UIRouterModule.forRoot]]
190198
*/
191199
export const UIROUTER_PROVIDERS: Provider[] = _UIROUTER_INSTANCE_PROVIDERS.concat(_UIROUTER_SERVICE_PROVIDERS);
192-

src/ng2/uiRouterNgModule.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ import {identity} from "ui-router-core";
99
import {LocationStrategy, HashLocationStrategy, PathLocationStrategy} from "@angular/common";
1010
import {_UIROUTER_INSTANCE_PROVIDERS, _UIROUTER_SERVICE_PROVIDERS} from "./providers";
1111

12+
export function makeRootProviders(module: StatesModule): Provider[] {
13+
return [
14+
{ provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true},
15+
{ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true },
16+
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true },
17+
];
18+
}
19+
20+
export function makeChildProviders(module: StatesModule): Provider[] {
21+
return [
22+
{ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true },
23+
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true },
24+
];
25+
}
26+
27+
export function locationStrategy(useHash) {
28+
return { provide: LocationStrategy, useClass: useHash ? HashLocationStrategy : PathLocationStrategy };
29+
}
1230

1331
/**
1432
* Creates UI-Router Modules
@@ -75,14 +93,13 @@ export class UIRouterModule {
7593
* @returns an `NgModule` which provides the [[UIRouter]] singleton instance
7694
*/
7795
static forRoot(config: RootModule = {}): ModuleWithProviders {
78-
let locationStrategy = config.useHash ? HashLocationStrategy : PathLocationStrategy;
7996
return {
8097
ngModule: UIRouterModule,
8198
providers: [
8299
_UIROUTER_INSTANCE_PROVIDERS,
83100
_UIROUTER_SERVICE_PROVIDERS,
84-
{ provide: LocationStrategy, useClass: locationStrategy },
85-
...makeProviders(config, true),
101+
locationStrategy(config.useHash),
102+
...makeRootProviders(config),
86103
]
87104
}
88105
}
@@ -114,25 +131,12 @@ export class UIRouterModule {
114131
static forChild(module: StatesModule = {}): ModuleWithProviders {
115132
return {
116133
ngModule: UIRouterModule,
117-
providers: makeProviders(module, false),
134+
providers: makeChildProviders(module),
118135
}
119136
}
120137

121138
}
122139

123-
/** @hidden */
124-
function makeProviders(module: StatesModule, forRoot: boolean): Provider[] {
125-
let providers: Provider[] = [module.configClass]
126-
.filter(identity)
127-
.map(configClass => ({ provide: configClass, useClass: configClass }));
128-
129-
if (forRoot) providers.push({ provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true});
130-
providers.push({ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true });
131-
providers.push({ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true });
132-
133-
return providers;
134-
}
135-
136140
/**
137141
* UI-Router declarative configuration which can be provided to [[UIRouterModule.forRoot]]
138142
*/

tsconfig.esm.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"declaration": true,
1313
"sourceMap": true
1414
},
15+
"angularCompilerOptions": {
16+
"skipTemplateCodegen": true,
17+
"strictMetadataEmit": true
18+
},
1519
"files": [
1620
"src/ng2.ts"
1721
]

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"declaration": true,
1313
"sourceMap": true
1414
},
15+
"angularCompilerOptions": {
16+
"skipTemplateCodegen": true,
17+
"strictMetadataEmit": true
18+
},
1519
"files": [
1620
"src/ng2.ts"
1721
]

0 commit comments

Comments
 (0)