angular1-template-loader
Version:
Backport of the Angulars webpack loader that inlines your angular templates into angular components.
33 lines (27 loc) • 1.06 kB
JavaScript
// using: regex, capture groups, and capture group variables.
var templateUrlRegex = /templateUrl *=(.*)$/gm;
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) {
if (match.indexOf('require(') !== -1) return match;
// replace: templateUrl = './path/to/template.html'
// with: templateUrl = require('./path/to/template.html')
return "templateUrl =" + replaceStringsWithRequires(url);
});
// Support for tests
if (this.callback) {
this.callback(null, newSource, sourcemap)
} else {
return newSource;
}
};