Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web-fonts): add downloadLocally options #3723

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -25,3 +25,4 @@ interactive/public/shiki
.svelte-kit
.eslintcache
vite.config.ts.timestamp-*
unocss-fonts/
13 changes: 13 additions & 0 deletions examples/nuxt3/nuxt.config.ts
Expand Up @@ -6,6 +6,19 @@ export default defineNuxtConfig({
attributify: true,
icons: true,
components: false,
theme: {
fontFamily: {
sans: 'sans-serif',
},
},
webFonts: {
downloadLocally: true,
provider: 'google',
fonts: {
sans: 'Lato',
serif: 'Merriweather',
},
},
shortcuts: [
['btn', 'px-4 py-1 rounded inline-block bg-teal-600 text-white cursor-pointer hover:bg-teal-700 disabled:cursor-default disabled:bg-gray-600 disabled:opacity-50'],
],
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/build.config.ts
Expand Up @@ -8,5 +8,6 @@ export default defineBuildConfig({
declaration: true,
externals: [
'@nuxt/schema',
'pathe',
],
})
19 changes: 3 additions & 16 deletions packages/nuxt/src/index.ts
Expand Up @@ -5,11 +5,12 @@ import { addComponentsDir, addPluginTemplate, defineNuxtModule, extendWebpackCon
import WebpackPlugin from '@unocss/webpack'
import type { VitePluginConfig } from '@unocss/vite'
import VitePlugin from '@unocss/vite'
import type { Nuxt, NuxtPlugin } from '@nuxt/schema'
import type { NuxtPlugin } from '@nuxt/schema'
import { loadConfig } from '@unocss/config'
import type { Preset, UserConfig } from '@unocss/core'
import type { UserConfig } from '@unocss/core'
import { resolveOptions } from './options'
import type { UnocssNuxtOptions } from './types'
import { configureWebFontPreset } from './web-fonts'

export { UnocssNuxtOptions }

Expand Down Expand Up @@ -135,20 +136,6 @@ 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
23 changes: 23 additions & 0 deletions packages/nuxt/src/web-fonts.ts
@@ -0,0 +1,23 @@
import type { Preset } from '@unocss/core'
import type { Nuxt } from '@nuxt/schema'
import { dirname, relative } from 'pathe'
import { defaultFontCssFilename } from '@unocss/preset-web-fonts/local-font'
import type { UnocssNuxtOptions } from './types'

export function configureWebFontPreset(nuxt: Nuxt, options: UnocssNuxtOptions) {
const webFontPreset = lookupPreset(options, '@unocss/preset-web-fonts')
if (webFontPreset && !!webFontPreset.options?.downloadLocally) {
const downloadDir = `${nuxt.options.dir.public}/unocss-fonts`
webFontPreset.options.downloadLocally = {
downloadDir,
downloadBasePath: nuxt.options.app.baseURL,
}
nuxt.options.css ??= []
nuxt.options.css.push(`~/${relative(dirname(nuxt.options.dir.public), downloadDir)}/${defaultFontCssFilename}`.replaceAll('\\', '/'))
Copy link
Member

@userquin userquin Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use unshift?

}
}

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
}
2 changes: 1 addition & 1 deletion packages/preset-web-fonts/src/local-font.ts
Expand Up @@ -6,7 +6,7 @@ import { resolveDownloadDir } from './util'

const fontUrlRegex = /[-a-z0-9@:%_+.~#?&/=]+\.(?:woff2?|eot|ttf|otf|svg)/gi

const defaultFontCssFilename = 'fonts.css'
export const defaultFontCssFilename = 'fonts.css'

interface UseLocalFontOptions {
downloadDir: string
Expand Down