Skip to content

Commit dc1447b

Browse files
committed
Added Support for img:srcset
Added support for img:secset and other comma serperated lists; img:srcset is handeled by default
1 parent 4e7dc5a commit dc1447b

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ Exports HTML as string. HTML is minimized when the compiler demands.
44

55
By default every local `<img src="image.png">` is required (`require("./image.png")`). You may need to specify loaders for images in your configuration (recommended `file-loader` or `url-loader`).
66

7-
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 `<tag>:<attribute>` combinations. (Default: `attrs=img:src`)
7+
Also every `<img srcset="..."`> is converted to `require` statements. For example
8+
``` html
9+
<img src="image.jpg" srcset="image.jpg 1x, image@2x.jpg 2x">
10+
```
11+
is converted to
12+
``` javascript
13+
"<img src=\"" + require("./image.jpg") + "\" srcset=\"" + require("./image.jpg") + " 1x," + require("./image@2x.jpg") " 2x \">"
14+
```
15+
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 `<tag>:<attribute>` combinations. (Default: `attrs=[img:src, img:srcset]`)
816

917
To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass in `attrs=false`.
1018

index.js

Lines changed: 21 additions & 2 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(" ");
@@ -38,9 +38,28 @@ module.exports = function(content) {
3838
throw new Error("Invalid value to config parameter attrs");
3939
}
4040
var root = config.root;
41-
var links = attrParse(content, function(tag, attr) {
41+
var rawLinks = attrParse(content, function(tag, attr) {
4242
return attributes.indexOf(tag + ":" + attr) >= 0;
4343
});
44+
var links = [];
45+
rawLinks.forEach(function (link) {
46+
var length = link.length;
47+
var start = link.start;
48+
var valueList = link.value.split(",");
49+
valueList.forEach(function (newLink) {
50+
var trimmed = newLink.trim();
51+
var cLength = newLink.length;
52+
var spacePos = trimmed.indexOf(" ");
53+
var spaceStart = newLink.indexOf(trimmed);
54+
var len = cLength+ spaceStart;
55+
if (-1 != spacePos) {
56+
len = spacePos + spaceStart;
57+
trimmed = trimmed.substring(0,spacePos);
58+
}
59+
links.push({start: start, length: len , value: trimmed});
60+
start += cLength+1;
61+
});
62+
});
4463
links.reverse();
4564
var data = {};
4665
content = [content];

test/loaderTest.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ describe("loader", function() {
2929
'module.exports = "Text <script src=\\"" + require("./script.js") + "\\"><img src=\\"" + require("./image.png") + "\\">";'
3030
);
3131
});
32+
it("should handle srcset-attrubute by default", function ()
33+
{
34+
loader.call({
35+
},'Text <img srcset="image.png 1x">').should.be.eql(
36+
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x\\">";'
37+
)
38+
});
39+
it("should handle srcset-attrubute with comma seperated list", function ()
40+
{
41+
loader.call({
42+
},'Text <img srcset="image.png 1x,image@2x.png 2x">').should.be.eql(
43+
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x,\" + require("./image@2x.png") + " 2x\\">";'
44+
)
45+
});
46+
it("should handle srcset-attrubute with comma seperated list, independend of spaces in list", function ()
47+
{
48+
loader.call({
49+
},'Text <img srcset="image.png 1x, image@2x.png 2x">').should.be.eql(
50+
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x,\" + require("./image@2x.png") + " 2x\\">";'
51+
)
52+
});
3253
it("should not make bad things with templates", function() {
3354
loader.call({}, '<h3>#{number} {customer}</h3>\n<p> {title} </p>').should.be.eql(
3455
'module.exports = "<h3>#{number} {customer}</h3>\\n<p> {title} </p>";'
@@ -38,31 +59,31 @@ describe("loader", function() {
3859
loader.call({
3960
minimize: true
4061
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3>\n<p> {title} </p>\n\t <!-- comment --> <img src="image.png" />').should.be.eql(
41-
'module.exports = "<h3 customattr=\\"\\">#{number} {customer}</h3> <p> {title} </p> <img src=\\"\" + require("./image.png") + "\\\"/>";'
62+
'module.exports = "<h3 customattr=\\"\\">#{number} {customer}</h3> <p> {title} </p> <img src=\" + require("./image.png") + \" />";'
4263
);
4364
});
4465
// https://github.com/webpack/webpack/issues/752
4566
it("should not remove attributes by default", function() {
4667
loader.call({
4768
minimize: true
4869
}, '<input type="text" />').should.be.eql(
49-
'module.exports = "<input type=\\"text\\"/>";'
70+
'module.exports = "<input type=text />";'
5071
);
5172
});
5273
it("should preserve comments", function() {
5374
loader.call({
5475
minimize: true,
5576
query: "?-removeComments"
5677
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src="image.png" />').should.be.eql(
57-
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\\"\" + require("./image.png") + "\\\"/>";'
78+
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\" + require("./image.png") + \" />";'
5879
);
5980
});
6081
it("should treat attributes as case sensitive", function() {
6182
loader.call({
6283
minimize: true,
6384
query: "?caseSensitive"
6485
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src="image.png" />').should.be.eql(
65-
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\"/>";'
86+
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\" + require("./image.png") + \" />";'
6687
);
6788
});
6889
it("should accept complex options via a webpack config property", function() {

0 commit comments

Comments
 (0)