Skip to content

Commit 806d24d

Browse files
frankfrank
frank
authored and
frank
committed
fix(css):url() with variable or relative path in sass/scss is broken (fix:vitejs#7651)
1 parent 3271266 commit 806d24d

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
@@ -1101,10 +1101,17 @@ UrlRewritePostcssPlugin.postcss = true
11011101

11021102
function rewriteCssUrls(
11031103
css: string,
1104-
replacer: CssUrlReplacer
1104+
replacer: CssUrlReplacer,
1105+
file?: string
11051106
): Promise<string> {
11061107
return asyncReplace(css, cssUrlRE, async (match) => {
11071108
const [matched, rawUrl] = match
1109+
const inLess = file?.endsWith('.less')
1110+
const inSass = file?.endsWith('.sass')
1111+
const inScss = file?.endsWith('.scss')
1112+
if (inLess && rawUrl.startsWith('@') || (inSass || inScss) && rawUrl.startsWith('$')) {
1113+
return `url('${rawUrl}')`
1114+
}
11081115
return await doUrlReplace(rawUrl, matched, replacer)
11091116
})
11101117
}
@@ -1478,7 +1485,7 @@ async function rebaseUrls(
14781485
}
14791486

14801487
if (hasUrls) {
1481-
rebased = await rewriteCssUrls(rebased || content, rebaseFn)
1488+
rebased = await rewriteCssUrls(rebased || content, rebaseFn, file)
14821489
}
14831490

14841491
if (hasDataUris) {

0 commit comments

Comments
 (0)