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(css): skip url replace for function calls #15544

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}