From 161cf82c13ef903941acdfc699852e445c277497 Mon Sep 17 00:00:00 2001 From: Ted Sander Date: Mon, 9 Mar 2015 23:36:26 -0700 Subject: [PATCH] feat(transformers): Allow more complicated package uris Allow /packages/ identifier to appear anywhere in a uri and parse the AssetId correctly. --- lib/tools/transformer/referenced_uris.dart | 13 ++++--- .../template_cache_generator_spec.dart | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/lib/tools/transformer/referenced_uris.dart b/lib/tools/transformer/referenced_uris.dart index 34d7c0170..41945d004 100644 --- a/lib/tools/transformer/referenced_uris.dart +++ b/lib/tools/transformer/referenced_uris.dart @@ -37,6 +37,7 @@ class _Processor { static const String cacheAnnotationName = 'angular.template_cache_annotation.NgTemplateCache'; static const String componentAnnotationName = 'angular.core.annotation_src.Component'; + static final RegExp pkgRegex = new RegExp(r'.*\/packages\/([^\/]*)(\/.*)'); _Processor(this.transform, this.resolver, this.options, this.skipNonCached, this.templatesOnly, this.urlResolver) { @@ -209,13 +210,13 @@ class _Processor { return null; } if (path.url.isAbsolute(uri)) { - var parts = path.posix.split(uri); - if (parts[1] == 'packages') { - var pkgPath = path.url.join('lib', path.url.joinAll(parts.skip(3))); - return new _CacheEntry(uri, reference, new AssetId(parts[2], pkgPath)); + var match = pkgRegex.firstMatch(uri); + if (match == null) { + warn('Cannot cache non-package absolute URIs. $uri', reference); + return null; } - warn('Cannot cache non-package absolute URIs. $uri', reference); - return null; + var assetId = new AssetId(match.group(1), 'lib/${match.group(2)}'); + return new _CacheEntry(uri, reference, assetId); } // Everything else is a resource in the web directory according to pub; // as all packages URIs were handled above. As specified in this diff --git a/test/tools/transformer/template_cache_generator_spec.dart b/test/tools/transformer/template_cache_generator_spec.dart index 9afb45f57..02fd8021e 100644 --- a/test/tools/transformer/template_cache_generator_spec.dart +++ b/test/tools/transformer/template_cache_generator_spec.dart @@ -185,6 +185,44 @@ main() { 'angular|lib/template_cache.dart': libCacheAnnotation, }); }); + + it('should cache complicated URLs', () { + return generates(setupPhases(), + inputs: { + 'a|web/main.dart': ''' + import 'package:angular/angular.dart'; + import 'dir/foo.dart'; + + // Packages can be anywhere in the uri. + @Component(templateUrl: "/x/y/z/packages/a.b.c/test1.html", + cssUrl: "/packages/b/test1.css") + class A {} + + main() {} + ''', + 'a|web/dir/foo.dart': ''' + import 'package:angular/angular.dart'; + + // Packages may show up multiple times. Only the last value + // will be an identifier. Deeper paths after packages work. + @Component( + templateUrl: "/a/packages/z/packages/a/dir/test2.html", + cssUrl: "/packages/y/dir/dir2/dir3/test2.css") + class B {} + ''', + 'a.b.c|lib/test1.html': htmlContent1, + 'a|lib/dir/test2.html': htmlContent2, + 'angular|lib/angular.dart': libAngular, + 'b|lib/test1.css': cssContent1, + 'y|lib/dir/dir2/dir3/test2.css': cssContent2, + }, + cacheContent: { + '/x/y/z/packages/a.b.c/test1.html': htmlContent1, + '/packages/b/test1.css': cssContent1, + '/a/packages/z/packages/a/dir/test2.html': htmlContent2, + '/packages/y/dir/dir2/dir3/test2.css': cssContent2, + }); + }); }); }