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: url() with variable or relative path in sass/scss is broken (fixes #7651) #8765

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -1101,10 +1101,20 @@ 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}')`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return `url('${rawUrl}')`
return `url(${rawUrl})`

I think rawUrl include quotes if needed.

}
return await doUrlReplace(rawUrl, matched, replacer)
})
}
Expand Down Expand Up @@ -1478,7 +1488,7 @@ async function rebaseUrls(
}

if (hasUrls) {
rebased = await rewriteCssUrls(rebased || content, rebaseFn)
rebased = await rewriteCssUrls(rebased || content, rebaseFn, file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could pass variablePrefix($, @) from rebaseUrls is called instead of file name? It will remove many conditions.

}

if (hasDataUris) {
Expand Down