import { dirname , resolve } from 'path' ; import { fileURLToPath } from 'url' ; import { addComponentsDir , addPluginTemplate , defineNuxtModule , extendViteConfig , extendWebpackConfig } from '@nuxt/kit' ; import WebpackPlugin from '@unocss/webpack' ; import VitePlugin from '@unocss/vite' ; import presetUno from '@unocss/preset-uno' ; import presetAttributify from '@unocss/preset-attributify' ; import presetIcons from '@unocss/preset-icons' ; import presetWebFonts from '@unocss/preset-web-fonts' ; import type { PresetUnoOptions } from '@unocss/preset-uno' ; import type { AttributifyOptions } from '@unocss/preset-attributify' ; import type { IconsOptions } from '@unocss/preset-icons' ; import type { UserConfig } from '@unocss/core' ; import type { WebFontsOptions } from '@unocss/preset-web-fonts' ; const dir = dirname ( fileURLToPath ( import . meta . url ) ) ; export interface UnocssNuxtOptions extends UserConfig { default autoImport ? : boolean ; default preflight ? : boolean ; default components ? : boolean ; default uno ? : boolean | PresetUnoOptions ; default attributify ? : boolean | AttributifyOptions ; default icons ? : boolean | IconsOptions ; default webFonts ? : boolean | WebFontsOptions ; } export default defineNuxtModule < UnocssNuxtOptions > ( { meta : { name : 'unocss' , configKey : 'unocss' , } , defaults : { autoImport : true , preflight : false , components : true , uno : true , attributify : false , webFonts : false , icons : false , } , setup ( options ) { if ( options . presets == null ) { options . presets = [ ] ; if ( options . uno ) options . presets . push ( presetUno ( typeof options . uno === 'boolean' ? { } : options . uno ) ) ; if ( options . attributify ) options . presets . push ( presetAttributify ( typeof options . attributify === 'boolean' ? { } : options . attributify ) ) ; if ( options . icons ) options . presets . push ( presetIcons ( typeof options . icons === 'boolean' ? { } : options . icons ) ) ; if ( options . webFonts ) options . presets . push ( presetWebFonts ( typeof options . webFonts === 'boolean' ? { } : options . webFonts ) ) ; } if ( options . autoImport ) { addPluginTemplate ( { filename : 'unocss.mjs' , src : '' , getContents : ( ) => { const lines = [ 'import \'uno.css\'' , 'export default () => {};' , ] ; if ( options . preflight ) lines . unshift ( 'import \'@unocss/reset/tailwind.css\'' ) ; return lines . join ( '\n' ) ; } , } ) ; } if ( options . components ) { addComponentsDir ( { path : resolve ( dir , '../runtime' ) , watch : false , } ) ; } extendViteConfig ( ( config ) => { config . plugins = config . plugins || [ ] ; config . plugins . unshift ( ... VitePlugin ( options ) ) ; } ) ; extendWebpackConfig ( ( config ) => { config . plugins = config . plugins || [ ] ; config . plugins . unshift ( WebpackPlugin ( options ) ) ; } ) ; } , } ) ; declare module '@nuxt/schema' { interface NuxtConfig { unocss ? : UnocssNuxtOptions ; } interface NuxtOptions { unocss ? : UnocssNuxtOptions ; } }