Skip to content

Commit b39208c

Browse files
committed
perf(compiler-sfc): skip srcset transform if all candidates are external
1 parent 33ba0e3 commit b39208c

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

packages/compiler-sfc/__tests__/__snapshots__/templateTransformSrcset.spec.ts.snap

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ const _hoisted_5 = _imports_0 + ' 2x, ' + _imports_0
139139
const _hoisted_6 = _imports_0 + ' 2x, ' + _imports_0 + ' 3x'
140140
const _hoisted_7 = _imports_0 + ', ' + _imports_0 + ' 2x, ' + _imports_0 + ' 3x'
141141
const _hoisted_8 = _imports_1 + ', ' + _imports_1 + ' 2x'
142-
const _hoisted_9 = \\"https://example.com/logo.png\\" + ', ' + \\"https://example.com/logo.png\\" + ' 2x'
143-
const _hoisted_10 = _imports_1 + ', ' + _imports_0 + ' 2x'
144-
const _hoisted_11 = \\"data:image/png;base64,i\\" + ' 1x, ' + \\"data:image/png;base64,i\\" + ' 2x'
142+
const _hoisted_9 = _imports_1 + ', ' + _imports_0 + ' 2x'
145143
146144
export function render(_ctx, _cache) {
147145
return (_openBlock(), _createBlock(_Fragment, null, [
@@ -183,15 +181,15 @@ export function render(_ctx, _cache) {
183181
}),
184182
_createVNode(\\"img\\", {
185183
src: \\"https://example.com/logo.png\\",
186-
srcset: _hoisted_9
184+
srcset: \\"https://example.com/logo.png, https://example.com/logo.png 2x\\"
187185
}),
188186
_createVNode(\\"img\\", {
189187
src: \\"/logo.png\\",
190-
srcset: _hoisted_10
188+
srcset: _hoisted_9
191189
}),
192190
_createVNode(\\"img\\", {
193191
src: \\"data:image/png;base64,i\\",
194-
srcset: _hoisted_11
192+
srcset: \\"data:image/png;base64,i 1x, data:image/png;base64,i 2x\\"
195193
})
196194
], 64 /* STABLE_FRAGMENT */))
197195
}"

packages/compiler-sfc/src/templateTransformSrcset.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,26 @@ export const transformSrcset: NodeTransform = (
5757
return { url, descriptor }
5858
})
5959

60-
// for data url need recheck url
60+
// data urls contains comma after the ecoding so we need to re-merge
61+
// them
6162
for (let i = 0; i < imageCandidates.length; i++) {
62-
if (imageCandidates[i].url.trim().startsWith('data:')) {
63+
const { url } = imageCandidates[i]
64+
if (isDataUrl(url)) {
6365
imageCandidates[i + 1].url =
64-
imageCandidates[i].url + ',' + imageCandidates[i + 1].url
66+
url + ',' + imageCandidates[i + 1].url
6567
imageCandidates.splice(i, 1)
6668
}
6769
}
6870

69-
// When srcset does not contain any relative URLs, skip transforming
70-
if (
71-
!options.includeAbsolute &&
72-
!imageCandidates.some(({ url }) => isRelativeUrl(url))
73-
) {
71+
const hasQualifiedUrl = imageCandidates.some(({ url }) => {
72+
return (
73+
!isExternalUrl(url) &&
74+
!isDataUrl(url) &&
75+
(options.includeAbsolute || isRelativeUrl(url))
76+
)
77+
})
78+
// When srcset does not contain any qualified URLs, skip transforming
79+
if (!hasQualifiedUrl) {
7480
return
7581
}
7682

0 commit comments

Comments
 (0)