Skip to content

fix(css): sass rebase url in relative imported modules #20067

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
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
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,7 @@ const makeScssWorker = (
...(sassOptions.importers ?? []),
internalImporter,
]
sassOptions.importer ??= internalImporter

const result = await compiler.compileStringAsync(data, sassOptions)
return {
Expand Down
5 changes: 5 additions & 0 deletions playground/css/__tests__/sass-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const sassTest = () => {
const imported = await page.$('.sass')
const atImport = await page.$('.sass-at-import')
const atImportAlias = await page.$('.sass-at-import-alias')
const atImportRelative = await page.$('.sass-at-import-relative')
const urlStartsWithVariable = await page.$('.sass-url-starts-with-variable')
const urlStartsWithVariableInterpolation1 = await page.$(
'.sass-url-starts-with-interpolation1',
Expand All @@ -38,6 +39,10 @@ export const sassTest = () => {
expect(await getBg(atImportAlias)).toMatch(
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getColor(atImportRelative)).toBe('olive')
expect(await getBg(atImportRelative)).toMatch(
isBuild ? /base64/ : '/nested/icon.png',
)
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 @@ -33,6 +33,9 @@ <h1>CSS</h1>
@import from SASS _index: This should be olive and have bg image which url
contains alias
</p>
<p class="sass-at-import-relative">
@import from SASS relative: This should be olive and have bg image
</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-interpolation1">
Expand Down
4 changes: 4 additions & 0 deletions playground/css/nested/relative.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.sass-at-import-relative {
color: olive;
background: url(./icon.png) 10px no-repeat;
}
1 change: 1 addition & 0 deletions playground/css/sass.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@use '=/nested'; // alias + custom index resolving -> /nested/_index.scss
@use '=/nested/partial'; // sass convention: omitting leading _ for partials
@use './nested/relative'; // relative path
@use '@vitejs/test-css-dep'; // package w/ sass entry points
@use '@vitejs/test-css-dep-exports'; // package with a sass export mapping
@use '@vitejs/test-scss-proxy-dep'; // package with a sass proxy import
Expand Down