import type { UnpluginOptions } from 'unplugin' import { genObjectFromRaw } from 'knitwork' import type { NuxtUIOptions } from '../unplugin' // Function slot values replace the default classes, so they have to survive the serialization. // A shorthand method like `label() {}` is not an expression, so it is read back from an object literal. function serialize(value: any): any { if (typeof value === 'function') { const source = value.toString() return /^(?:async\s*)?(?:function\b|\(|[\w$]+\s*=>)/.test(source) ? source : `({ ${source} })[${JSON.stringify(value.name)}]` } if (Array.isArray(value)) { return value.map(serialize) } if (value && typeof value === 'object') { return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, serialize(item)])) } return value === undefined ? value : JSON.stringify(value) } /** * This plugin injects Nuxt UI configuration into the runtime build so Nuxt UI components can * access it. */ export default function AppConfigPlugin(_options: NuxtUIOptions & { theme: NonNullable }, appConfig: Record) { return { name: 'nuxt:ui:app-config', enforce: 'pre', resolveId(id) { if (id === '#build/app.config') { return 'virtual:nuxt-ui-app-config' } }, loadInclude: id => id === 'virtual:nuxt-ui-app-config', load() { return `export default ${genObjectFromRaw(serialize(appConfig))}` }, vite: { config() { return { test: { server: { deps: { inline: ['@nuxt/ui'] } } } } } } } satisfies UnpluginOptions }