Skip to content

Commit 41d255b

Browse files
committed
fix(compiler-sfc): fix skipped srcset transform when using base option
Based on implementation from #4835 due to conflicts fix #4819 close #4834, close #4835
1 parent 57bb37b commit 41d255b

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`compiler sfc: transform srcset srcset w/ explicit base option 1`] = `
4+
"import { createElementVNode as _createElementVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
5+
import _imports_0 from '@/logo.png'
6+
7+
8+
const _hoisted_1 = _imports_0 + ', ' + _imports_0 + ' 2x'
9+
const _hoisted_2 = _imports_0 + ' 1x, ' + \\"/foo/logo.png\\" + ' 2x'
10+
const _hoisted_3 = /*#__PURE__*/_createElementVNode(\\"img\\", { srcset: _hoisted_1 }, null, -1 /* HOISTED */)
11+
const _hoisted_4 = /*#__PURE__*/_createElementVNode(\\"img\\", { srcset: _hoisted_2 }, null, -1 /* HOISTED */)
12+
13+
export function render(_ctx, _cache) {
14+
return (_openBlock(), _createElementBlock(_Fragment, null, [
15+
_hoisted_3,
16+
_hoisted_4
17+
], 64 /* STABLE_FRAGMENT */))
18+
}"
19+
`;
20+
321
exports[`compiler sfc: transform srcset transform srcset 1`] = `
422
"import { createElementVNode as _createElementVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
523
import _imports_0 from './logo.png'

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

+12
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,16 @@ describe('compiler sfc: transform srcset', () => {
8686
expect(code).toMatch(`_createStaticVNode`)
8787
expect(code).toMatchSnapshot()
8888
})
89+
90+
test('srcset w/ explicit base option', () => {
91+
const code = compileWithSrcset(
92+
`
93+
<img srcset="@/logo.png, @/logo.png 2x"/>
94+
<img srcset="@/logo.png 1x, ./logo.png 2x"/>
95+
`,
96+
{ base: '/foo/' },
97+
{ hoistStatic: true }
98+
).code
99+
expect(code).toMatchSnapshot()
100+
})
89101
})

packages/compiler-sfc/src/templateTransformSrcset.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,45 @@ export const transformSrcset: NodeTransform = (
6969
}
7070
}
7171

72-
const hasQualifiedUrl = imageCandidates.some(({ url }) => {
72+
const shouldProcessUrl = (url: string) => {
7373
return (
7474
!isExternalUrl(url) &&
7575
!isDataUrl(url) &&
7676
(options.includeAbsolute || isRelativeUrl(url))
7777
)
78-
})
78+
}
7979
// When srcset does not contain any qualified URLs, skip transforming
80-
if (!hasQualifiedUrl) {
80+
if (!imageCandidates.some(({ url }) => shouldProcessUrl(url))) {
8181
return
8282
}
8383

8484
if (options.base) {
8585
const base = options.base
8686
const set: string[] = []
87-
imageCandidates.forEach(({ url, descriptor }) => {
87+
let needImportTransform = false
88+
89+
imageCandidates.forEach(candidate => {
90+
let { url, descriptor } = candidate
8891
descriptor = descriptor ? ` ${descriptor}` : ``
89-
if (isRelativeUrl(url)) {
90-
set.push((path.posix || path).join(base, url) + descriptor)
92+
if (url[0] === '.') {
93+
candidate.url = (path.posix || path).join(base, url)
94+
set.push(candidate.url + descriptor)
95+
} else if (shouldProcessUrl(url)) {
96+
needImportTransform = true
9197
} else {
9298
set.push(url + descriptor)
9399
}
94100
})
95-
attr.value.content = set.join(', ')
96-
return
101+
102+
if (!needImportTransform) {
103+
attr.value.content = set.join(', ')
104+
return
105+
}
97106
}
98107

99108
const compoundExpression = createCompoundExpression([], attr.loc)
100109
imageCandidates.forEach(({ url, descriptor }, index) => {
101-
if (
102-
!isExternalUrl(url) &&
103-
!isDataUrl(url) &&
104-
(options.includeAbsolute || isRelativeUrl(url))
105-
) {
110+
if (shouldProcessUrl(url)) {
106111
const { path } = parseUrl(url)
107112
let exp: SimpleExpressionNode
108113
if (path) {

0 commit comments

Comments
 (0)