Skip to content

Commit 21680bd

Browse files
hi-ogawasapphi-red
andauthoredDec 17, 2024··
fix(css): skip non css in custom sass importer (#18970)
Co-authored-by: 翠 / green <green@sapphi.red>
1 parent 62fad6d commit 21680bd

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed
 

‎.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ playground/html/valid.html
1313
playground/external/public/slash@3.0.0.js
1414
playground/ssr-html/public/slash@3.0.0.js
1515
playground/worker/classic-worker.js
16+
playground/css/weapp.wxss

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

+23-2
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,20 @@ const makeModernScssWorker = (
23802380
? fileURLToPath(context.containingUrl)
23812381
: options.filename
23822382
const resolved = await internalCanonicalize(url, importer)
2383-
return resolved ? pathToFileURL(resolved) : null
2383+
if (
2384+
resolved &&
2385+
// only limit to these extensions because:
2386+
// - for the `@import`/`@use`s written in file loaded by `load` function,
2387+
// the `canonicalize` function of that `importer` is called first
2388+
// - the `load` function of an importer is only called for the importer
2389+
// that returned a non-null result from its `canonicalize` function
2390+
(resolved.endsWith('.css') ||
2391+
resolved.endsWith('.scss') ||
2392+
resolved.endsWith('.sass'))
2393+
) {
2394+
return pathToFileURL(resolved)
2395+
}
2396+
return null
23842397
},
23852398
async load(canonicalUrl) {
23862399
const ext = path.extname(canonicalUrl.pathname)
@@ -2469,7 +2482,15 @@ const makeModernCompilerScssWorker = (
24692482
url,
24702483
cleanScssBugUrl(importer),
24712484
)
2472-
return resolved ? pathToFileURL(resolved) : null
2485+
if (
2486+
resolved &&
2487+
(resolved.endsWith('.css') ||
2488+
resolved.endsWith('.scss') ||
2489+
resolved.endsWith('.sass'))
2490+
) {
2491+
return pathToFileURL(resolved)
2492+
}
2493+
return null
24732494
},
24742495
async load(canonicalUrl) {
24752496
const ext = path.extname(canonicalUrl.pathname)

‎playground/css/nested/_index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@use 'sass:string';
22
@use '/nested/root-relative'; // root relative path
3+
@use '../weapp.wxss'; // test user's custom importer in a file loaded by vite's custom importer
34

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

‎playground/css/vite.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default defineConfig({
6565
importers: [
6666
{
6767
canonicalize(url) {
68-
return url === 'virtual-dep'
68+
return url === 'virtual-dep' || url.endsWith('.wxss')
6969
? new URL('custom-importer:virtual-dep')
7070
: null
7171
},

‎playground/css/weapp.wxss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is not css

0 commit comments

Comments
 (0)
Please sign in to comment.