Skip to content

Commit

Permalink
fix(css):url() with variable or relative path in sass/scss is broken …
Browse files Browse the repository at this point in the history
…(fix:vitejs#7651)
  • Loading branch information
frank authored and frank committed Jun 24, 2022
1 parent 3271266 commit 806d24d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -1101,10 +1101,17 @@ UrlRewritePostcssPlugin.postcss = true

function rewriteCssUrls(
css: string,
replacer: CssUrlReplacer
replacer: CssUrlReplacer,
file?: string
): Promise<string> {
return asyncReplace(css, cssUrlRE, async (match) => {
const [matched, rawUrl] = match
const inLess = file?.endsWith('.less')
const inSass = file?.endsWith('.sass')
const inScss = file?.endsWith('.scss')
if (inLess && rawUrl.startsWith('@') || (inSass || inScss) && rawUrl.startsWith('$')) {
return `url('${rawUrl}')`
}
return await doUrlReplace(rawUrl, matched, replacer)
})
}
Expand Down Expand Up @@ -1478,7 +1485,7 @@ async function rebaseUrls(
}

if (hasUrls) {
rebased = await rewriteCssUrls(rebased || content, rebaseFn)
rebased = await rewriteCssUrls(rebased || content, rebaseFn, file)
}

if (hasDataUris) {
Expand Down

0 comments on commit 806d24d

Please sign in to comment.