Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 161cf82

Browse files
Ted Sanderrkirov
authored andcommitted
feat(transformers): Allow more complicated package uris
Allow /packages/ identifier to appear anywhere in a uri and parse the AssetId correctly.
1 parent 16e8e14 commit 161cf82

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

lib/tools/transformer/referenced_uris.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class _Processor {
3737
static const String cacheAnnotationName =
3838
'angular.template_cache_annotation.NgTemplateCache';
3939
static const String componentAnnotationName = 'angular.core.annotation_src.Component';
40+
static final RegExp pkgRegex = new RegExp(r'.*\/packages\/([^\/]*)(\/.*)');
4041

4142
_Processor(this.transform, this.resolver, this.options, this.skipNonCached,
4243
this.templatesOnly, this.urlResolver) {
@@ -209,13 +210,13 @@ class _Processor {
209210
return null;
210211
}
211212
if (path.url.isAbsolute(uri)) {
212-
var parts = path.posix.split(uri);
213-
if (parts[1] == 'packages') {
214-
var pkgPath = path.url.join('lib', path.url.joinAll(parts.skip(3)));
215-
return new _CacheEntry(uri, reference, new AssetId(parts[2], pkgPath));
213+
var match = pkgRegex.firstMatch(uri);
214+
if (match == null) {
215+
warn('Cannot cache non-package absolute URIs. $uri', reference);
216+
return null;
216217
}
217-
warn('Cannot cache non-package absolute URIs. $uri', reference);
218-
return null;
218+
var assetId = new AssetId(match.group(1), 'lib/${match.group(2)}');
219+
return new _CacheEntry(uri, reference, assetId);
219220
}
220221
// Everything else is a resource in the web directory according to pub;
221222
// as all packages URIs were handled above. As specified in this

test/tools/transformer/template_cache_generator_spec.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,44 @@ main() {
185185
'angular|lib/template_cache.dart': libCacheAnnotation,
186186
});
187187
});
188+
189+
it('should cache complicated URLs', () {
190+
return generates(setupPhases(),
191+
inputs: {
192+
'a|web/main.dart': '''
193+
import 'package:angular/angular.dart';
194+
import 'dir/foo.dart';
195+
196+
// Packages can be anywhere in the uri.
197+
@Component(templateUrl: "/x/y/z/packages/a.b.c/test1.html",
198+
cssUrl: "/packages/b/test1.css")
199+
class A {}
200+
201+
main() {}
202+
''',
203+
'a|web/dir/foo.dart': '''
204+
import 'package:angular/angular.dart';
205+
206+
// Packages may show up multiple times. Only the last value
207+
// will be an identifier. Deeper paths after packages work.
208+
@Component(
209+
templateUrl: "/a/packages/z/packages/a/dir/test2.html",
210+
cssUrl: "/packages/y/dir/dir2/dir3/test2.css")
211+
class B {}
212+
''',
213+
'a.b.c|lib/test1.html': htmlContent1,
214+
'a|lib/dir/test2.html': htmlContent2,
215+
'angular|lib/angular.dart': libAngular,
216+
'b|lib/test1.css': cssContent1,
217+
'y|lib/dir/dir2/dir3/test2.css': cssContent2,
218+
},
219+
cacheContent: {
220+
'/x/y/z/packages/a.b.c/test1.html': htmlContent1,
221+
'/packages/b/test1.css': cssContent1,
222+
'/a/packages/z/packages/a/dir/test2.html': htmlContent2,
223+
'/packages/y/dir/dir2/dir3/test2.css': cssContent2,
224+
});
225+
});
188226
});
189227
}
190228

0 commit comments

Comments
 (0)