diff --git a/packages/vuetify-nuxt-module/src/types.ts b/packages/vuetify-nuxt-module/src/types.ts index 47c7ce8..f1cdabc 100644 --- a/packages/vuetify-nuxt-module/src/types.ts +++ b/packages/vuetify-nuxt-module/src/types.ts @@ -1,6 +1,6 @@ import type { LocaleOptions, RtlOptions, VuetifyOptions } from 'vuetify' -export type DateAdapter = 'vuetify' | 'date-fns' | 'moment' | 'luxon' | 'dayjs' | 'js-joda' | 'date-fns-jalali' | 'jalaali' | 'hijri' | 'custom' +export type DateAdapter = 'vuetify' | 'string' | 'date-fns' | 'moment' | 'luxon' | 'dayjs' | 'js-joda' | 'date-fns-jalali' | 'jalaali' | 'hijri' | 'custom' /** * Date configuration. diff --git a/packages/vuetify-nuxt-module/src/utils/loader.ts b/packages/vuetify-nuxt-module/src/utils/loader.ts index 121b9bc..0eed352 100644 --- a/packages/vuetify-nuxt-module/src/utils/loader.ts +++ b/packages/vuetify-nuxt-module/src/utils/loader.ts @@ -56,7 +56,7 @@ export async function load ( } if (adapter) { - if (adapter === 'vuetify' || adapter === 'custom') { + if (['vuetify', 'string', 'custom'].includes(adapter)) { ctx.dateAdapter = adapter } else { if (date.includes(adapter)) { diff --git a/packages/vuetify-nuxt-module/src/vite/vuetify-date-configuration-plugin.ts b/packages/vuetify-nuxt-module/src/vite/vuetify-date-configuration-plugin.ts index 2a14647..fd9b2d8 100644 --- a/packages/vuetify-nuxt-module/src/vite/vuetify-date-configuration-plugin.ts +++ b/packages/vuetify-nuxt-module/src/vite/vuetify-date-configuration-plugin.ts @@ -32,6 +32,12 @@ export function dateConfiguration() { ` } + // StringDateAdapter was added in Vuetify 3.9.0: + // https://github.com/vuetifyjs/vuetify/releases/tag/v3.9.0 + if (ctx.dateAdapter === 'string' && !ctx.vuetifyGte('3.9.0')) { + throw new Error('[vuetify-nuxt-module] The "string" date adapter requires Vuetify 3.9.0 or newer.') + } + const { adapter: _adapter, ...newDateOptions } = ctx.vuetifyOptions.date ?? {} let dateFnsLocale: string | undefined @@ -71,6 +77,10 @@ export function dateConfiguration() { return `options.adapter = new Adapter({ locale: ${dateFnsLocale} })` } + if (ctx.dateAdapter === 'string') { + return 'options.adapter = StringDateAdapter' + } + return 'options.adapter = Adapter' } @@ -83,6 +93,10 @@ export function dateConfiguration() { return 'import { VuetifyDateAdapter } from \'vuetify/labs/date/adapters/vuetify\'' } + if (ctx.dateAdapter === 'string') { + return 'import { StringDateAdapter } from \'vuetify/date/adapters/string\'' + } + const imports = [`import Adapter from '@date-io/${ctx.dateAdapter}'`] if (ctx.dateAdapter === 'date-fns') { imports.push(`import { ${dateFnsLocale} } from 'date-fns/locale'`) diff --git a/packages/vuetify-nuxt-module/test/date-configuration-plugin.test.ts b/packages/vuetify-nuxt-module/test/date-configuration-plugin.test.ts index fee42ac..f62a2e8 100644 --- a/packages/vuetify-nuxt-module/test/date-configuration-plugin.test.ts +++ b/packages/vuetify-nuxt-module/test/date-configuration-plugin.test.ts @@ -1,5 +1,7 @@ +import { createServer } from 'vite' import { describe, expect, it, vi } from 'vitest' -import { RESOLVED_VIRTUAL_VUETIFY_DATE_CONFIGURATION } from '../src/vite/constants' +import { StringDateAdapter } from 'vuetify/date/adapters/string' +import { RESOLVED_VIRTUAL_VUETIFY_DATE_CONFIGURATION, VIRTUAL_VUETIFY_DATE_CONFIGURATION } from '../src/vite/constants' import { vuetifyDateConfigurationPlugin } from '../src/vite/vuetify-date-configuration-plugin' function makeCtx (localeCode: string | undefined) { @@ -53,3 +55,34 @@ describe('vuetifyDateConfigurationPlugin date-fns locale', () => { expect(ctx.logger.warn).toHaveBeenCalledExactlyOnceWith(expect.stringContaining('(unset)')) }) }) + +describe('vuetifyDateConfigurationPlugin string adapter', () => { + it('loads and configures Vuetify StringDateAdapter', async () => { + const ctx = makeCtx('en') + ctx.dateAdapter = 'string' + ctx.vuetifyOptions.date.adapter = 'string' + + const server = await createServer({ + logLevel: 'silent', + plugins: [vuetifyDateConfigurationPlugin(ctx)], + }) + + try { + const module = await server.ssrLoadModule(VIRTUAL_VUETIFY_DATE_CONFIGURATION) + + expect(module.adapter).toBe('string') + expect(module.dateConfiguration().adapter).toBe(StringDateAdapter) + } finally { + await server.close() + } + }) + + it('rejects Vuetify versions without StringDateAdapter', async () => { + const ctx = makeCtx('en') + ctx.dateAdapter = 'string' + ctx.vuetifyOptions.date.adapter = 'string' + ctx.vuetifyGte = (version: string) => version !== '3.9.0' + + await expect(loadModule(ctx)).rejects.toThrow('requires Vuetify 3.9.0 or newer') + }) +}) diff --git a/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/app.vue b/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/app.vue new file mode 100644 index 0000000..f58d966 --- /dev/null +++ b/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/app.vue @@ -0,0 +1,11 @@ + + + diff --git a/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/nuxt.config.ts b/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/nuxt.config.ts new file mode 100644 index 0000000..28b35c8 --- /dev/null +++ b/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/nuxt.config.ts @@ -0,0 +1,10 @@ +import MyModule from '../../../src/module' + +export default defineNuxtConfig({ + modules: [MyModule], + vuetify: { + vuetifyOptions: { + date: { adapter: 'string' }, + }, + }, +}) diff --git a/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/package.json b/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/package.json new file mode 100644 index 0000000..990d4e1 --- /dev/null +++ b/packages/vuetify-nuxt-module/test/fixtures/string-date-adapter/package.json @@ -0,0 +1,5 @@ +{ + "name": "string-date-adapter", + "type": "module", + "private": true +} diff --git a/packages/vuetify-nuxt-module/test/string-date-adapter.test.ts b/packages/vuetify-nuxt-module/test/string-date-adapter.test.ts new file mode 100644 index 0000000..4492131 --- /dev/null +++ b/packages/vuetify-nuxt-module/test/string-date-adapter.test.ts @@ -0,0 +1,15 @@ +import { fileURLToPath } from 'node:url' +import { $fetch, setup } from '@nuxt/test-utils' +import { describe, expect, it } from 'vitest' + +describe('string date adapter', async () => { + await setup({ + rootDir: fileURLToPath(new URL('fixtures/string-date-adapter', import.meta.url)), + }) + + it('uses the built-in string adapter from Nuxt configuration', async () => { + const html = await $fetch('/') + expect(html).toContain('data-date-type="string"') + expect(html).toContain('>2026-08-22

') + }) +})