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
This commit is contained in:
Evan You
2022-05-11 16:55:58 +08:00
parent 57bb37bd64
commit 41d255ba5d
3 changed files with 48 additions and 13 deletions

View File

@@ -69,40 +69,45 @@ export const transformSrcset: NodeTransform = (
}
}
const hasQualifiedUrl = imageCandidates.some(({ url }) => {
const shouldProcessUrl = (url: string) => {
return (
!isExternalUrl(url) &&
!isDataUrl(url) &&
(options.includeAbsolute || isRelativeUrl(url))
)
})
}
// When srcset does not contain any qualified URLs, skip transforming
if (!hasQualifiedUrl) {
if (!imageCandidates.some(({ url }) => shouldProcessUrl(url))) {
return
}
if (options.base) {
const base = options.base
const set: string[] = []
imageCandidates.forEach(({ url, descriptor }) => {
let needImportTransform = false
imageCandidates.forEach(candidate => {
let { url, descriptor } = candidate
descriptor = descriptor ? ` ${descriptor}` : ``
if (isRelativeUrl(url)) {
set.push((path.posix || path).join(base, url) + descriptor)
if (url[0] === '.') {
candidate.url = (path.posix || path).join(base, url)
set.push(candidate.url + descriptor)
} else if (shouldProcessUrl(url)) {
needImportTransform = true
} else {
set.push(url + descriptor)
}
})
attr.value.content = set.join(', ')
return
if (!needImportTransform) {
attr.value.content = set.join(', ')
return
}
}
const compoundExpression = createCompoundExpression([], attr.loc)
imageCandidates.forEach(({ url, descriptor }, index) => {
if (
!isExternalUrl(url) &&
!isDataUrl(url) &&
(options.includeAbsolute || isRelativeUrl(url))
) {
if (shouldProcessUrl(url)) {
const { path } = parseUrl(url)
let exp: SimpleExpressionNode
if (path) {