Skip to content

Commit

Permalink
feat(AoT): Implement Ahead of Time compilation support
Browse files Browse the repository at this point in the history
initial work for Aot support
  • Loading branch information
christopherthielen authored Jan 11, 2017
2 parents 0a997a9 + 75cc29b commit 45c7341
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 40 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 --internal-aliases internal,coreapi,ng2api --external-aliases internalapi,external --navigation-label-globals ui-router-ng2"
},
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/ng2/lazyLoadNgModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
const ng2Injector = transition.injector().get(NATIVE_INJECTOR_TOKEN);

const createModule = (factory: NgModuleFactory<any>) =>
Expand All @@ -53,7 +53,7 @@ export function loadNgModule(moduleToLoad: NgModuleToLoad): (transition: Transit
return loadModuleFactory(moduleToLoad, ng2Injector)
.then(createModule)
.then(applyModule);
}
};
}

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

return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync)
return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync);
}

/**
Expand Down
33 changes: 20 additions & 13 deletions src/ng2/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]},
];

/**
Expand All @@ -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);

38 changes: 21 additions & 17 deletions src/ng2/uiRouterNgModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
]
}
}
Expand Down Expand Up @@ -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]]
*/
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"declaration": true,
"sourceMap": true
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true
},
"files": [
"src/ng2.ts"
]
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"declaration": true,
"sourceMap": true
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true
},
"files": [
"src/ng2.ts"
]
Expand Down

0 comments on commit 45c7341

Please sign in to comment.