Skip to content

Commit

Permalink
fix(ComponentUrlMapper): support relative template URLs in Dartium
Browse files Browse the repository at this point in the history
When running in Dartium without using transformers (i.e. with a normal
static web server), handle relative template URLs. This works by using
mirrors to get the URL of the library where the component class is
defined.

Closes #2771

Closes #3743
  • Loading branch information
Tim Blasi authored and kegluneq committed Sep 1, 2015
1 parent 42e1b07 commit 7c7888d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 4 deletions.
5 changes: 4 additions & 1 deletion modules/angular2/src/core/compiler/component_url_mapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Injectable} from 'angular2/di';
import {Type, isPresent} from 'angular2/src/core/facade/lang';
import {Map, MapWrapper} from 'angular2/src/core/facade/collection';
import {reflector} from 'angular2/src/core/reflection/reflection';

/**
* Resolve a `Type` from a {@link ComponentMetadata} into a URL.
Expand All @@ -17,7 +18,9 @@ export class ComponentUrlMapper {
* - an absolute URL,
* - a path relative to the application
*/
getUrl(component: Type): string { return './'; }
getUrl(component: Type): string {
return reflector.isReflectionEnabled() ? reflector.importUri(component) : './';
}
}

export class RuntimeComponentUrlMapper extends ComponentUrlMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface PlatformReflectionCapabilities {
getter(name: string): GetterFn;
setter(name: string): SetterFn;
method(name: string): MethodFn;
importUri(type: Type): string;
}
2 changes: 2 additions & 0 deletions modules/angular2/src/core/reflection/reflection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class NoReflectionCapabilities implements PlatformReflectionCapabilities {
MethodFn method(String name) {
throw "Cannot find method ${name}";
}

String importUri(Type type) => './';
}

final Reflector reflector = new Reflector(new NoReflectionCapabilities());
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,8 @@ class ReflectionCapabilities implements PlatformReflectionCapabilities {
ClassMirror classMirror = reflectType(type);
return classMirror.metadata;
}

String importUri(Type type) {
return '${(reflectClass(type).owner as LibraryMirror).uri}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
return o.${name}.apply(o, args);`;
return <MethodFn>new Function('o', 'args', functionBody);
}

// There is not a concept of import uri in Js, but this is useful in developing Dart applications.
importUri(type: Type): string { return './'; }
}
2 changes: 2 additions & 0 deletions modules/angular2/src/core/reflection/reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export class Reflector {
}

_containsReflectionInfo(typeOrFunc) { return this._injectableInfo.has(typeOrFunc); }

importUri(type: Type): string { return this.reflectionCapabilities.importUri(type); }
}

function _mergeMaps(target: Map<any, any>, config: StringMap<string, Function>): void {
Expand Down
17 changes: 17 additions & 0 deletions modules/angular2/test/core/compiler/component_url_mapper_spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library angular2.test.core.compiler.component_url_mapper_spec;

import 'package:angular2/test_lib.dart';
import 'package:angular2/src/core/compiler/component_url_mapper.dart';

main() {
describe("ComponentUrlMapper", () {
it("should return the URL of the component's library", () {
var mapper = new ComponentUrlMapper();
expect(mapper
.getUrl(SomeComponent)
.endsWith("core/compiler/component_url_mapper_spec.dart")).toBeTrue();
});
});
}

class SomeComponent {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class NullReflectionCapabilities implements ReflectionCapabilities {

_notImplemented(String name) => throw 'Not implemented: $name';

bool isReflectionEnabled() {
return false;
}
bool isReflectionEnabled() => false;

Function factory(Type type) => _notImplemented("factory");

Expand All @@ -27,6 +25,8 @@ class NullReflectionCapabilities implements ReflectionCapabilities {
SetterFn setter(String name) => _nullSetter;

MethodFn method(String name) => _nullMethod;

String importUri(Type type) => './';
}

_nullGetter(Object p) => null;
Expand Down

0 comments on commit 7c7888d

Please sign in to comment.