chore: replace `URL` dependence in moduleId loader (#3418)

IE, Edge, and some other browsers don't support `URL`. Replace it with standard DOM techniques.
This commit is contained in:
Jesús Rodríguez 2017-03-23 23:59:54 +01:00 committed by Ward Bell
parent 3cf43d66d1
commit a083fdbe8b
1 changed files with 8 additions and 6 deletions

View File

@ -3,15 +3,17 @@ var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g; var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
module.exports.translate = function(load){ module.exports.translate = function(load){
var url = document.createElement('a');
var url = new URL(load.address); url.href = load.address;
var basePathParts = url.pathname.split('/'); var basePathParts = url.pathname.split('/');
basePathParts.pop(); basePathParts.pop();
var basePath = basePathParts.join('/'); var basePath = basePathParts.join('/');
var baseHref = new URL(this.baseURL).pathname; var baseHref = document.createElement('a');
baseHref.href = this.baseURL;
baseHref = baseHref.pathname;
basePath = basePath.replace(baseHref, ''); basePath = basePath.replace(baseHref, '');
@ -23,16 +25,16 @@ module.exports.translate = function(load){
resolvedUrl = basePath + url.substr(1); resolvedUrl = basePath + url.substr(1);
} }
return `templateUrl: '${resolvedUrl}'`; return 'templateUrl: "' + resolvedUrl + '"';
}) })
.replace(stylesRegex, function(match, relativeUrls) { .replace(stylesRegex, function(match, relativeUrls) {
var urls = []; var urls = [];
while ((match = stringRegex.exec(relativeUrls)) !== null) { while ((match = stringRegex.exec(relativeUrls)) !== null) {
if (match[2].startsWith('.')) { if (match[2].startsWith('.')) {
urls.push(`'${basePath}${match[2].substr(1)}'`); urls.push('"' + basePath + match[2].substr(1) + '"');
} else { } else {
urls.push(`'${match[2]}'`); urls.push('"' + match[2] + '"');
} }
} }