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): root relative import in sass modern API on Windows #18945

Merged
merged 2 commits into from
Dec 16, 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
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;
}
Loading