Skip to content

Commit b28cc04

Browse files
franksapphi-red
frank
authored andcommitted
fix(css):url() with variable or relative path in sass/scss is broken (fix:vitejs#7651)
1 parent cc4be70 commit b28cc04

File tree

1 file changed

+9
-2
lines changed
  • packages/vite/src/node/plugins

1 file changed

+9
-2
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,17 @@ UrlRewritePostcssPlugin.postcss = true
12091209

12101210
function rewriteCssUrls(
12111211
css: string,
1212-
replacer: CssUrlReplacer
1212+
replacer: CssUrlReplacer,
1213+
file?: string
12131214
): Promise<string> {
12141215
return asyncReplace(css, cssUrlRE, async (match) => {
12151216
const [matched, rawUrl] = match
1217+
const inLess = file?.endsWith('.less')
1218+
const inSass = file?.endsWith('.sass')
1219+
const inScss = file?.endsWith('.scss')
1220+
if (inLess && rawUrl.startsWith('@') || (inSass || inScss) && rawUrl.startsWith('$')) {
1221+
return `url('${rawUrl}')`
1222+
}
12161223
return await doUrlReplace(rawUrl, matched, replacer)
12171224
})
12181225
}
@@ -1621,7 +1628,7 @@ async function rebaseUrls(
16211628
}
16221629

16231630
if (hasUrls) {
1624-
rebased = await rewriteCssUrls(rebased || content, rebaseFn)
1631+
rebased = await rewriteCssUrls(rebased || content, rebaseFn, file)
16251632
}
16261633

16271634
if (hasDataUris) {

0 commit comments

Comments
 (0)