Skip to content

Commit 2bd6077

Browse files
poyohobluwy
andauthoredJun 6, 2023
feat(css): support at import preprocessed styles (#8400)
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
1 parent 4a06e66 commit 2bd6077

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed
 

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

+6
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,12 @@ async function compileCSS(
954954

955955
return id
956956
},
957+
async load(id) {
958+
const code = fs.readFileSync(id, 'utf-8')
959+
const result = await compileCSS(id, code, config)
960+
result.deps?.forEach((dep) => deps.add(dep))
961+
return result.code
962+
},
957963
nameLayer(index) {
958964
return `vite--anon-layer-${getHash(id)}-${index}`
959965
},

‎packages/vite/src/types/shims.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare module 'postcss-import' {
3131
basedir: string,
3232
importOptions: any,
3333
) => string | string[] | Promise<string | string[]>
34+
load: (id: string) => Promise<string>
3435
nameLayer: (index: number, rootFilename: string) => string
3536
}) => Plugin
3637
export = plugin

‎playground/css/__tests__/css.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,7 @@ test('async css order with css modules', async () => {
534534
expect(await getColor('.modules-pink')).toMatchInlineSnapshot('"pink"')
535535
}, true)
536536
})
537+
538+
test('@import scss', async () => {
539+
expect(await getColor('.at-import-scss')).toBe('red')
540+
})

‎playground/css/imported.scss

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$color: red;
2+
3+
.at-import-scss {
4+
color: $color;
5+
}

‎playground/css/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,8 @@ <h1>CSS</h1>
192192
<pre class="aliased-content"></pre>
193193
<p class="aliased-module">import '#alias-module': this should be blue</p>
194194
</div>
195-
195+
<style>
196+
@import url(./imported.scss);
197+
</style>
198+
<div class="at-import-scss">@import scss: this should be red</div>
196199
<script type="module" src="./main.js"></script>

0 commit comments

Comments
 (0)
Please sign in to comment.