Skip to content

Commit 561b940

Browse files
hi-ogawapatak-dev
andauthored
fix(css): fix sass file:// reference (#17909)
Co-authored-by: patak <[email protected]>
1 parent dbd6214 commit 561b940

File tree

8 files changed

+48
-22
lines changed

8 files changed

+48
-22
lines changed

packages/vite/src/node/plugins/css.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1085,17 +1085,27 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
10851085
},
10861086

10871087
get sass() {
1088-
return (
1089-
sassResolve ||
1090-
(sassResolve = config.createResolver({
1088+
if (!sassResolve) {
1089+
const resolver = config.createResolver({
10911090
extensions: ['.scss', '.sass', '.css'],
10921091
mainFields: ['sass', 'style'],
10931092
conditions: ['sass', 'style'],
10941093
tryIndex: true,
10951094
tryPrefix: '_',
10961095
preferRelative: true,
1097-
}))
1098-
)
1096+
})
1097+
sassResolve = async (...args) => {
1098+
const id = args[0]
1099+
if (id.startsWith('file://')) {
1100+
const fileUrl = new URL(id)
1101+
if (fs.existsSync(fileUrl)) {
1102+
return fileURLToPath(fileUrl)
1103+
}
1104+
}
1105+
return resolver(...args)
1106+
}
1107+
}
1108+
return sassResolve
10991109
},
11001110

11011111
get less() {

playground/css/__tests__/css.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ test('sass', async () => {
9393
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
9494
)
9595
expect(await getColor(partialImport)).toBe('orchid')
96+
expect(await getColor(await page.$('.sass-file-absolute'))).toBe('orange')
9697

9798
editFile('sass.scss', (code) =>
9899
code.replace('color: $injectedColor', 'color: red'),

playground/css/file-absolute.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.sass-file-absolute {
2+
color: orange;
3+
}

playground/css/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ <h1>CSS</h1>
4141
<p class="sass-dep">
4242
@import dependency w/ no scss entrypoint: this should be lavender
4343
</p>
44+
<p class="sass-file-absolute">
45+
@import "file:///xxx/absolute-path.scss" should be orange
46+
</p>
4447

4548
<p class="less">Less: This should be blue</p>
4649
<p class="less-at-import">

playground/css/sass.scss

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@import 'virtual-dep'; // virtual file added through importer
77
@import '=/pkg-dep'; // package w/out sass field
88
@import '=/weapp.wxss'; // wxss file
9+
@import 'virtual-file-absolute';
910

1011
.sass {
1112
/* injected via vite.config.js */

playground/css/vite.config-sass-modern-compiler.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig } from 'vite'
22
import baseConfig from './vite.config.js'
3+
import configSassModern from './vite.config-sass-modern.js'
34

45
export default defineConfig({
56
...baseConfig,
@@ -8,23 +9,7 @@ export default defineConfig({
89
preprocessorOptions: {
910
...baseConfig.css.preprocessorOptions,
1011
scss: {
11-
api: 'modern-compiler',
12-
additionalData: `$injectedColor: orange;`,
13-
importers: [
14-
{
15-
canonicalize(url) {
16-
return url === 'virtual-dep'
17-
? new URL('custom-importer:virtual-dep')
18-
: null
19-
},
20-
load() {
21-
return {
22-
contents: ``,
23-
syntax: 'scss',
24-
}
25-
},
26-
},
27-
],
12+
...configSassModern.css.preprocessorOptions.scss,
2813
},
2914
},
3015
},

playground/css/vite.config-sass-modern.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { pathToFileURL } from 'node:url'
2+
import path from 'node:path'
13
import { defineConfig } from 'vite'
24
import baseConfig from './vite.config.js'
35

@@ -24,6 +26,19 @@ export default defineConfig({
2426
}
2527
},
2628
},
29+
{
30+
canonicalize(url) {
31+
return url === 'virtual-file-absolute'
32+
? new URL('custom-importer:virtual-file-absolute')
33+
: null
34+
},
35+
load() {
36+
return {
37+
contents: `@import "${pathToFileURL(path.join(import.meta.dirname, 'file-absolute.scss')).href}"`,
38+
syntax: 'scss',
39+
}
40+
},
41+
},
2742
],
2843
},
2944
},

playground/css/vite.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import { pathToFileURL } from 'node:url'
23
import stylus from 'stylus'
34
import { defineConfig } from 'vite'
45

@@ -65,6 +66,13 @@ export default defineConfig({
6566
function (url) {
6667
return url === 'virtual-dep' ? { contents: '' } : null
6768
},
69+
function (url) {
70+
return url === 'virtual-file-absolute'
71+
? {
72+
contents: `@import "${pathToFileURL(path.join(import.meta.dirname, 'file-absolute.scss')).href}"`,
73+
}
74+
: null
75+
},
6876
function (url) {
6977
return url.endsWith('.wxss') ? { contents: '' } : null
7078
},

0 commit comments

Comments
 (0)