Skip to content

Commit a5d25ea

Browse files
Copilotsapphi-red
andcommitted
fix(css): avoid warnings for image-set containing __VITE_ASSET__ tokens and add test
Co-authored-by: sapphi-red <[email protected]>
1 parent e2d3f78 commit a5d25ea

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,10 @@ function skipUrlReplacer(unquotedUrl: string) {
20212021
isExternalUrl(unquotedUrl) ||
20222022
isDataUrl(unquotedUrl) ||
20232023
unquotedUrl[0] === '#' ||
2024-
functionCallRE.test(unquotedUrl)
2024+
functionCallRE.test(unquotedUrl) ||
2025+
// skip if it is already a placeholder
2026+
unquotedUrl.startsWith('__VITE_ASSET__') ||
2027+
unquotedUrl.startsWith('__VITE_PUBLIC_ASSET__')
20252028
)
20262029
}
20272030
async function doUrlReplace(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { isBuild, serverLogs } from '~utils'
3+
4+
// Test for CSS image-set warning issue (PR #20520)
5+
// This test ensures that when CSS contains image-set() with url() functions,
6+
// and those URLs get processed to __VITE_ASSET__ tokens, subsequent image-set
7+
// processing doesn't try to resolve the tokens again, which would cause warnings.
8+
test.runIf(isBuild)(
9+
'should not warn about VITE_ASSET tokens in image-set',
10+
async () => {
11+
// The warning we're looking for:
12+
// "__VITE_ASSET__xxx__ referenced in __VITE_ASSET__xxx__ didn't resolve at build time"
13+
14+
const warningPattern = /VITE_ASSET__.*?didn't resolve at build time/
15+
const warningLogs = serverLogs.filter((log) => warningPattern.test(log))
16+
17+
// The issue occurs when:
18+
// 1. CSS has image-set with url() functions
19+
// 2. URL processing converts URLs to __VITE_ASSET__ tokens
20+
// 3. Image-set processing then tries to process the tokens again
21+
// 4. This causes false warnings since tokens can't be resolved as files
22+
23+
// With the fix in skipUrlReplacer(), asset tokens should be skipped
24+
expect(warningLogs.length).toBe(0)
25+
},
26+
)

0 commit comments

Comments
 (0)