Skip to content

Commit 7c7888d

Browse files
Tim Blasikegluneq
authored andcommitted
fix(ComponentUrlMapper): support relative template URLs in Dartium
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
1 parent 42e1b07 commit 7c7888d

File tree

8 files changed

+36
-4
lines changed

8 files changed

+36
-4
lines changed

modules/angular2/src/core/compiler/component_url_mapper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Injectable} from 'angular2/di';
22
import {Type, isPresent} from 'angular2/src/core/facade/lang';
33
import {Map, MapWrapper} from 'angular2/src/core/facade/collection';
4+
import {reflector} from 'angular2/src/core/reflection/reflection';
45

56
/**
67
* Resolve a `Type` from a {@link ComponentMetadata} into a URL.
@@ -17,7 +18,9 @@ export class ComponentUrlMapper {
1718
* - an absolute URL,
1819
* - a path relative to the application
1920
*/
20-
getUrl(component: Type): string { return './'; }
21+
getUrl(component: Type): string {
22+
return reflector.isReflectionEnabled() ? reflector.importUri(component) : './';
23+
}
2124
}
2225

2326
export class RuntimeComponentUrlMapper extends ComponentUrlMapper {

modules/angular2/src/core/reflection/platform_reflection_capabilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export interface PlatformReflectionCapabilities {
1010
getter(name: string): GetterFn;
1111
setter(name: string): SetterFn;
1212
method(name: string): MethodFn;
13+
importUri(type: Type): string;
1314
}

modules/angular2/src/core/reflection/reflection.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class NoReflectionCapabilities implements PlatformReflectionCapabilities {
3838
MethodFn method(String name) {
3939
throw "Cannot find method ${name}";
4040
}
41+
42+
String importUri(Type type) => './';
4143
}
4244

4345
final Reflector reflector = new Reflector(new NoReflectionCapabilities());

modules/angular2/src/core/reflection/reflection_capabilities.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,8 @@ class ReflectionCapabilities implements PlatformReflectionCapabilities {
297297
ClassMirror classMirror = reflectType(type);
298298
return classMirror.metadata;
299299
}
300+
301+
String importUri(Type type) {
302+
return '${(reflectClass(type).owner as LibraryMirror).uri}';
303+
}
300304
}

modules/angular2/src/core/reflection/reflection_capabilities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
155155
return o.${name}.apply(o, args);`;
156156
return <MethodFn>new Function('o', 'args', functionBody);
157157
}
158+
159+
// There is not a concept of import uri in Js, but this is useful in developing Dart applications.
160+
importUri(type: Type): string { return './'; }
158161
}

modules/angular2/src/core/reflection/reflector.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ export class Reflector {
153153
}
154154

155155
_containsReflectionInfo(typeOrFunc) { return this._injectableInfo.has(typeOrFunc); }
156+
157+
importUri(type: Type): string { return this.reflectionCapabilities.importUri(type); }
156158
}
157159

158160
function _mergeMaps(target: Map<any, any>, config: StringMap<string, Function>): void {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library angular2.test.core.compiler.component_url_mapper_spec;
2+
3+
import 'package:angular2/test_lib.dart';
4+
import 'package:angular2/src/core/compiler/component_url_mapper.dart';
5+
6+
main() {
7+
describe("ComponentUrlMapper", () {
8+
it("should return the URL of the component's library", () {
9+
var mapper = new ComponentUrlMapper();
10+
expect(mapper
11+
.getUrl(SomeComponent)
12+
.endsWith("core/compiler/component_url_mapper_spec.dart")).toBeTrue();
13+
});
14+
});
15+
}
16+
17+
class SomeComponent {}

modules_dart/transform/lib/src/transform/template_compiler/reflection/reflection_capabilities.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class NullReflectionCapabilities implements ReflectionCapabilities {
1010

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

13-
bool isReflectionEnabled() {
14-
return false;
15-
}
13+
bool isReflectionEnabled() => false;
1614

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

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

2927
MethodFn method(String name) => _nullMethod;
28+
29+
String importUri(Type type) => './';
3030
}
3131

3232
_nullGetter(Object p) => null;

0 commit comments

Comments
 (0)