Skip to content

Commit 8213962

Browse files
authored
fix(styles): resolve new scss component styles (#305)
1 parent e1f00ab commit 8213962

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/utils/configure-vite.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,16 @@ export function configureVite(configKey: string, nuxt: Nuxt, ctx: VuetifyNuxtCon
5454
viteInlineConfig.css.preprocessorOptions ??= {}
5555
viteInlineConfig.css.preprocessorOptions.sass ??= {}
5656
viteInlineConfig.css.preprocessorOptions.sass.api = 'modern-compiler'
57+
viteInlineConfig.css.preprocessorOptions.scss ??= {}
58+
viteInlineConfig.css.preprocessorOptions.scss.api = 'modern-compiler'
5759
}
5860
else {
5961
viteInlineConfig.css ??= {}
6062
viteInlineConfig.css.preprocessorOptions ??= {}
6163
viteInlineConfig.css.preprocessorOptions.sass ??= {}
6264
viteInlineConfig.css.preprocessorOptions.sass.api = 'modern'
65+
viteInlineConfig.css.preprocessorOptions.scss ??= {}
66+
viteInlineConfig.css.preprocessorOptions.scss.api = 'modern'
6367
if (!('preprocessorMaxWorkers' in viteInlineConfig.css))
6468
viteInlineConfig.css.preprocessorMaxWorkers = true
6569
}

src/vite/vuetify-styles-plugin.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import process from 'node:process'
22
import { pathToFileURL } from 'node:url'
3+
import { existsSync } from 'node:fs'
34
import type { Plugin } from 'vite'
45
import { isObject, normalizePath, resolveVuetifyBase } from '@vuetify/loader-shared'
56
import { 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[ca]ss$/))
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(/\.css$/, '.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(/\.css$/, '.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(/\.scss/) ? ';\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(/\.css$/, '.sass')
116+
if (!existsSync(mapping))
117+
mapping = source.replace(/\.css$/, '.scss')
118+
}
119+
catch {
120+
mapping = source.replace(/\.css$/, '.scss')
121+
}
122+
mappings.set(source, mapping)
123+
}
124+
return mapping
125+
}
126+
}
127+
108128
function isSubdir(root: string, test: string) {
109129
const relative = relativePath(root, test)
110130
return relative && !relative.startsWith('..') && !isAbsolute(relative)

0 commit comments

Comments
 (0)