Skip to content

Commit 75ff362

Browse files
fix: support the srcset attribute for the source tag (#224)
BREAKING CHANGE: the `srcset` attribute for the `source` tag is now supported by default
1 parent 0bed597 commit 75ff362

File tree

8 files changed

+330
-30
lines changed

8 files changed

+330
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ You may need to specify loaders for images in your configuration (recommended `f
5757

5858
| Name | Type | Default | Description |
5959
| :-------------------------------: | :-----------------: | :------------------------------------------: | :--------------------------------------- |
60-
| **[`attributes`](#attributes)** | `{Array\|String}` | `['img:src']` | Enables/Disables attributes handling |
60+
| **[`attributes`](#attributes)** | `{Array\|String}` | `['img:src', 'source:srcset']` | Enables/Disables attributes handling |
6161
| **[`root`](#root)** | `{String}` | `undefiend` | Allow to handle root-relative attributes |
6262
| **[`interpolate`](#interpolate)** | `{Boolean\|String}` | `false` | Allow to use expressions in HTML syntax |
6363
| **[`minimize`](#minimize)** | `{Boolean\|Object}` | `true` in production mode, otherwise `false` | Tell `html-loader` to minimize HTML |
@@ -66,7 +66,7 @@ You may need to specify loaders for images in your configuration (recommended `f
6666
### `attributes`
6767

6868
Type: `Array|String`
69-
Default: `['img:src']`
69+
Default: `['img:src', 'source:srcset']`
7070

7171
You can specify which tag-attribute combination should be processed by this loader via the query parameter `attributes`.
7272
Pass an array or a space-separated list of `<tag>:<attribute>` combinations. (Default: `attributes=img:src`)

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import parseAttributes from './parseAttributes';
44
import { IDENT_REGEX } from './constants';
55

66
export function getTagsAndAttributes(attributes) {
7-
const defaultAttributes = ['img:src'];
7+
const defaultAttributes = ['img:src', 'source:srcset'];
88

99
if (typeof attributes !== 'undefined') {
1010
if (typeof attributes === 'string') {

test/__snapshots__/attributes-option.test.js.snap

Lines changed: 162 additions & 12 deletions
Large diffs are not rendered by default.

test/__snapshots__/esModule-option.test.js.snap

Lines changed: 45 additions & 3 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ var ___HTML_LOADER_IDENT_1___ = require(\\"./image.png\\");
3030
var ___HTML_LOADER_IDENT_2___ = require(\\"aliasImg\\");
3131
var ___HTML_LOADER_IDENT_3___ = require(\\"./image.png\\");
3232
var ___HTML_LOADER_IDENT_4___ = require(\\"./icons.svg\\");
33+
var ___HTML_LOADER_IDENT_5___ = require(\\"./image.png\\");
34+
var ___HTML_LOADER_IDENT_6___ = require(\\"./image.png\\");
35+
var ___HTML_LOADER_IDENT_7___ = require(\\"./image.png\\");
36+
var ___HTML_LOADER_IDENT_8___ = require(\\"./image.png\\");
37+
var ___HTML_LOADER_IDENT_9___ = require(\\"./image.png\\");
3338
// Exports
34-
module.exports = \\"<h1>My First Heading</h1>\\\\n<p>My first paragraph.</p>\\\\n<h2>An Unordered HTML List</h2>\\\\n\\\\n<ul>\\\\n <li>Coffee</li>\\\\n <li>Tea</li>\\\\n <li>Milk</li>\\\\n</ul>\\\\n\\\\n<h2>An Ordered HTML List</h2>\\\\n\\\\n<ol>\\\\n <li>Coffee</li>\\\\n <li>Tea</li>\\\\n <li>Milk</li>\\\\n</ol>\\\\n\\\\n<script>console.log({\\\\\\"json\\\\\\": \\\\\\"with \\\\\\\\\\\\\\"quotes\\\\\\\\\\\\\\" in value\\\\\\"})</script>\\\\n\\\\n<!-- comment -->\\\\n\\\\n<div>Foo</div>\\\\n\\\\n\\\\n<div>BAR</div>\\\\n\\\\n<input type=\\\\\\"text\\\\\\" />\\\\n\\\\n<script> console.log(1 + 2 + \`\${3 + 3}\`) </script>\\\\n\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_0___) + \\"\\\\\\">\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_1___) + \\"\\\\\\">\\\\n<img src=\\\\\\"/image.png\\\\\\">\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_2___) + \\"\\\\\\">\\\\n<img src=\\\\\\"\\\\\\">\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_3___) + \\"\\\\\\">\\\\n\\\\n<script src=\\\\\\"script.js\\\\\\"></script>\\\\n\\\\n<custom-element custom-src=\\\\\\"image.png\\\\\\" />\\\\n<custom-img custom-src=\\\\\\"image.png\\\\\\"/></custom-element>\\\\n\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_4___) + \\"#hash\\\\\\">\\\\n\\\\n<a href=\\\\\\"mailto:username@exampledomain.com\\\\\\"></a>\\\\n\\";"
39+
module.exports = \\"<h1>My First Heading</h1>\\\\n<p>My first paragraph.</p>\\\\n<h2>An Unordered HTML List</h2>\\\\n\\\\n<ul>\\\\n <li>Coffee</li>\\\\n <li>Tea</li>\\\\n <li>Milk</li>\\\\n</ul>\\\\n\\\\n<h2>An Ordered HTML List</h2>\\\\n\\\\n<ol>\\\\n <li>Coffee</li>\\\\n <li>Tea</li>\\\\n <li>Milk</li>\\\\n</ol>\\\\n\\\\n<script>console.log({\\\\\\"json\\\\\\": \\\\\\"with \\\\\\\\\\\\\\"quotes\\\\\\\\\\\\\\" in value\\\\\\"})</script>\\\\n\\\\n<!-- comment -->\\\\n\\\\n<div>Foo</div>\\\\n\\\\n\\\\n<div>BAR</div>\\\\n\\\\n<input type=\\\\\\"text\\\\\\" />\\\\n\\\\n<script> console.log(1 + 2 + \`\${3 + 3}\`) </script>\\\\n\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_0___) + \\"\\\\\\">\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_1___) + \\"\\\\\\">\\\\n<img src=\\\\\\"/image.png\\\\\\">\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_2___) + \\"\\\\\\">\\\\n<img src=\\\\\\"\\\\\\">\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_3___) + \\"\\\\\\">\\\\n\\\\n<script src=\\\\\\"script.js\\\\\\"></script>\\\\n\\\\n<custom-element custom-src=\\\\\\"image.png\\\\\\" />\\\\n<custom-img custom-src=\\\\\\"image.png\\\\\\"/></custom-element>\\\\n\\\\n<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_4___) + \\"#hash\\\\\\">\\\\n\\\\n<a href=\\\\\\"mailto:username@exampledomain.com\\\\\\"></a>\\\\n\\\\n<img srcset=\\\\\\"image.png 480w, image.png 800w\\\\\\" sizes=\\\\\\"(max-width: 600px) 480px, 800px\\\\\\" src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_5___) + \\"\\\\\\" alt=\\\\\\"Elva dressed as a fairy\\\\\\">\\\\n<img srcset=\\\\\\" image.png 480w , image.png 800w \\\\\\" sizes=\\\\\\"(max-width: 600px) 480px, 800px\\\\\\" src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_6___) + \\"\\\\\\" alt=\\\\\\"Elva dressed as a fairy\\\\\\">\\\\n\\\\n<picture>\\\\n <source media=\\\\\\"(min-width: 650px)\\\\\\" srcset=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_7___) + \\"\\\\\\">\\\\n <source media=\\\\\\"(min-width: 465px)\\\\\\" srcset=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_8___) + \\"\\\\\\">\\\\n <img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_9___) + \\"\\\\\\" alt=\\\\\\"Flowers\\\\\\" style=\\\\\\"width:auto;\\\\\\">\\\\n</picture>\\\\n\\";"
3540
`;
3641

3742
exports[`loader should work: result 1`] = `
@@ -81,6 +86,15 @@ exports[`loader should work: result 1`] = `
8186
<img src=\\"/webpack/public/path/icons.svg#hash\\">
8287
8388
<a href=\\"mailto:username@exampledomain.com\\"></a>
89+
90+
<img srcset=\\"image.png 480w, image.png 800w\\" sizes=\\"(max-width: 600px) 480px, 800px\\" src=\\"/webpack/public/path/image.png\\" alt=\\"Elva dressed as a fairy\\">
91+
<img srcset=\\" image.png 480w , image.png 800w \\" sizes=\\"(max-width: 600px) 480px, 800px\\" src=\\"/webpack/public/path/image.png\\" alt=\\"Elva dressed as a fairy\\">
92+
93+
<picture>
94+
<source media=\\"(min-width: 650px)\\" srcset=\\"/webpack/public/path/image.png\\">
95+
<source media=\\"(min-width: 465px)\\" srcset=\\"/webpack/public/path/image.png\\">
96+
<img src=\\"/webpack/public/path/image.png\\" alt=\\"Flowers\\" style=\\"width:auto;\\">
97+
</picture>
8498
"
8599
`;
86100

0 commit comments

Comments
 (0)