diff --git a/README.md b/README.md index eb4efbed..a413ba2b 100644 --- a/README.md +++ b/README.md @@ -25,15 +25,7 @@ npm i -D html-loader By default every local `` is required (`require('./image.png')`). You may need to specify loaders for images in your configuration (recommended `file-loader` or `url-loader`). -Also every ` is converted to `require` statements. For example -``` html - -``` -is converted to -``` javascript -"" -``` -You can specify which tag-attribute combination should be processed by this loader via the query parameter `attrs`. Pass an array or a space-separated list of `:` combinations. (Default: `attrs=[img:src, img:srcset]`) +You can specify which tag-attribute combination should be processed by this loader via the query parameter `attrs`. Pass an array or a space-separated list of `:` combinations. (Default: `attrs=img:src`) To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass in `attrs=false`. diff --git a/index.js b/index.js index ee697e72..d751899d 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ function getLoaderConfig(context) { module.exports = function(content) { this.cacheable && this.cacheable(); var config = getLoaderConfig(this); - var attributes = ["img:src", "img:srcset"]; + var attributes = ["img:src"]; if(config.attrs !== undefined) { if(typeof config.attrs === "string") attributes = config.attrs.split(" "); @@ -38,56 +38,27 @@ module.exports = function(content) { throw new Error("Invalid value to config parameter attrs"); } var root = config.root; - var rawLinks = attrParse(content, function(tag, attr) { + var links = attrParse(content, function(tag, attr) { return attributes.indexOf(tag + ":" + attr) >= 0; }); - var links = []; - rawLinks.forEach(function (link) { - var length = link.length; - var start = link.start; - var valueList = link.value.split(","); - valueList.forEach(function (newLink) { - var trimmed = newLink.trim(); - var cLength = newLink.length; - var spacePos = trimmed.indexOf(" "); - var spaceStart = newLink.indexOf(trimmed); - var len = cLength+ spaceStart; - if (-1 != spacePos) { - len = spacePos + spaceStart; - trimmed = trimmed.substring(0,spacePos); - } - links.push({start: start, length: len , value: trimmed}); - start += cLength+1; - }); - }); links.reverse(); var data = {}; content = [content]; links.forEach(function(link) { - var newValue = link.value.split(","); - var newValue = newValue.map(function (value) { - var valueArray = value.trim().split(" "); - var obj = { - value: valueArray.shift(), - additional: valueArray, - }; - if(!loaderUtils.isUrlRequest(obj.value, root)) return; - var uri = url.parse(obj.value); - if (uri.hash !== null && uri.hash !== undefined) { - obj.hash = uri.hash; - uri.hash = null; - obj.value = uri.format(); - } - return obj; - }); + if(!loaderUtils.isUrlRequest(link.value, root)) return; + + var uri = url.parse(link.value); + if (uri.hash !== null && uri.hash !== undefined) { + uri.hash = null; + link.value = uri.format(); + link.length = link.value.length; + } do { var ident = randomIdent(); } while(data[ident]); - data[ident] = newValue; + data[ident] = link.value; var x = content.pop(); - - content.push(x.substr(link.start + link.length)); content.push(ident); content.push(x.substr(0, link.start)); @@ -124,16 +95,9 @@ module.exports = function(content) { } else { content = JSON.stringify(content); } + return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) { if(!data[match]) return match; - return data[match].reduce(function (pV,cV, index, array) { - - var hash = cV.hash || ""; - var additional = cV.additional.length != 0 ? " " + cV.additional.join(" ") : ""; - if (index != array.length -1) { - additional += ","; - } - return pV + '" + require(' + JSON.stringify(loaderUtils.urlToRequest(cV.value, root)) + ') + "' + hash + additional; - },""); + return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "'; }) + ";"; } diff --git a/test/loaderTest.js b/test/loaderTest.js index 370cc1a4..0ef19d81 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -29,27 +29,6 @@ describe("loader", function() { 'module.exports = "Text