Skip to content

Commit 53ff8db

Browse files
committed
Added possibility to handle srcset
1 parent 4e7dc5a commit 53ff8db

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

index.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function getLoaderConfig(context) {
2626
module.exports = function(content) {
2727
this.cacheable && this.cacheable();
2828
var config = getLoaderConfig(this);
29-
var attributes = ["img:src"];
29+
var attributes = ["img:src", "img:srcset"];
3030
if(config.attrs !== undefined) {
3131
if(typeof config.attrs === "string")
3232
attributes = config.attrs.split(" ");
@@ -45,20 +45,30 @@ module.exports = function(content) {
4545
var data = {};
4646
content = [content];
4747
links.forEach(function(link) {
48-
if(!loaderUtils.isUrlRequest(link.value, root)) return;
49-
50-
var uri = url.parse(link.value);
51-
if (uri.hash !== null && uri.hash !== undefined) {
52-
uri.hash = null;
53-
link.value = uri.format();
54-
link.length = link.value.length;
55-
}
48+
var newValue = link.value.split(",");
49+
var newValue = newValue.map(function (value) {
50+
var valueArray = value.trim().split(" ");
51+
var obj = {
52+
value: valueArray.shift(),
53+
additional: valueArray,
54+
};
55+
if(!loaderUtils.isUrlRequest(obj.value, root)) return;
56+
var uri = url.parse(obj.value);
57+
if (uri.hash !== null && uri.hash !== undefined) {
58+
obj.hash = uri.hash;
59+
uri.hash = null;
60+
obj.value = uri.format();
61+
}
62+
return obj;
63+
});
5664

5765
do {
5866
var ident = randomIdent();
5967
} while(data[ident]);
60-
data[ident] = link.value;
68+
data[ident] = newValue;
6169
var x = content.pop();
70+
71+
6272
content.push(x.substr(link.start + link.length));
6373
content.push(ident);
6474
content.push(x.substr(0, link.start));
@@ -95,9 +105,16 @@ module.exports = function(content) {
95105
} else {
96106
content = JSON.stringify(content);
97107
}
98-
99108
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
100109
if(!data[match]) return match;
101-
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
110+
return data[match].reduce(function (pV,cV, index, array) {
111+
112+
var hash = cV.hash || "";
113+
var additional = cV.additional.length != 0 ? " " + cV.additional.join(" ") : "";
114+
if (index != array.length -1) {
115+
additional += ",";
116+
}
117+
return pV + '" + require(' + JSON.stringify(loaderUtils.urlToRequest(cV.value, root)) + ') + "' + hash + additional;
118+
},"");
102119
}) + ";";
103120
}

0 commit comments

Comments
 (0)