MCPcopy
hub / github.com/vitejs/vite / doUrlReplace

Function doUrlReplace

packages/vite/src/node/plugins/css.ts:2152–2191  ·  view source on GitHub ↗
(
  rawUrl: string,
  matched: string,
  replacer: CssUrlReplacer,
  funcName: string = 'url',
)

Source from the content-addressed store, hash-verified

2150 )
2151}
2152async function doUrlReplace(
2153 rawUrl: string,
2154 matched: string,
2155 replacer: CssUrlReplacer,
2156 funcName: string = 'url',
2157) {
2158 let wrap = ''
2159 const first = rawUrl[0]
2160 let unquotedUrl = rawUrl
2161 if (first === `"` || first === `'`) {
2162 wrap = first
2163 unquotedUrl = rawUrl.slice(1, -1)
2164 }
2165 if (skipUrlReplacer(unquotedUrl)) {
2166 return matched
2167 }
2168 // Remove escape sequences to get the actual file name before resolving.
2169 unquotedUrl = unquotedUrl.replace(/\\(\W)/g, '$1')
2170
2171 let newUrl = await replacer(unquotedUrl, rawUrl)
2172 if (newUrl === false) {
2173 return matched
2174 }
2175
2176 // The new url might need wrapping even if the original did not have it, e.g.
2177 // if a space was added during replacement or the URL contains ")"
2178 if (wrap === '' && (newUrl !== encodeURI(newUrl) || newUrl.includes(')'))) {
2179 wrap = '"'
2180 }
2181 // If wrapping in single quotes and newUrl also contains single quotes, switch to double quotes.
2182 // Give preference to double quotes since SVG inlining converts double quotes to single quotes.
2183 if (wrap === "'" && newUrl.includes("'")) {
2184 wrap = '"'
2185 }
2186 // Escape double quotes if they exist (they also tend to be rarer than single quotes)
2187 if (wrap === '"' && newUrl.includes('"')) {
2188 newUrl = newUrl.replace(nonEscapedDoubleQuoteRe, '\\"')
2189 }
2190 return `${funcName}(${wrap}${newUrl}${wrap})`
2191}
2192
2193async function doImportCSSReplace(
2194 rawUrl: string,

Callers 3

rewriteCssUrlsFunction · 0.85
rewriteCssDataUrisFunction · 0.85
rewriteCssImageSetFunction · 0.85

Calls 2

skipUrlReplacerFunction · 0.85
replacerFunction · 0.85

Tested by

no test coverage detected