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

feat(ComponentResolver): Add a SystemJS resolver for compiled apps #9145

Merged
merged 1 commit into from Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/@angular/compiler/src/runtime_compiler.ts
Expand Up @@ -62,7 +62,7 @@ export class RuntimeCompiler implements ComponentResolver {
compMeta.selector, compiledTemplate.viewFactory, componentType));
}

clearCache() {
clearCache(): void {
this._styleCache.clear();
this._compiledTemplateCache.clear();
this._compiledTemplateDone.clear();
Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/core/src/linker.ts
Expand Up @@ -5,7 +5,7 @@ export {DynamicComponentLoader} from './linker/dynamic_component_loader';
export {ElementRef} from './linker/element_ref';
export {ExpressionChangedAfterItHasBeenCheckedException} from './linker/exceptions';
export {QueryList} from './linker/query_list';
export {SystemJsComponentResolver} from './linker/systemjs_component_resolver';
export {SystemJsCmpFactoryResolver, SystemJsComponentResolver} from './linker/systemjs_component_resolver';
export {TemplateRef} from './linker/template_ref';
export {ViewContainerRef} from './linker/view_container_ref';
export {EmbeddedViewRef, ViewRef} from './linker/view_ref';
3 changes: 2 additions & 1 deletion modules/@angular/core/src/linker/component_resolver.ts
Expand Up @@ -14,7 +14,7 @@ import {ComponentFactory} from './component_factory';
*/
export abstract class ComponentResolver {
abstract resolveComponent(component: Type|string): Promise<ComponentFactory<any>>;
abstract clearCache(): any /** TODO #9100 */;
abstract clearCache(): void;
}

function _isComponentFactory(type: any): boolean {
Expand All @@ -37,5 +37,6 @@ export class ReflectorComponentResolver extends ComponentResolver {
}
return PromiseWrapper.resolve(componentFactory);
}

clearCache() {}
}
40 changes: 35 additions & 5 deletions modules/@angular/core/src/linker/systemjs_component_resolver.ts
Expand Up @@ -3,6 +3,7 @@ import {Type, global, isString} from '../facade/lang';
import {ComponentFactory} from './component_factory';
import {ComponentResolver} from './component_resolver';

const _SEPARATOR = '#';

/**
* Component resolver that can load components lazily
Expand All @@ -13,13 +14,42 @@ export class SystemJsComponentResolver implements ComponentResolver {

resolveComponent(componentType: string|Type): Promise<ComponentFactory<any>> {
if (isString(componentType)) {
let [module, component] = componentType.split(_SEPARATOR);

if (component === void(0)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why void(0)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because (undefined) => void(0) == undefined;, ie you can define undefined

// Use the default export when no component is specified
component = 'default';
}

return (<any>global)
.System.import(module)
.then((module: any) => this._resolver.resolveComponent(module[component]));
}

return this._resolver.resolveComponent(componentType);
}

clearCache(): void {}
}

const FACTORY_MODULE_SUFFIX = '.ngfactory';
const FACTORY_CLASS_SUFFIX = 'NgFactory';

/**
* Component resolver that can load component factories lazily
* @experimental
*/
export class SystemJsCmpFactoryResolver implements ComponentResolver {
resolveComponent(componentType: string|Type): Promise<ComponentFactory<any>> {
if (isString(componentType)) {
let [module, factory] = componentType.split(_SEPARATOR);
return (<any>global)
.System.import(componentType)
.then((module: any /** TODO #9100 */) => this._resolver.resolveComponent(module.default));
} else {
return this._resolver.resolveComponent(<Type>componentType);
.System.import(module + FACTORY_MODULE_SUFFIX)
.then((module: any) => module[factory + FACTORY_CLASS_SUFFIX]);
}

return Promise.resolve(null);
}

clearCache() {}
clearCache(): void {}
}
2 changes: 1 addition & 1 deletion modules/@angular/facade/src/lang.ts
Expand Up @@ -128,7 +128,7 @@ export function isNumber(obj: any): boolean {
return typeof obj === 'number';
}

export function isString(obj: any): boolean {
export function isString(obj: any): obj is String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, didn't know TS supports this!

return typeof obj === 'string';
}

Expand Down
9 changes: 6 additions & 3 deletions tools/public_api_guard/public_api_spec.ts
Expand Up @@ -161,7 +161,7 @@ const CORE = [
'ComponentRef.onDestroy(callback:Function):void',
'ComponentRef<C>',
'ComponentResolver',
'ComponentResolver.clearCache():any',
'ComponentResolver.clearCache():void',
'ComponentResolver.resolveComponent(component:Type|string):Promise<ComponentFactory<any>>',
'const APP_ID:any',
'const APP_INITIALIZER:any',
Expand Down Expand Up @@ -512,8 +512,11 @@ const CORE = [
'SkipSelfMetadataFactory',
'state(stateNameExpr:string, styles:AnimationStyleMetadata):AnimationStateDeclarationMetadata',
'style(tokens:string|{[key:string]:string|number}|Array<string|{[key:string]:string|number}>):AnimationStyleMetadata',
'SystemJsCmpFactoryResolver',
'SystemJsCmpFactoryResolver.clearCache():void',
'SystemJsCmpFactoryResolver.resolveComponent(componentType:string|Type):Promise<ComponentFactory<any>>',
'SystemJsComponentResolver',
'SystemJsComponentResolver.clearCache():any',
'SystemJsComponentResolver.clearCache():void',
'SystemJsComponentResolver.constructor(_resolver:ComponentResolver)',
'SystemJsComponentResolver.resolveComponent(componentType:string|Type):Promise<ComponentFactory<any>>',
'TemplateRef.createEmbeddedView(context:C):EmbeddedViewRef<C>',
Expand Down Expand Up @@ -1356,7 +1359,7 @@ const COMPILER = [
'RenderTypes.renderNode:CompileIdentifierMetadata',
'RenderTypes.renderText:CompileIdentifierMetadata',
'RuntimeCompiler',
'RuntimeCompiler.clearCache():any',
'RuntimeCompiler.clearCache():void',
'RuntimeCompiler.constructor(_metadataResolver:CompileMetadataResolver, _templateNormalizer:DirectiveNormalizer, _templateParser:TemplateParser, _styleCompiler:StyleCompiler, _viewCompiler:ViewCompiler, _xhr:XHR, _genConfig:CompilerConfig)',
'RuntimeCompiler.resolveComponent(component:Type|string):Promise<ComponentFactory<any>>',
'SourceModule',
Expand Down