Skip to content

Commit

Permalink
fix(css): root relative import in sass modern API on Windows (#18945)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 16, 2024
1 parent 27f691b commit c4b532c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import {
urlRE,
} from '../utils'
import type { Logger } from '../logger'
import { cleanUrl, slash } from '../../shared/utils'
import { cleanUrl, isWindows, slash } from '../../shared/utils'
import { createBackCompatIdResolver } from '../idResolver'
import type { ResolveIdFn } from '../idResolver'
import { PartialEnvironment } from '../baseEnvironment'
Expand Down Expand Up @@ -1162,8 +1162,14 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
preferRelative: true,
})
sassResolve = async (...args) => {
// the modern API calls `canonicalize` with resolved file URLs
// for relative URLs before raw specifiers
if (args[1].startsWith('file://')) {
args[1] = fileURLToPath(args[1])
args[1] = fileURLToPath(args[1], {
windows:
// file:///foo cannot be converted to path with windows mode
isWindows && args[1].startsWith('file:///') ? false : undefined,
})
}
return resolver(...args)
}
Expand Down
1 change: 1 addition & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ test('sass', async () => {
expect(await getColor(partialImport)).toBe('orchid')
expect(await getColor(await page.$('.sass-file-absolute'))).toBe('orange')
expect(await getColor(await page.$('.sass-dir-index'))).toBe('orange')
expect(await getColor(await page.$('.sass-root-relative'))).toBe('orange')

if (isBuild) return

Expand Down
3 changes: 3 additions & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ <h1>CSS</h1>
@import "file:///xxx/absolute-path.scss" should be orange
</p>
<p class="sass-dir-index">@import "./dir" should be orange</p>
<p class="sass-root-relative">
@import "/nested/root-relative.scss" should be orange
</p>

<p class="less">Less: This should be blue</p>
<p class="less-at-import">
Expand Down
1 change: 1 addition & 0 deletions playground/css/nested/_index.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use 'sass:string';
@use '/nested/root-relative'; // root relative path

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

Expand Down
3 changes: 3 additions & 0 deletions playground/css/nested/root-relative.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sass-root-relative {
color: orange;
}

0 comments on commit c4b532c

Please # to comment.