Skip to content

Commit

Permalink
chore: move nuxt config to module
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Apr 20, 2024
1 parent 5904595 commit efb3c2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
23 changes: 20 additions & 3 deletions packages/nuxt/src/index.ts
Expand Up @@ -5,9 +5,9 @@ import { addComponentsDir, addPluginTemplate, defineNuxtModule, extendWebpackCon
import WebpackPlugin from '@unocss/webpack'
import type { VitePluginConfig } from '@unocss/vite'
import VitePlugin from '@unocss/vite'
import type { NuxtPlugin } from '@nuxt/schema'
import type { Nuxt, NuxtPlugin } from '@nuxt/schema'
import { loadConfig } from '@unocss/config'
import type { UserConfig } from '@unocss/core'
import type { Preset, UserConfig } from '@unocss/core'
import { resolveOptions } from './options'
import type { UnocssNuxtOptions } from './types'

Expand Down Expand Up @@ -36,7 +36,10 @@ export default defineNuxtModule<UnocssNuxtOptions>({
},
async setup(options, nuxt) {
// preset shortcuts
resolveOptions(nuxt, options)
resolveOptions(options)

// configure local webfonts preset
configureWebFontPreset(nuxt, options)

options.mode ??= 'global'
const InjectModes: VitePluginConfig['mode'][] = ['global', 'dist-chunk']
Expand Down Expand Up @@ -132,6 +135,20 @@ export default defineNuxtModule<UnocssNuxtOptions>({
},
})

function lookupPreset<P extends Preset<any>>(options: UnocssNuxtOptions, presetName: P['name']) {
const preset: P | undefined = (options.presets || []).flat().find(p => p.name === presetName) as any
return preset
}

function configureWebFontPreset(nuxt: Nuxt, options: UnocssNuxtOptions) {
const webFontPreset = lookupPreset(options, '@unocss/preset-web-fonts')
if (webFontPreset && !!webFontPreset.options?.downloadLocally) {
webFontPreset.options.downloadLocally = {}
webFontPreset.options.downloadLocally.downloadDir = `${nuxt.options.dir.public}/unocss-fonts`
webFontPreset.options.downloadLocally.downloadBasePath = nuxt.options.app.baseURL
}
}

declare module '@nuxt/schema' {
interface NuxtConfig {
unocss?: UnocssNuxtOptions
Expand Down
16 changes: 1 addition & 15 deletions packages/nuxt/src/options.ts
Expand Up @@ -5,12 +5,10 @@ import presetWebFonts from '@unocss/preset-web-fonts'
import presetTypography from '@unocss/preset-typography'
import presetTagify from '@unocss/preset-tagify'
import presetWind from '@unocss/preset-wind'
import type { Preset } from '@unocss/core'
import type { Nuxt } from '@nuxt/schema'
import { defaultPipelineExclude } from '../../shared-integration/src/defaults'
import type { UnocssNuxtOptions } from './types'

export function resolveOptions(nuxt: Nuxt, options: UnocssNuxtOptions) {
export function resolveOptions(options: UnocssNuxtOptions) {
if (options.presets == null) {
options.presets = []
const presetMap = {
Expand All @@ -29,13 +27,6 @@ export function resolveOptions(nuxt: Nuxt, options: UnocssNuxtOptions) {
}
}

const webFontPreset = lookupPreset(options, '@unocss/preset-web-fonts')
if (webFontPreset && !!webFontPreset.options?.downloadLocally) {
webFontPreset.options.downloadLocally = {}
webFontPreset.options.downloadLocally.downloadDir = `${nuxt.options.dir.public}/unocss-fonts`
webFontPreset.options.downloadLocally.downloadBasePath = nuxt.options.app.baseURL
}

options.content ??= {}
options.content.pipeline ??= {}
if (options.content.pipeline !== false) {
Expand All @@ -45,8 +36,3 @@ export function resolveOptions(nuxt: Nuxt, options: UnocssNuxtOptions) {
options.content.pipeline.exclude.push(/\?macro=true/)
}
}

function lookupPreset<P extends Preset<any>>(options: UnocssNuxtOptions, presetName: P['name']) {
const preset: P | undefined = (options.presets || []).flat().find(p => p.name === presetName) as any
return preset
}

0 comments on commit efb3c2e

Please sign in to comment.