Skip to content

Commit 034fd3a

Browse files
committed
feat(vuetify-nuxt-module): add experimental cache for vuetify styles in dev
Enable caching for processed Vuetify SCSS files during development to improve rebuild performance. The cache is automatically invalidated when relevant SASS/SCSS files change.
1 parent d445cf9 commit 034fd3a

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

apps/playground/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default defineNuxtConfig({
4646
},
4747
viewportSize: true,
4848
},
49-
styles: { configFile: '~/assets/custom-vuetify.scss' },
49+
styles: { configFile: '~/assets/custom-vuetify.scss', experimental: { cache: true } },
5050
},
5151
},
5252
vite: {

packages/vuetify-nuxt-module/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ export interface MOptions {
255255
* Path to the custom Vuetify SASS configuration file.
256256
*/
257257
configFile: string
258+
experimental?: {
259+
cache?: boolean
260+
}
258261
} | {
259262
/**
260263
* Include the standard colors palette?

packages/vuetify-nuxt-module/src/vite/vuetify-styles-plugin.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export function vuetifyStylesPlugin (
1616
_logger: ReturnType<typeof import('@nuxt/kit')['useLogger']>,
1717
) {
1818
let configFile: string | undefined
19-
// let cacheDir: string | undefined
19+
let useLoadCache = false
2020
const vuetifyBase = resolveVuetifyBase()
2121
const noneFiles = new Set<string>()
22+
const loadCache = new Map<string, { code: string, map: { mappings: string } }>()
2223
let isNone = false
2324
let sassVariables = false
2425
let fileImport = false
@@ -36,6 +37,7 @@ export function vuetifyStylesPlugin (
3637

3738
if (isObject(options.styles) && 'configFile' in options.styles) {
3839
sassVariables = true
40+
useLoadCache = !config.isProduction && !!options.styles.experimental?.cache
3941
// use file import when vite version > 5.4.2
4042
// check https://github.com/vitejs/vite/pull/17909
4143
fileImport = semver.gt(viteVersion, '5.4.2')
@@ -88,17 +90,41 @@ export function vuetifyStylesPlugin (
8890
: undefined)
8991

9092
if (target) {
93+
if (useLoadCache) {
94+
const cached = loadCache.get(id)
95+
if (cached) {
96+
return cached
97+
}
98+
}
9199
const suffix = /\.scss/.test(target) ? ';\n' : '\n'
92-
return {
100+
const result = {
93101
code: `@use "${configFile}"${suffix}@use "${fileImport ? pathToFileURL(target).href : normalizePath(target)}"${suffix}`,
94102
map: {
95103
mappings: '',
96104
},
97105
}
106+
if (useLoadCache) {
107+
loadCache.set(id, result)
108+
}
109+
return result
98110
}
99111
}
100112
return isNone && noneFiles.has(id) ? '' : undefined
101113
},
114+
handleHotUpdate ({ file }) {
115+
if (!useLoadCache) {
116+
return
117+
}
118+
119+
const normalizedFile = normalizePath(file)
120+
if (
121+
normalizedFile === normalizePath(configFile || '')
122+
|| normalizedFile.endsWith('.sass')
123+
|| normalizedFile.endsWith('.scss')
124+
) {
125+
loadCache.clear()
126+
}
127+
},
102128
}
103129
}
104130

0 commit comments

Comments
 (0)