31 lines
903 B
JavaScript
31 lines
903 B
JavaScript
var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm;
|
|
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
|
|
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
|
|
|
|
function replaceStringsWithRequires(string) {
|
|
return string.replace(stringRegex, function (match, quote, url) {
|
|
if (url.charAt(0) !== ".") {
|
|
url = "./" + url;
|
|
}
|
|
return "require('" + url + "')";
|
|
});
|
|
}
|
|
|
|
module.exports = function(source, sourcemap) {
|
|
// Not cacheable during unit tests;
|
|
this.cacheable && this.cacheable();
|
|
|
|
var newSource = source.replace(templateUrlRegex, function (match, url) {
|
|
return "template:" + replaceStringsWithRequires(url);
|
|
}).replace(stylesRegex, function (match, urls) {
|
|
return "styles:" + replaceStringsWithRequires(urls);
|
|
});
|
|
|
|
// Support for tests
|
|
if (this.callback) {
|
|
this.callback(null, newSource, sourcemap)
|
|
} else {
|
|
return newSource;
|
|
}
|
|
};
|