UNPKG

1.23 kBJavaScriptView Raw
1var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
2var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
3var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
4
5module.exports.translate = function(load){
6 var url = document.createElement('a');
7 url.href = load.address;
8
9 var basePathParts = url.pathname.split('/');
10
11 basePathParts.pop();
12 var basePath = basePathParts.join('/');
13
14 var baseHref = document.createElement('a');
15 baseHref.href = this.baseURL;
16 baseHref = baseHref.pathname;
17
18 basePath = basePath.replace(baseHref, '');
19
20 load.source = load.source
21 .replace(templateUrlRegex, function(match, quote, url){
22 let resolvedUrl = url;
23
24 if (url.startsWith('.')) {
25 resolvedUrl = basePath + url.substr(1);
26 }
27
28 return 'templateUrl: "' + resolvedUrl + '"';
29 })
30 .replace(stylesRegex, function(match, relativeUrls) {
31 var urls = [];
32
33 while ((match = stringRegex.exec(relativeUrls)) !== null) {
34 if (match[2].startsWith('.')) {
35 urls.push('"' + basePath + match[2].substr(1) + '"');
36 } else {
37 urls.push('"' + match[2] + '"');
38 }
39 }
40
41 return "styleUrls: [" + urls.join(', ') + "]";
42 });
43
44 return load;
45};