diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 4db92302a18a6e..1051e7527b42d4 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -2433,31 +2433,23 @@ const makeScssWorker = ( return unquotedUrl.startsWith('#{') } - const createInternalImporter = ( - isForRelative: boolean, - ): Sass.Importer<'async'> => ({ + const internalImporter: Sass.Importer<'async'> = { async canonicalize(url, context) { - if (isForRelative) { - // sass passes resolved paths for importer passed to `importer` option - const resolved = new URL(url, context.containingUrl ?? undefined) - if (fs.existsSync(resolved)) return resolved - } else { - const importer = context.containingUrl - ? fileURLToPath(context.containingUrl) - : options.filename - const resolved = await resolvers.sass( - environment, - url, - cleanScssBugUrl(importer), - ) - if ( - resolved && - (resolved.endsWith('.css') || - resolved.endsWith('.scss') || - resolved.endsWith('.sass')) - ) { - return pathToFileURL(resolved) - } + const importer = context.containingUrl + ? fileURLToPath(context.containingUrl) + : options.filename + const resolved = await resolvers.sass( + environment, + url, + cleanScssBugUrl(importer), + ) + if ( + resolved && + (resolved.endsWith('.css') || + resolved.endsWith('.scss') || + resolved.endsWith('.sass')) + ) { + return pathToFileURL(resolved) } return null }, @@ -2480,13 +2472,12 @@ const makeScssWorker = ( result.contents ?? (await fsp.readFile(result.file, 'utf-8')) return { contents, syntax, sourceMapUrl: canonicalUrl } }, - }) - + } sassOptions.importers = [ ...(sassOptions.importers ?? []), - createInternalImporter(false), + internalImporter, ] - sassOptions.importer ??= createInternalImporter(true) + sassOptions.importer ??= internalImporter const result = await compiler.compileStringAsync(data, sassOptions) return { diff --git a/playground/css/vite.config.js b/playground/css/vite.config.js index 71ab69b3c35d67..be3f8dadfa964d 100644 --- a/playground/css/vite.config.js +++ b/playground/css/vite.config.js @@ -64,7 +64,7 @@ export default defineConfig({ resolve: { alias: [ { find: '=', replacement: __dirname }, - { find: /=replace\/(.*)/, replacement: `${__dirname}/$1` }, + { find: /^=replace\/(.*)/, replacement: `${__dirname}/$1` }, { find: 'spacefolder', replacement: __dirname + '/folder with space' }, { find: '#alias', replacement: __dirname + '/aliased/foo.css' }, {