11import process from 'node:process'
22import { pathToFileURL } from 'node:url'
3+ import { existsSync } from 'node:fs'
34import type { Plugin } from 'vite'
45import { isObject , normalizePath , resolveVuetifyBase } from '@vuetify/loader-shared'
56import { isAbsolute , relative as relativePath } from 'pathe'
@@ -21,6 +22,7 @@ export function vuetifyStylesPlugin(
2122 let fileImport = false
2223 const PREFIX = 'vuetify-styles/'
2324 const SSR_PREFIX = `/@${ PREFIX } `
25+ const resolveCss = resolveCssFactory ( )
2426
2527 return < Plugin > {
2628 name : 'vuetify:styles:nuxt' ,
@@ -49,7 +51,7 @@ export function vuetifyStylesPlugin(
4951 } ,
5052 async resolveId ( source , importer , { custom, ssr } ) {
5153 if ( source . startsWith ( PREFIX ) || source . startsWith ( SSR_PREFIX ) ) {
52- if ( source . endsWith ( '.sass' ) )
54+ if ( source . match ( / \. s [ c a ] s s $ / ) )
5355 return source
5456
5557 const idx = source . indexOf ( '?' )
@@ -63,16 +65,14 @@ export function vuetifyStylesPlugin(
6365 && isSubdir ( vuetifyBase , path . isAbsolute ( source ) ? source : importer )
6466 )
6567 ) {
66- if ( options . styles === 'sass' ) {
67- const target = source . replace ( / \. c s s $ / , '.sass' )
68- return this . resolve ( target , importer , { skipSelf : true , custom } )
69- }
68+ if ( options . styles === 'sass' )
69+ return this . resolve ( resolveCss ( source ) , importer , { skipSelf : true , custom } )
7070
7171 const resolution = await this . resolve ( source , importer , { skipSelf : true , custom } )
7272 if ( ! resolution )
7373 return undefined
7474
75- const target = resolution . id . replace ( / \. c s s $ / , '.sass' )
75+ const target = resolveCss ( resolution . id )
7676 if ( isNone ) {
7777 noneFiles . add ( target )
7878 return target
@@ -92,8 +92,9 @@ export function vuetifyStylesPlugin(
9292 : undefined
9393
9494 if ( target ) {
95+ const suffix = target . match ( / \. s c s s / ) ? ';\n' : '\n'
9596 return {
96- code : `@use "${ configFile } "\n @use "${ fileImport ? pathToFileURL ( target ) . href : normalizePath ( target ) } "` ,
97+ code : `@use "${ configFile } "${ suffix } @use "${ fileImport ? pathToFileURL ( target ) . href : normalizePath ( target ) } "${ suffix } ` ,
9798 map : {
9899 mappings : '' ,
99100 } ,
@@ -105,6 +106,25 @@ export function vuetifyStylesPlugin(
105106 }
106107}
107108
109+ function resolveCssFactory ( ) {
110+ const mappings = new Map < string , string > ( )
111+ return ( source : string ) => {
112+ let mapping = mappings . get ( source )
113+ if ( ! mapping ) {
114+ try {
115+ mapping = source . replace ( / \. c s s $ / , '.sass' )
116+ if ( ! existsSync ( mapping ) )
117+ mapping = source . replace ( / \. c s s $ / , '.scss' )
118+ }
119+ catch {
120+ mapping = source . replace ( / \. c s s $ / , '.scss' )
121+ }
122+ mappings . set ( source , mapping )
123+ }
124+ return mapping
125+ }
126+ }
127+
108128function isSubdir ( root : string , test : string ) {
109129 const relative = relativePath ( root , test )
110130 return relative && ! relative . startsWith ( '..' ) && ! isAbsolute ( relative )
0 commit comments