feat(core): provide support for relative assets for components
Assets defined for `templateUrl` and `styleUrls` can now be loaded in relative to where the component file is placed so long as the `moduleId` is set within the component annotation. Closes #5634 Closes #5634
This commit is contained in:
@ -24,7 +24,7 @@ import {Compiler} from 'angular2/src/core/linker/compiler';
|
||||
import {RuntimeCompiler} from 'angular2/src/compiler/runtime_compiler';
|
||||
import {ElementSchemaRegistry} from 'angular2/src/compiler/schema/element_schema_registry';
|
||||
import {DomElementSchemaRegistry} from 'angular2/src/compiler/schema/dom_element_schema_registry';
|
||||
import {UrlResolver} from 'angular2/src/compiler/url_resolver';
|
||||
import {UrlResolver, DEFAULT_PACKAGE_URL_PROVIDER} from 'angular2/src/compiler/url_resolver';
|
||||
import {AppRootUrl} from 'angular2/src/compiler/app_root_url';
|
||||
import {AnchorBasedAppRootUrl} from 'angular2/src/compiler/anchor_based_app_root_url';
|
||||
import {Parser, Lexer} from 'angular2/src/core/change_detection/change_detection';
|
||||
@ -40,6 +40,7 @@ export const COMPILER_PROVIDERS: Array<Type | Provider | any[]> = CONST_EXPR([
|
||||
TemplateParser,
|
||||
TemplateNormalizer,
|
||||
RuntimeMetadataResolver,
|
||||
DEFAULT_PACKAGE_URL_PROVIDER,
|
||||
StyleCompiler,
|
||||
CommandCompiler,
|
||||
ChangeDetectionCompiler,
|
||||
|
@ -19,6 +19,7 @@ import {reflector} from 'angular2/src/core/reflection/reflection';
|
||||
import {Injectable, Inject, Optional} from 'angular2/src/core/di';
|
||||
import {PLATFORM_DIRECTIVES} from 'angular2/src/core/platform_directives_and_pipes';
|
||||
import {MODULE_SUFFIX} from './util';
|
||||
import {getUrlScheme} from 'angular2/src/compiler/url_resolver';
|
||||
|
||||
@Injectable()
|
||||
export class RuntimeMetadataResolver {
|
||||
@ -107,8 +108,11 @@ function isValidDirective(value: Type): boolean {
|
||||
}
|
||||
|
||||
function calcModuleUrl(type: Type, dirMeta: md.DirectiveMetadata): string {
|
||||
if (isPresent(dirMeta.moduleId)) {
|
||||
return `package:${dirMeta.moduleId}${MODULE_SUFFIX}`;
|
||||
var moduleId = dirMeta.moduleId;
|
||||
if (isPresent(moduleId)) {
|
||||
var scheme = getUrlScheme(moduleId);
|
||||
return isPresent(scheme) && scheme.length > 0 ? moduleId :
|
||||
`package:${moduleId}${MODULE_SUFFIX}`;
|
||||
} else {
|
||||
return reflector.importUri(type);
|
||||
}
|
||||
|
@ -1,22 +1,27 @@
|
||||
library angular2.src.services.url_resolver;
|
||||
|
||||
import 'package:angular2/src/core/di.dart' show Injectable, Provider;
|
||||
import 'package:angular2/src/core/di.dart' show Injectable, Inject, Provider;
|
||||
import 'package:angular2/src/facade/lang.dart' show isPresent, StringWrapper;
|
||||
import 'package:angular2/src/core/application_tokens.dart' show PACKAGE_ROOT_URL;
|
||||
|
||||
UrlResolver createWithoutPackagePrefix() {
|
||||
return new UrlResolver.withUrlPrefix(null);
|
||||
}
|
||||
|
||||
const DEFAULT_PACKAGE_URL_PROVIDER = const Provider(PACKAGE_ROOT_URL, useValue: "/packages");
|
||||
|
||||
@Injectable()
|
||||
class UrlResolver {
|
||||
/// This will be the location where 'package:' Urls will resolve. Default is
|
||||
/// '/packages'
|
||||
final String _packagePrefix;
|
||||
|
||||
const UrlResolver() : _packagePrefix = '/packages';
|
||||
UrlResolver([@Inject(PACKAGE_ROOT_URL) packagePrefix]) :
|
||||
this._packagePrefix = isPresent(packagePrefix) ? StringWrapper.stripRight(packagePrefix, '/') : null;
|
||||
|
||||
/// Creates a UrlResolver that will resolve 'package:' Urls to a different
|
||||
/// prefixed location.
|
||||
const UrlResolver.withUrlPrefix(this._packagePrefix);
|
||||
UrlResolver.withUrlPrefix(this._packagePrefix);
|
||||
|
||||
/**
|
||||
* Resolves the `url` given the `baseUrl`:
|
||||
@ -32,15 +37,21 @@ class UrlResolver {
|
||||
*/
|
||||
String resolve(String baseUrl, String url) {
|
||||
Uri uri = Uri.parse(url);
|
||||
if (!uri.isAbsolute) {
|
||||
|
||||
if (isPresent(baseUrl) && baseUrl.length > 0) {
|
||||
Uri baseUri = Uri.parse(baseUrl);
|
||||
uri = baseUri.resolveUri(uri);
|
||||
}
|
||||
|
||||
if (_packagePrefix != null && uri.scheme == 'package') {
|
||||
return '$_packagePrefix/${uri.path}';
|
||||
var path = StringWrapper.stripLeft(uri.path, '/');
|
||||
return '$_packagePrefix/${path}';
|
||||
} else {
|
||||
return uri.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String getUrlScheme(String url) {
|
||||
return Uri.parse(url).scheme;
|
||||
}
|
||||
|
@ -1,12 +1,21 @@
|
||||
import {Injectable} from 'angular2/src/core/di';
|
||||
import {isPresent, isBlank, RegExpWrapper, normalizeBlank} from 'angular2/src/facade/lang';
|
||||
import {Injectable, Inject} from 'angular2/src/core/di';
|
||||
import {
|
||||
StringWrapper,
|
||||
isPresent,
|
||||
isBlank,
|
||||
RegExpWrapper,
|
||||
normalizeBlank
|
||||
} from 'angular2/src/facade/lang';
|
||||
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {PACKAGE_ROOT_URL} from 'angular2/src/core/application_tokens';
|
||||
import {Provider} from 'angular2/src/core/di';
|
||||
|
||||
export function createWithoutPackagePrefix(): UrlResolver {
|
||||
return new UrlResolver();
|
||||
}
|
||||
|
||||
export var DEFAULT_PACKAGE_URL_PROVIDER = new Provider(PACKAGE_ROOT_URL, {useValue: "/"});
|
||||
|
||||
/**
|
||||
* Used by the {@link Compiler} when resolving HTML and CSS template URLs.
|
||||
@ -17,6 +26,14 @@ export function createWithoutPackagePrefix(): UrlResolver {
|
||||
*/
|
||||
@Injectable()
|
||||
export class UrlResolver {
|
||||
private _packagePrefix: string;
|
||||
|
||||
constructor(@Inject(PACKAGE_ROOT_URL) packagePrefix: string = null) {
|
||||
if (isPresent(packagePrefix)) {
|
||||
this._packagePrefix = StringWrapper.stripRight(packagePrefix, "/") + "/";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the `url` given the `baseUrl`:
|
||||
* - when the `url` is null, the `baseUrl` is returned,
|
||||
@ -29,7 +46,21 @@ export class UrlResolver {
|
||||
* @param {string} url
|
||||
* @returns {string} the resolved URL
|
||||
*/
|
||||
resolve(baseUrl: string, url: string): string { return _resolveUrl(baseUrl, url); }
|
||||
resolve(baseUrl: string, url: string): string {
|
||||
var resolvedUrl = url;
|
||||
if (isPresent(baseUrl) && baseUrl.length > 0) {
|
||||
resolvedUrl = _resolveUrl(baseUrl, resolvedUrl);
|
||||
}
|
||||
if (isPresent(this._packagePrefix) && getUrlScheme(resolvedUrl) == "package") {
|
||||
resolvedUrl = resolvedUrl.replace("package:", this._packagePrefix);
|
||||
}
|
||||
return resolvedUrl;
|
||||
}
|
||||
}
|
||||
|
||||
export function getUrlScheme(url: string): string {
|
||||
var match = _split(url);
|
||||
return (match && match[_ComponentIndex.Scheme]) || "";
|
||||
}
|
||||
|
||||
// The code below is adapted from Traceur:
|
||||
|
Reference in New Issue
Block a user