1 |
|
2 | var loaderUtils = require("loader-utils");
|
3 |
|
4 |
|
5 | var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm;
|
6 | var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
|
7 | var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
|
8 |
|
9 | function replaceStringsWithRequires(string) {
|
10 | return string.replace(stringRegex, function (match, quote, url) {
|
11 | if (url.charAt(0) !== ".") {
|
12 | url = "./" + url;
|
13 | }
|
14 | return "require('" + url + "')";
|
15 | });
|
16 | }
|
17 |
|
18 | module.exports = function(source, sourcemap) {
|
19 |
|
20 | var config = {};
|
21 | var query = loaderUtils.parseQuery(this.query);
|
22 | var styleProperty = 'styles';
|
23 | var templateProperty = 'template';
|
24 |
|
25 | if (this.options != null) {
|
26 | Object.assign(config, this.options['angular2TemplateLoader']);
|
27 | }
|
28 |
|
29 | Object.assign(config, query);
|
30 |
|
31 | if (config.keepUrl === true) {
|
32 | styleProperty = 'styleUrls';
|
33 | templateProperty = 'templateUrl';
|
34 | }
|
35 |
|
36 |
|
37 | this.cacheable && this.cacheable();
|
38 |
|
39 | var newSource = source.replace(templateUrlRegex, function (match, url) {
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | return templateProperty + ":" + replaceStringsWithRequires(url);
|
45 | })
|
46 | .replace(stylesRegex, function (match, urls) {
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | return styleProperty + ":" + replaceStringsWithRequires(urls);
|
52 | });
|
53 |
|
54 |
|
55 | if (this.callback) {
|
56 | this.callback(null, newSource, sourcemap)
|
57 | } else {
|
58 | return newSource;
|
59 | }
|
60 | };
|