Skip to content

Commit

Permalink
fix(css): skip url replace for function calls (#15544)
Browse files Browse the repository at this point in the history
  • Loading branch information
smeng9 authored Jan 10, 2024
1 parent a5c6d3d commit 21a52e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const commonjsProxyRE = /\?commonjs-proxy/
const inlineRE = /[?&]inline\b/
const inlineCSSRE = /[?&]inline-css\b/
const styleAttrRE = /[?&]style-attr\b/
const varRE = /^var\(/i
const functionCallRE = /^[A-Z_][\w-]*\(/i
const nonEscapedDoubleQuoteRe = /(?<!\\)(")/g

const cssBundleName = 'style.css'
Expand Down Expand Up @@ -1517,7 +1517,7 @@ function skipUrlReplacer(rawUrl: string) {
isExternalUrl(rawUrl) ||
isDataUrl(rawUrl) ||
rawUrl[0] === '#' ||
varRE.test(rawUrl)
functionCallRE.test(rawUrl)
)
}
async function doUrlReplace(
Expand Down
6 changes: 6 additions & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ test('sass', async () => {
const atImport = await page.$('.sass-at-import')
const atImportAlias = await page.$('.sass-at-import-alias')
const urlStartsWithVariable = await page.$('.sass-url-starts-with-variable')
const urlStartsWithFunctionCall = await page.$(
'.sass-url-starts-with-function-call',
)
const partialImport = await page.$('.sass-partial')

expect(await getColor(imported)).toBe('orange')
Expand All @@ -86,6 +89,9 @@ test('sass', async () => {
expect(await getBg(urlStartsWithVariable)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getBg(urlStartsWithFunctionCall)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getColor(partialImport)).toBe('orchid')

editFile('sass.scss', (code) =>
Expand Down
3 changes: 3 additions & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ <h1>CSS</h1>
</p>
<p class="sass-partial">@import from SASS _partial: This should be orchid</p>
<p class="sass-url-starts-with-variable">url starts with variable</p>
<p class="sass-url-starts-with-function-call">
url starts with function call
</p>
<p>Imported SASS string:</p>
<pre class="imported-sass"></pre>
<p class="sass-dep">
Expand Down
8 changes: 8 additions & 0 deletions playground/css/nested/_index.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@use 'sass:string';

@import './css-in-scss.css';

.sass-at-import {
Expand All @@ -15,3 +17,9 @@ $var: '/ok.png';
background: url($var);
background-position: center;
}

$var2: '/OK.PNG';
.sass-url-starts-with-function-call {
background: url(to-lower-case($var2));
background-position: center;
}

0 comments on commit 21a52e6

Please sign in to comment.