11import { readFile } from 'node:fs/promises'
22import path from 'node:path'
33import { fileURLToPath , pathToFileURL } from 'node:url'
4+ import { getSassResolver , resolveWithResolver } from './resolve.ts'
45import type { PreprocessorOptions } from './options.ts'
56
67export type PreprocessorLang = 'sass' | 'scss' | 'less' | 'styl' | 'stylus'
@@ -251,8 +252,7 @@ async function compileSass(
251252 ? fileURLToPath ( context . containingUrl )
252253 : filename
253254
254- const dir = path . dirname ( importer )
255- const resolved = await tryResolveScss ( url , dir )
255+ const resolved = await tryResolveScss ( url , importer )
256256 if ( resolved ) {
257257 return pathToFileURL ( resolved )
258258 }
@@ -291,8 +291,9 @@ async function compileSass(
291291
292292async function tryResolveScss (
293293 url : string ,
294- dir : string ,
294+ importer : string ,
295295) : Promise < string | undefined > {
296+ const dir = path . dirname ( importer )
296297 const { existsSync } = await import ( 'node:fs' )
297298 const extensions = [ '.scss' , '.sass' , '.css' ]
298299 const prefixes = [ '' , '_' ]
@@ -306,28 +307,28 @@ async function tryResolveScss(
306307 )
307308 if ( existsSync ( file ) ) return file
308309 }
309- return undefined
310- }
311-
312- for ( const ext of extensions ) {
313- for ( const prefix of prefixes ) {
314- const file = path . resolve (
315- dir ,
316- path . dirname ( url ) ,
317- prefix + path . basename ( url ) + ext ,
318- )
319- if ( existsSync ( file ) ) return file
310+ } else {
311+ for ( const ext of extensions ) {
312+ for ( const prefix of prefixes ) {
313+ const file = path . resolve (
314+ dir ,
315+ path . dirname ( url ) ,
316+ prefix + path . basename ( url ) + ext ,
317+ )
318+ if ( existsSync ( file ) ) return file
319+ }
320320 }
321- }
322321
323- for ( const ext of extensions ) {
324- for ( const prefix of prefixes ) {
325- const file = path . resolve ( dir , url , `${ prefix } index${ ext } ` )
326- if ( existsSync ( file ) ) return file
322+ for ( const ext of extensions ) {
323+ for ( const prefix of prefixes ) {
324+ const file = path . resolve ( dir , url , `${ prefix } index${ ext } ` )
325+ if ( existsSync ( file ) ) return file
326+ }
327327 }
328328 }
329329
330- return undefined
330+ // Fall back to node_modules resolution
331+ return resolveWithResolver ( getSassResolver ( ) , url , importer )
331332}
332333
333334let _sass : any
0 commit comments