Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): rewrite url when image-set and url exist at the same time #18868

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1748,16 +1748,26 @@ const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
const replacerForDeclaration = (rawUrl: string) => {
return opts.replacer(rawUrl, importer)
}
const rewriterToUse = isCssImageSet
? rewriteCssImageSet
: rewriteCssUrls
promises.push(
rewriterToUse(declaration.value, replacerForDeclaration).then(
(url) => {
declaration.value = url
},
),
)
if (isCssUrl && isCssImageSet) {
promises.push(
rewriteCssUrls(declaration.value, replacerForDeclaration)
.then((url) => rewriteCssImageSet(url, replacerForDeclaration))
.then((url) => {
declaration.value = url
}),
)
} else {
const rewriterToUse = isCssImageSet
? rewriteCssImageSet
: rewriteCssUrls
promises.push(
rewriterToUse(declaration.value, replacerForDeclaration).then(
(url) => {
declaration.value = url
},
),
)
}
}
})
if (promises.length) {
Expand Down
5 changes: 5 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ describe('css url() references', () => {
})
})

test('image-set and url exist at the same time.', async () => {
const imageSet = await getBg('.image-set-and-url-exsiting-at-same-time')
expect(imageSet).toMatch(assetMatch)
})

test('relative in @import', async () => {
expect(await getBg('.css-url-relative-at-imported')).toMatch(assetMatch)
})
Expand Down
12 changes: 12 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ <h2>CSS url references</h2>
CSS background image-set() inline style (with multiple descriptor)
</span>
</div>
<div
class="image-set-and-url-exsiting-at-same-time"
style="
background-image: image-set(url('./nested/asset.png')),
url('./nested/asset.png');
background-size: 10px 10px;
"
>
<span style="background: #fff"
>CSS background image-set() and url existing at the same time</span
>
</div>
<div class="css-url-relative-at-imported">
<span style="background: #fff"
>CSS background (relative from @imported file in different dir)</span
Expand Down
Loading