Skip to content

Commit a898886

Browse files
committed
feat: replace manual version checks with semver and improve type safety
- Add semver dependency for version comparison - Replace isVuetifyAtLeast with vuetifyGte using semver - Change viteVersion from tuple to string type - Update all version checks to use semver - Add @types/semver for type safety
1 parent ca4225b commit a898886

File tree

9 files changed

+121
-307
lines changed

9 files changed

+121
-307
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"local-pkg": "^0.5.0",
8282
"pathe": "^1.1.2",
8383
"perfect-debounce": "^1.0.0",
84+
"semver": "^7.7.3",
8485
"ufo": "^1.5.4",
8586
"unconfig": "^0.5.5",
8687
"upath": "^2.0.1",
@@ -104,6 +105,7 @@
104105
"@nuxtjs/i18n": "^8.0.0",
105106
"@parcel/watcher": "^2.3.0",
106107
"@types/node": "^18",
108+
"@types/semver": "^7.7.1",
107109
"@unocss/nuxt": "^66.5.10",
108110
"bumpp": "^9.2.0",
109111
"eslint": "^8.54.0",

pnpm-lock.yaml

Lines changed: 90 additions & 280 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useLogger,
88
} from '@nuxt/kit'
99
import { getPackageInfo } from 'local-pkg'
10+
import semver from 'semver'
1011
import type { HookResult } from '@nuxt/schema'
1112
import type { VuetifyOptions, createVuetify } from 'vuetify'
1213
import { version as VITE_VERSION } from 'vite'
@@ -81,15 +82,11 @@ export default defineNuxtModule<ModuleOptions>({
8182
logger.error(`Cannot support nuxt version: ${getNuxtVersion(nuxt)}`)
8283

8384
const vuetifyPkg = await getPackageInfo('vuetify')
84-
const versions = vuetifyPkg?.version?.split('.').map(v => Number.parseInt(v))
85-
const isVuetifyAtLeast = (major: number, minor: number) =>
86-
!!versions
87-
&& versions.length > 1
88-
&& (versions[0] > major || (versions[0] === major && versions[1] >= minor))
85+
const currentVersion = vuetifyPkg?.version
86+
const vuetifyGte = (version: string) =>
87+
!!currentVersion && semver.gte(currentVersion, version)
8988

90-
const viteVersion = VITE_VERSION.split('.')
91-
.map((v: string) => v.includes('-') ? v.split('-')[0] : v)
92-
.map(v => Number.parseInt(v)) as VuetifyNuxtContext['viteVersion']
89+
const viteVersion = VITE_VERSION
9390

9491
const ctx: VuetifyNuxtContext = {
9592
logger,
@@ -106,7 +103,7 @@ export default defineNuxtModule<ModuleOptions>({
106103
ssrClientHints: undefined!,
107104
componentsPromise: undefined!,
108105
labComponentsPromise: undefined!,
109-
isVuetifyAtLeast,
106+
vuetifyGte,
110107
viteVersion,
111108
}
112109

src/utils/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ export interface VuetifyNuxtContext {
2525
ssrClientHints: ResolvedClientHints
2626
componentsPromise: Promise<VuetifyComponentsImportMap>
2727
labComponentsPromise: Promise<VuetifyComponentsImportMap>
28-
isVuetifyAtLeast: (major: number, minor: number) => boolean
29-
viteVersion: [major: number, minor: number, patch: number]
28+
/**
29+
* Check if Vuetify version is greater than or equal to the given version
30+
* @example ctx.vuetifyGte('3.4.0') // true if Vuetify version is 3.4.0 or greater
31+
*/
32+
vuetifyGte: (version: string) => boolean
33+
viteVersion: string
3034
enableRules?: boolean
3135
rulesConfiguration?: { fromLabs: boolean }
3236
}

src/utils/configure-nuxt.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function configureNuxt(
2121

2222
// Automatically enable rules if not disabled
2323
if (typeof ctx.enableRules !== 'undefined')
24-
ctx.enableRules = ctx.isVuetifyAtLeast(3, 8)
24+
ctx.enableRules = ctx.vuetifyGte('3.8.0')
2525

2626
// disable inline styles when SSR enabled
2727
if (ctx.isSSR && !!styles && typeof styles === 'object')
@@ -89,19 +89,19 @@ export function configureNuxt(
8989

9090
if (importComposables) {
9191
const composables = ['useDate', 'useLocale', 'useDefaults', 'useDisplay', 'useLayout', 'useRtl', 'useTheme']
92-
if (ctx.isVuetifyAtLeast(3, 5))
92+
if (ctx.vuetifyGte('3.5.0'))
9393
composables.push('useGoTo')
94-
if (ctx.isVuetifyAtLeast(3, 8)) {
94+
if (ctx.vuetifyGte('3.8.0')) {
9595
composables.push('useHotkey')
9696
if (ctx.enableRules)
9797
composables.push('useRules')
9898
}
99-
if (ctx.isVuetifyAtLeast(3, 10))
99+
if (ctx.vuetifyGte('3.10.0'))
100100
composables.push('useMask')
101101

102102
addImports(composables.map(name => ({
103103
name,
104-
from: ctx.isVuetifyAtLeast(3, 4) || name !== 'useDate' ? 'vuetify' : 'vuetify/labs/date',
104+
from: ctx.vuetifyGte('3.4.0') || name !== 'useDate' ? 'vuetify' : 'vuetify/labs/date',
105105
as: prefixComposables ? name.replace(/^use/, 'useV') : undefined,
106106
meta: { docsUrl: name === 'useRules' ? 'https://vuetifyjs.com/en/features/rules/' : `https://vuetifyjs.com/en/api/${toKebabCase(name)}/` },
107107
})))

src/utils/configure-vite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Nuxt } from '@nuxt/schema'
22
import defu from 'defu'
33
import type { ObjectImportPluginOptions } from '@vuetify/loader-shared'
44
import { isPackageExists } from 'local-pkg'
5+
import semver from 'semver'
56
import { vuetifyStylesPlugin } from '../vite/vuetify-styles-plugin'
67
import { vuetifyConfigurationPlugin } from '../vite/vuetify-configuration-plugin'
78
import { vuetifyIconsPlugin } from '../vite/vuetify-icons-configuration-plugin'
@@ -45,8 +46,7 @@ export function configureVite(configKey: string, nuxt: Nuxt, ctx: VuetifyNuxtCon
4546

4647
if (!ctx.moduleOptions.disableModernSassCompiler) {
4748
// vite version >= 5.4.0
48-
const [major, minor, patch] = ctx.viteVersion
49-
const enableModernSassCompiler = major > 5 || (major === 5 && minor >= 4)
49+
const enableModernSassCompiler = semver.gte(ctx.viteVersion, '5.4.0')
5050
if (enableModernSassCompiler) {
5151
const sassEmbedded = isPackageExists('sass-embedded')
5252
if (sassEmbedded) {

src/vite/vuetify-configuration-plugin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async function buildConfiguration(ctx: VuetifyNuxtContext) {
175175
})
176176

177177
// lab components
178-
let addDatePicker = ctx.isVuetifyAtLeast(3, 4)
178+
let addDatePicker = ctx.vuetifyGte('3.4.0')
179179
? !Array.from(componentsToImport.values()).some(components => components.includes('VDatePicker'))
180180
: true
181181

@@ -184,7 +184,7 @@ async function buildConfiguration(ctx: VuetifyNuxtContext) {
184184
if (typeof labComponents === 'boolean') {
185185
config.imports.push('import * as labsComponents from \'vuetify/labs/components\'')
186186
config.labComponents.add('*')
187-
if (!ctx.isVuetifyAtLeast(3, 4))
187+
if (!ctx.vuetifyGte('3.4.0'))
188188
addDatePicker = false
189189
}
190190
else if (typeof labComponents === 'string') {
@@ -220,7 +220,7 @@ async function buildConfiguration(ctx: VuetifyNuxtContext) {
220220
config.labComponents.add(component)
221221
})
222222

223-
if (!ctx.isVuetifyAtLeast(3, 4) && dateOptions && !addDatePicker) {
223+
if (!ctx.vuetifyGte('3.4.0') && dateOptions && !addDatePicker) {
224224
const entry = componentsToImport.get('VDatePicker')
225225
if (entry) {
226226
entry.push('VDatePicker')
@@ -239,9 +239,9 @@ async function buildConfiguration(ctx: VuetifyNuxtContext) {
239239
// include date picker only when needed
240240
if (dateOptions && addDatePicker) {
241241
let warn = true
242-
if (ctx.isVuetifyAtLeast(0, 0)) {
242+
if (ctx.vuetifyGte('3.0.0')) {
243243
warn = false
244-
if (ctx.isVuetifyAtLeast(3, 4)) {
244+
if (ctx.vuetifyGte('3.4.0')) {
245245
config.components.add('VDatePicker')
246246
config.imports.push('import {VDatePicker} from \'vuetify/components/VDatePicker\'')
247247
}

src/vite/vuetify-date-configuration-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function dateConfiguration() {
4242
}
4343

4444
function buildAdapter() {
45-
if (ctx.dateAdapter === 'custom' || (ctx.dateAdapter === 'vuetify' && ctx.isVuetifyAtLeast(3, 4)))
45+
if (ctx.dateAdapter === 'custom' || (ctx.dateAdapter === 'vuetify' && ctx.vuetifyGte('3.4.0')))
4646
return ''
4747

4848
if (ctx.dateAdapter === 'vuetify')
@@ -56,7 +56,7 @@ export function dateConfiguration() {
5656
}
5757

5858
function buildImports() {
59-
if (ctx.dateAdapter === 'custom' || (ctx.dateAdapter === 'vuetify' && ctx.isVuetifyAtLeast(3, 4)))
59+
if (ctx.dateAdapter === 'custom' || (ctx.dateAdapter === 'vuetify' && ctx.vuetifyGte('3.4.0')))
6060
return ''
6161

6262
if (ctx.dateAdapter === 'vuetify')

src/vite/vuetify-styles-plugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { isObject, normalizePath, resolveVuetifyBase } from '@vuetify/loader-sha
77
import { isAbsolute, relative as relativePath } from 'pathe'
88
import type { Options } from '@vuetify/loader-shared'
99
import path from 'upath'
10+
import semver from 'semver'
1011
import type { VuetifyNuxtContext } from '../utils/config'
1112

1213
export function vuetifyStylesPlugin(
1314
options: Options,
14-
[major, minor, patch]: VuetifyNuxtContext['viteVersion'],
15+
viteVersion: VuetifyNuxtContext['viteVersion'],
1516
_logger: ReturnType<typeof import('@nuxt/kit')['useLogger']>,
1617
) {
1718
let configFile: string | undefined
@@ -36,7 +37,7 @@ export function vuetifyStylesPlugin(
3637
sassVariables = true
3738
// use file import when vite version > 5.4.2
3839
// check https://github.com/vitejs/vite/pull/17909
39-
fileImport = major > 5 || (major === 5 && minor > 4) || (major === 5 && minor === 4 && patch > 2)
40+
fileImport = semver.gt(viteVersion, '5.4.2')
4041
if (path.isAbsolute(options.styles.configFile))
4142
configFile = path.resolve(options.styles.configFile)
4243
else

0 commit comments

Comments
 (0)