|
| 1 | +/** |
| 2 | + * Replacement for [angular2-template-loader](https://npmjs.org/package/angular2-template-loader) without `loader-utils`. |
| 3 | + */ |
| 4 | + |
| 5 | +const templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm; |
| 6 | +const stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g; |
| 7 | +const 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 | + this.cacheable && this.cacheable(); |
| 20 | + |
| 21 | + const newSource = source |
| 22 | + .replace(templateUrlRegex, function (match, url) { |
| 23 | + // replace: templateUrl: './path/to/template.html' |
| 24 | + // with: template: require('./path/to/template.html') |
| 25 | + return "template:" + replaceStringsWithRequires(url); |
| 26 | + }) |
| 27 | + .replace(stylesRegex, function (match, urls) { |
| 28 | + // replace: stylesUrl: ['./foo.css', "./baz.css", "./index.component.css"] |
| 29 | + // with: styles: [require('./foo.css'), require("./baz.css"), require("./index.component.css")] |
| 30 | + return "styles:" + replaceStringsWithRequires(urls); |
| 31 | + }); |
| 32 | + |
| 33 | + if (this.callback) { |
| 34 | + this.callback(null, newSource, sourcemap); |
| 35 | + } else { |
| 36 | + return newSource; |
| 37 | + } |
| 38 | +}; |
0 commit comments