-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
fix(css): should not wrap with double quote when the url rebase feature bailed out #20068
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
Merged
patak-dev
merged 4 commits into
vitejs:main
from
sapphi-red:fix/css-sass-rebase-url-starting-should-not-wrap
May 23, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1901,10 +1901,15 @@ type CssUrlResolver = ( | |
) => | ||
| [url: string, id: string | undefined] | ||
| Promise<[url: string, id: string | undefined]> | ||
/** | ||
* replace URL references | ||
* | ||
* When returning `false`, it keeps the content as-is | ||
*/ | ||
type CssUrlReplacer = ( | ||
url: string, | ||
importer?: string, | ||
) => string | Promise<string> | ||
unquotedUrl: string, | ||
rawUrl: string, | ||
) => string | false | Promise<string | false> | ||
// https://drafts.csswg.org/css-syntax-3/#identifier-code-point | ||
export const cssUrlRE = | ||
/(?<!@import\s+)(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/ | ||
|
@@ -2034,12 +2039,12 @@ async function rewriteCssImageSet( | |
return url | ||
}) | ||
} | ||
function skipUrlReplacer(rawUrl: string) { | ||
function skipUrlReplacer(unquotedUrl: string) { | ||
return ( | ||
isExternalUrl(rawUrl) || | ||
isDataUrl(rawUrl) || | ||
rawUrl[0] === '#' || | ||
functionCallRE.test(rawUrl) | ||
isExternalUrl(unquotedUrl) || | ||
isDataUrl(unquotedUrl) || | ||
unquotedUrl[0] === '#' || | ||
functionCallRE.test(unquotedUrl) | ||
) | ||
} | ||
async function doUrlReplace( | ||
|
@@ -2050,16 +2055,20 @@ async function doUrlReplace( | |
) { | ||
let wrap = '' | ||
const first = rawUrl[0] | ||
let unquotedUrl = rawUrl | ||
if (first === `"` || first === `'`) { | ||
wrap = first | ||
rawUrl = rawUrl.slice(1, -1) | ||
unquotedUrl = rawUrl.slice(1, -1) | ||
} | ||
if (skipUrlReplacer(unquotedUrl)) { | ||
return matched | ||
} | ||
|
||
if (skipUrlReplacer(rawUrl)) { | ||
let newUrl = await replacer(unquotedUrl, rawUrl) | ||
if (newUrl === false) { | ||
return matched | ||
} | ||
|
||
let newUrl = await replacer(rawUrl) | ||
// The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement | ||
if (wrap === '' && newUrl !== encodeURI(newUrl)) { | ||
wrap = '"' | ||
|
@@ -2083,16 +2092,22 @@ async function doImportCSSReplace( | |
) { | ||
let wrap = '' | ||
const first = rawUrl[0] | ||
let unquotedUrl = rawUrl | ||
if (first === `"` || first === `'`) { | ||
wrap = first | ||
rawUrl = rawUrl.slice(1, -1) | ||
unquotedUrl = rawUrl.slice(1, -1) | ||
} | ||
if (skipUrlReplacer(unquotedUrl)) { | ||
return matched | ||
} | ||
if (isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl[0] === '#') { | ||
|
||
const newUrl = await replacer(unquotedUrl, rawUrl) | ||
if (newUrl === false) { | ||
return matched | ||
} | ||
|
||
const prefix = matched.includes('url(') ? 'url(' : '' | ||
return `@import ${prefix}${wrap}${await replacer(rawUrl)}${wrap}` | ||
return `@import ${prefix}${wrap}${newUrl}${wrap}` | ||
} | ||
|
||
async function minifyCSS( | ||
|
@@ -2392,14 +2407,24 @@ const makeModernScssWorker = ( | |
return resolved ?? null | ||
} | ||
|
||
const skipRebaseUrls = (unquotedUrl: string, rawUrl: string) => { | ||
const isQuoted = rawUrl[0] === '"' || rawUrl[0] === "'" | ||
// matches `url($foo)` | ||
if (!isQuoted && unquotedUrl[0] === '$') { | ||
return true | ||
} | ||
// matches `url(#{foo})` and `url('#{foo}')` | ||
return unquotedUrl.startsWith('#{') | ||
} | ||
|
||
const internalLoad = async (file: string, rootFile: string) => { | ||
const result = await rebaseUrls( | ||
environment, | ||
file, | ||
rootFile, | ||
alias, | ||
'$', | ||
resolvers.sass, | ||
skipRebaseUrls, | ||
) | ||
if (result.contents) { | ||
return result.contents | ||
|
@@ -2529,6 +2554,16 @@ const makeModernCompilerScssWorker = ( | |
sassOptions.url = pathToFileURL(options.filename) | ||
sassOptions.sourceMap = options.enableSourcemap | ||
|
||
const skipRebaseUrls = (unquotedUrl: string, rawUrl: string) => { | ||
const isQuoted = rawUrl[0] === '"' || rawUrl[0] === "'" | ||
// matches `url($foo)` | ||
if (!isQuoted && unquotedUrl[0] === '$') { | ||
return true | ||
} | ||
// matches `url(#{foo})` and `url('#{foo}')` | ||
return unquotedUrl.startsWith('#{') | ||
} | ||
Comment on lines
+2557
to
+2565
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
const internalImporter: Sass.Importer<'async'> = { | ||
async canonicalize(url, context) { | ||
const importer = context.containingUrl | ||
|
@@ -2562,8 +2597,8 @@ const makeModernCompilerScssWorker = ( | |
fileURLToPath(canonicalUrl), | ||
options.filename, | ||
alias, | ||
'$', | ||
resolvers.sass, | ||
skipRebaseUrls, | ||
) | ||
const contents = | ||
result.contents ?? (await fsp.readFile(result.file, 'utf-8')) | ||
|
@@ -2709,8 +2744,8 @@ async function rebaseUrls( | |
file: string, | ||
rootFile: string, | ||
alias: Alias[], | ||
variablePrefix: string, | ||
resolver: ResolveIdFn, | ||
ignoreUrl?: (unquotedUrl: string, rawUrl: string) => boolean, | ||
): Promise<{ file: string; contents?: string }> { | ||
file = path.resolve(file) // ensure os-specific flashes | ||
// in the same dir, no need to rebase | ||
|
@@ -2733,20 +2768,22 @@ async function rebaseUrls( | |
} | ||
|
||
let rebased | ||
const rebaseFn = async (url: string) => { | ||
if (url[0] === '/') return url | ||
// ignore url's starting with variable | ||
if (url.startsWith(variablePrefix)) return url | ||
const rebaseFn = async (unquotedUrl: string, rawUrl: string) => { | ||
if (ignoreUrl?.(unquotedUrl, rawUrl)) return false | ||
if (unquotedUrl[0] === '/') return unquotedUrl | ||
// match alias, no need to rewrite | ||
for (const { find } of alias) { | ||
const matches = | ||
typeof find === 'string' ? url.startsWith(find) : find.test(url) | ||
typeof find === 'string' | ||
? unquotedUrl.startsWith(find) | ||
: find.test(unquotedUrl) | ||
if (matches) { | ||
return url | ||
return unquotedUrl | ||
} | ||
} | ||
const absolute = | ||
(await resolver(environment, url, file)) || path.resolve(fileDir, url) | ||
(await resolver(environment, unquotedUrl, file)) || | ||
path.resolve(fileDir, unquotedUrl) | ||
const relative = path.relative(rootDir, absolute) | ||
return normalizePath(relative) | ||
} | ||
|
@@ -2778,6 +2815,13 @@ const makeLessWorker = ( | |
alias: Alias[], | ||
maxWorkers: number | undefined, | ||
) => { | ||
const skipRebaseUrls = (unquotedUrl: string, _rawUrl: string) => { | ||
// matches both | ||
// - interpolation: `url('@{foo}')` | ||
// - variable: `url(@foo)` | ||
return unquotedUrl[0] === '@' | ||
} | ||
|
||
const viteLessResolve = async ( | ||
filename: string, | ||
dir: string, | ||
|
@@ -2802,8 +2846,8 @@ const makeLessWorker = ( | |
resolved, | ||
rootFile, | ||
alias, | ||
'@', | ||
resolvers.less, | ||
skipRebaseUrls, | ||
) | ||
return { | ||
resolved, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now checks
functionCallRE.test(unquotedUrl)
additionally.The following code works and the URL replacement should be skipped.