|
3 | 3 | import type {AST, MutableAsset} from '@parcel/types';
|
4 | 4 | import type {PostHTMLNode} from 'posthtml';
|
5 | 5 | import PostHTML from 'posthtml';
|
| 6 | +import {parse, stringify} from 'srcset'; |
6 | 7 | // A list of all attributes that may produce a dependency
|
7 | 8 | // Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
|
8 | 9 | const ATTRS = {
|
@@ -85,24 +86,11 @@ const OPTIONS = {
|
85 | 86 | };
|
86 | 87 |
|
87 | 88 | function collectSrcSetDependencies(asset, srcset, opts) {
|
88 |
| - let newSources = []; |
89 |
| - for (const source of srcset.split(',')) { |
90 |
| - let pair = source.trim().split(' '); |
91 |
| - if (pair.length === 0) { |
92 |
| - continue; |
93 |
| - } |
94 |
| - |
95 |
| - pair[0] = asset.addURLDependency(pair[0], opts); |
96 |
| - newSources.push(pair.join(' ')); |
97 |
| - } |
98 |
| - |
99 |
| - /** |
100 |
| - * https://html.spec.whatwg.org/multipage/images.html#srcset-attribute |
101 |
| - * |
102 |
| - * If an image candidate string in srcset contains a width descriptor or a pixel density descriptor or ASCII whitespace, the following image candidate string must begin with whitespace. |
103 |
| - * So we need to join each image candidate string with ", ". |
104 |
| - */ |
105 |
| - return newSources.join(', '); |
| 89 | + let parsed = parse(srcset).map(({url, ...v}) => ({ |
| 90 | + url: asset.addURLDependency(url, opts), |
| 91 | + ...v, |
| 92 | + })); |
| 93 | + return stringify(parsed); |
106 | 94 | }
|
107 | 95 |
|
108 | 96 | function getAttrDepHandler(attr) {
|
|
0 commit comments