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(less): prevent rebasing @import url(...) #17857

Merged
merged 6 commits into from
Oct 30, 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
8 changes: 5 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1667,10 +1667,11 @@ type CssUrlReplacer = (
) => string | Promise<string>
// https://drafts.csswg.org/css-syntax-3/#identifier-code-point
export const cssUrlRE =
/(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/
/(?<!@import\s+)(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/
export const cssDataUriRE =
/(?<=^|[^\w\-\u0080-\uffff])data-uri\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/
export const importCssRE = /@import ('[^']+\.css'|"[^"]+\.css"|[^'")]+\.css)/
export const importCssRE =
/@import\s+(?:url\()?('[^']+\.css'|"[^"]+\.css"|[^'"\s)]+\.css)/
// Assuming a function name won't be longer than 256 chars
// eslint-disable-next-line regexp/no-unused-capturing-group -- doesn't detect asyncReplace usage
const cssImageSetRE = /(?<=image-set\()((?:[\w-]{1,256}\([^)]*\)|[^)])*)(?=\))/
Expand Down Expand Up @@ -1834,7 +1835,8 @@ async function doImportCSSReplace(
return matched
}

return `@import ${wrap}${await replacer(rawUrl)}${wrap}`
const prefix = matched.includes('url(') ? 'url(' : ''
return `@import ${prefix}${wrap}${await replacer(rawUrl)}${wrap}`
}

async function minifyCSS(
Expand Down
2 changes: 2 additions & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ test('less', async () => {
const imported = await page.$('.less')
const atImport = await page.$('.less-at-import')
const atImportAlias = await page.$('.less-at-import-alias')
const atImportUrlOmmer = await page.$('.less-at-import-url-ommer')
const urlStartsWithVariable = await page.$('.less-url-starts-with-variable')

expect(await getColor(imported)).toBe('blue')
Expand All @@ -125,6 +126,7 @@ test('less', async () => {
expect(await getBg(atImportAlias)).toMatch(
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getColor(atImportUrlOmmer)).toBe('darkorange')
expect(await getBg(urlStartsWithVariable)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
Expand Down
3 changes: 3 additions & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ <h1>CSS</h1>
@import from Less: This should be darkslateblue and have bg image which url
contains alias
</p>
<p class="less-at-import-url-ommer">
@import url() from Less: This should be darkorange
</p>
<p class="less-url-starts-with-variable">url starts with variable</p>

<div class="form-box-data-uri">
Expand Down
2 changes: 2 additions & 0 deletions playground/css/less/components/form.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url('../../less/ommer.less');

.form-box-data-uri {
// data-uri() calls with relative paths should be replaced just like urls.
background-image: data-uri('../images/backgrounds/form-select.svg');
Expand Down
3 changes: 3 additions & 0 deletions playground/css/less/ommer.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.less-at-import-url-ommer {
color: darkorange;
}