Skip to content

Commit

Permalink
chore: export default fonts folder
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Apr 20, 2024
1 parent f52518b commit 912bfe2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/astro/build.config.ts
Expand Up @@ -8,5 +8,9 @@ export default defineBuildConfig({
declaration: true,
externals: [
'astro',
'ofetch',
'destr',
'ufo',
'@unocss/preset-web-fonts/local-font',
],
})
2 changes: 1 addition & 1 deletion packages/astro/src/index.ts
Expand Up @@ -98,7 +98,7 @@ export default function UnoCSSAstroIntegration<Theme extends object>(
if (injectExtra.length > 0)
injects.push(...injectExtra)

configureWebFontPreset(config, defaults)
await configureWebFontPreset(config, defaults)

updateConfig({
vite: {
Expand Down
7 changes: 5 additions & 2 deletions packages/astro/src/web-fonts.ts
@@ -1,10 +1,13 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { Preset, UserConfigDefaults } from '@unocss/core'
import type { AstroConfig } from 'astro'

export function configureWebFontPreset(config: AstroConfig, options?: UserConfigDefaults) {
export async function configureWebFontPreset(config: AstroConfig, options?: UserConfigDefaults) {
const webFontPreset = options ? lookupPreset(options, '@unocss/preset-web-fonts') : undefined
if (webFontPreset && !!webFontPreset.options?.downloadLocally) {
const downloadDir = `${config.publicDir}/unocss-fonts`
const { defaultFontFolder } = await import('@unocss/preset-web-fonts/local-font')
const downloadDir = resolve(fileURLToPath(config.publicDir), defaultFontFolder)
webFontPreset.options.downloadLocally = {
downloadDir,
downloadBasePath: config.base,
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/index.ts
Expand Up @@ -40,7 +40,7 @@ export default defineNuxtModule<UnocssNuxtOptions>({
resolveOptions(options)

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

options.mode ??= 'global'
const InjectModes: VitePluginConfig['mode'][] = ['global', 'dist-chunk']
Expand Down
8 changes: 4 additions & 4 deletions packages/nuxt/src/web-fonts.ts
@@ -1,13 +1,13 @@
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 { dirname, relative, resolve } from 'pathe'
import type { UnocssNuxtOptions } from './types'

export function configureWebFontPreset(nuxt: Nuxt, options: UnocssNuxtOptions) {
export async 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`
const { defaultFontFolder, defaultFontCssFilename } = await import('@unocss/preset-web-fonts/local-font')
const downloadDir = resolve(nuxt.options.dir.public, defaultFontFolder)
webFontPreset.options.downloadLocally = {
downloadDir,
downloadBasePath: nuxt.options.app.baseURL,
Expand Down
3 changes: 2 additions & 1 deletion packages/preset-web-fonts/src/local-font.ts
Expand Up @@ -6,6 +6,7 @@ import { resolveDownloadDir } from './util'

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

export const defaultFontFolder = 'unocss-fonts'
export const defaultFontCssFilename = 'fonts.css'

interface UseLocalFontOptions {
Expand All @@ -26,7 +27,7 @@ export async function useLocalFont(css: string, { downloadDir, downloadBasePath
for (const url of css.match(fontUrlRegex) || []) {
const name = url.split('/').pop()!
await saveFont(url, resolve(downloadDir, name))
css = css.replaceAll(url, `${downloadBasePath}${name}`)
css = css.replaceAll(url, `${downloadBasePath}${defaultFontFolder}/${name}`)
}

// Save the updated font.css file
Expand Down
19 changes: 13 additions & 6 deletions packages/vite/src/web-fonts.ts
Expand Up @@ -11,20 +11,27 @@ export function createWebFontPlugins(ctx: UnocssPluginContext): Plugin[] {
if (!webFontPreset || !webFontPreset.options?.downloadLocally)
return

const [
{ defaultFontFolder, useLocalFont },
{ getRemoteFontsCSS },
{ $fetch },
{ resolve },
] = await Promise.all([
import('@unocss/preset-web-fonts/local-font'),
import('@unocss/preset-web-fonts/remote-font'),
import('ofetch'),
import('node:path'),
])

if (webFontPreset.options.downloadLocally === true)
webFontPreset.options.downloadLocally = {}

if (typeof webFontPreset.options.downloadLocally.downloadDir === 'undefined')
webFontPreset.options.downloadLocally.downloadDir = `${config.publicDir}/unocss-fonts`
webFontPreset.options.downloadLocally.downloadDir = resolve(config.publicDir, defaultFontFolder)

if (typeof webFontPreset.options.downloadLocally.downloadBasePath === 'undefined')
webFontPreset.options.downloadLocally.downloadBasePath = config.base

const [{ useLocalFont }, { getRemoteFontsCSS }] = await Promise.all([
import('@unocss/preset-web-fonts/local-font'),
import('@unocss/preset-web-fonts/remote-font'),
])
const { $fetch } = await import('ofetch')
const fontCSS = await getRemoteFontsCSS(webFontPreset.options.fontObject, { inlineImports: true, customFetch: $fetch })
await useLocalFont(fontCSS, webFontPreset.options.downloadLocally)
},
Expand Down

0 comments on commit 912bfe2

Please sign in to comment.