Skip to content

Commit 331f45a

Browse files
feat: handle more tags with the src attribute (#226)
BREAKING CHANGE: the `src` attribute of `audio`, `video`, `track`, `embed`, `source`, `input` now handled
1 parent 60a56e7 commit 331f45a

13 files changed

+883
-42
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ You may need to specify loaders for images in your configuration (recommended `f
5555

5656
## Options
5757

58-
| Name | Type | Default | Description |
59-
| :-------------------------------: | :-----------------: | :------------------------------------------: | :--------------------------------------- |
60-
| **[`attributes`](#attributes)** | `{Array\|String}` | `['img:src', ':srcset']` | Enables/Disables attributes handling |
61-
| **[`root`](#root)** | `{String}` | `undefiend` | Allow to handle root-relative attributes |
62-
| **[`interpolate`](#interpolate)** | `{Boolean\|String}` | `false` | Allow to use expressions in HTML syntax |
63-
| **[`minimize`](#minimize)** | `{Boolean\|Object}` | `true` in production mode, otherwise `false` | Tell `html-loader` to minimize HTML |
64-
| **[`esModule`](#esmodule)** | `{Boolean}` | `false` | Use ES modules syntax |
58+
| Name | Type | Default | Description |
59+
| :-------------------------------: | :-----------------: | :----------------------------------------------------------------------------------------------------: | :--------------------------------------- |
60+
| **[`attributes`](#attributes)** | `{Array\|String}` | `[':srcset', 'img:src', 'audio:src', 'video:src', 'track:src', 'embed:src', 'source:src','input:src']` | Enables/Disables attributes handling |
61+
| **[`root`](#root)** | `{String}` | `undefiend` | Allow to handle root-relative attributes |
62+
| **[`interpolate`](#interpolate)** | `{Boolean\|String}` | `false` | Allow to use expressions in HTML syntax |
63+
| **[`minimize`](#minimize)** | `{Boolean\|Object}` | `true` in production mode, otherwise `false` | Tell `html-loader` to minimize HTML |
64+
| **[`esModule`](#esmodule)** | `{Boolean}` | `false` | Use ES modules syntax |
6565

6666
### `attributes`
6767

6868
Type: `Array|String`
69-
Default: `['img:src', ':srcset']`
69+
Default: `[':srcset', 'img:src', 'audio:src', 'video:src', 'track:src', 'embed:src', 'source:src','input:src']`
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import Parser from 'fastparse';
44
const IDENT_REGEX = /___HTML_LOADER_IDENT_[0-9.]+___/g;
55

66
function getTagsAndAttributes(attributes) {
7-
const defaultAttributes = ['img:src', ':srcset'];
7+
const defaultAttributes = [
8+
':srcset',
9+
'img:src',
10+
'audio:src',
11+
'video:src',
12+
'track:src',
13+
'embed:src',
14+
'source:src',
15+
'input:src',
16+
];
817

918
if (typeof attributes !== 'undefined') {
1019
if (typeof attributes === 'string') {
@@ -378,7 +387,7 @@ export function parseAttributes(html, isRelevantTagAttr) {
378387
return;
379388
}
380389

381-
if (this.currentTag === 'img' && name === 'srcset') {
390+
if (name === 'srcset') {
382391
let sourceSet;
383392

384393
try {

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

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

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

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

test/__snapshots__/loader.test.js.snap

Lines changed: 40 additions & 1 deletion
Large diffs are not rendered by default.

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

Lines changed: 150 additions & 9 deletions
Large diffs are not rendered by default.

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

Lines changed: 80 additions & 2 deletions
Large diffs are not rendered by default.

test/attributes-option.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("'attributes' option", () => {
4040
],
4141
},
4242
{
43-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
43+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
4444
loader: 'file-loader',
4545
options: { esModule: false, name: '[name].[ext]' },
4646
},
@@ -75,7 +75,7 @@ describe("'attributes' option", () => {
7575
],
7676
},
7777
{
78-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
78+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
7979
loader: 'file-loader',
8080
options: { esModule: true, name: '[name].[ext]' },
8181
},
@@ -110,7 +110,7 @@ describe("'attributes' option", () => {
110110
],
111111
},
112112
{
113-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
113+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
114114
loader: 'file-loader',
115115
options: { esModule: false, name: '[name].[ext]' },
116116
},
@@ -145,7 +145,7 @@ describe("'attributes' option", () => {
145145
],
146146
},
147147
{
148-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
148+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|ogg|pdf|vtt)$/i,
149149
loader: 'file-loader',
150150
options: { esModule: true, name: '[name].[ext]' },
151151
},

test/fixtures/example.ogg

13.6 KB
Binary file not shown.

test/fixtures/example.pdf

Whitespace-only changes.

0 commit comments

Comments
 (0)