Skip to content

Commit

Permalink
chore: change types and use vite.base in fonts.css
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Apr 20, 2024
1 parent 026916a commit 1290478
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 32 deletions.
9 changes: 5 additions & 4 deletions packages/preset-web-fonts/src/local-font.ts
Expand Up @@ -10,11 +10,12 @@ const defaultFontCssFilename = 'fonts.css'

interface UseLocalFontOptions {
downloadDir: string
downloadBasePath: string
}

export { resolveDownloadDir }

export async function useLocalFont(css: string, { downloadDir }: UseLocalFontOptions) {
export async function useLocalFont(css: string, { downloadDir, downloadBasePath }: UseLocalFontOptions) {
const [{ mkdir, writeFile }, { resolve }] = await Promise.all([
import('node:fs/promises'),
import('node:path'),
Expand All @@ -23,9 +24,9 @@ export async function useLocalFont(css: string, { downloadDir }: UseLocalFontOpt

// Download the fonts locally and update the font.css file
for (const url of css.match(fontUrlRegex) || []) {
const path = resolve(downloadDir, url.split('/').pop()!)
await saveFont(url, path)
css = css.replaceAll(url, path)
const name = url.split('/').pop()!
await saveFont(url, resolve(downloadDir, name))
css = css.replaceAll(url, `${downloadBasePath}${name}`)
}

// Save the updated font.css file
Expand Down
8 changes: 4 additions & 4 deletions packages/preset-web-fonts/src/preset.ts
Expand Up @@ -46,8 +46,7 @@ export function createWebFontPreset(fetcher: (url: string) => Promise<any>) {
inlineImports = true,
themeKey = 'fontFamily',
customFetch = fetcher,
downloadLocally = false,
downloadDir,
downloadLocally,
} = options

const fontObject = Object.fromEntries(
Expand All @@ -66,15 +65,16 @@ export function createWebFontPreset(fetcher: (url: string) => Promise<any>) {
}

const { readFontCSS, resolveDownloadDir } = await import('./local-font')
const resolvedDownloadDir = await resolveDownloadDir(downloadDir)
const resolvedDownloadDir = await resolveDownloadDir(
downloadLocally === true ? undefined : downloadLocally.downloadDir,
)
return readFontCSS(resolvedDownloadDir)
},
layer: inlineImports ? undefined : LAYER_IMPORTS,
},
],
options: {
downloadLocally,
downloadDir,
fontObject,
},
}
Expand Down
28 changes: 19 additions & 9 deletions packages/preset-web-fonts/src/types.ts
Expand Up @@ -55,18 +55,28 @@ export interface WebFontsOptions {
customFetch?: (url: string) => Promise<any>

/**
* Download fonts locally
* Download fonts locally.
*
* @default false
*/
downloadLocally?: boolean

/**
* Where to download the fonts
*
* @default 'process.cwd()/public/unocss-fonts'
*/
downloadDir?: string | (() => Promise<string>)
downloadLocally?: boolean | {
/**
* Where to download the fonts.
*
* This option will be overridden by integrations:
* - Vite, Astro and Nuxt will use Vite [publicDir](): `${publicDir}/unocss-fonts`
* - SvelteKit will use `static` folder: `static/unocss-fonts`
*
* @default 'process.cwd()/public/unocss-fonts'
*/
downloadDir?: string
/**
*
*
* @default /
*/
downloadBasePath?: string
}
}

export interface Provider {
Expand Down
11 changes: 5 additions & 6 deletions packages/preset-web-fonts/src/util.ts
@@ -1,16 +1,15 @@
// eslint-disable-next-line node/prefer-global/process
export const isNode = typeof process !== 'undefined' && process.stdout && !process.versions.deno

export async function resolveDownloadDir(downloadDir?: string | (() => Promise<string>)) {
export async function resolveDownloadDir(downloadDir?: string | (() => string)) {
if (!isNode)
return ''

if (typeof downloadDir === 'function')
return downloadDir()

const { resolve } = await import('node:path')
const { cwd } = await import('node:process')

if (typeof downloadDir === 'function')
return await downloadDir()
else if (typeof downloadDir === 'string')
return resolve(cwd(), downloadDir)
return `${cwd()}/public/unocss-fonts`
return typeof downloadDir === 'string' ? resolve(cwd(), downloadDir) : `${cwd()}/public/unocss-fonts`
}
18 changes: 10 additions & 8 deletions packages/vite/src/web-fonts.ts
Expand Up @@ -11,20 +11,22 @@ export function createWebFontPlugins(ctx: UnocssPluginContext): Plugin[] {
if (!webFontPreset || !webFontPreset.options?.downloadLocally)
return

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

const [{ resolveDownloadDir, useLocalFont }, { getRemoteFontsCSS }] = await Promise.all([
if (typeof webFontPreset.options.downloadLocally.downloadDir === 'undefined')
webFontPreset.options.downloadLocally.downloadDir = `${config.publicDir}/unocss-fonts`

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 })
const downloadDir = await resolveDownloadDir(webFontPreset.options.downloadDir)
await useLocalFont(fontCSS, { downloadDir })
await useLocalFont(fontCSS, webFontPreset.options.downloadLocally)
},
},
]
Expand Down
3 changes: 2 additions & 1 deletion playground/uno.config.ts
Expand Up @@ -12,7 +12,7 @@ import presetWebFonts from '@unocss/preset-web-fonts'
export default defineConfig({
theme: {
fontFamily: {
sans: '\'Lato\', sans-serif',
sans: 'sans-serif',
mono: '\'Fira Code\', monospace',
},
},
Expand All @@ -24,6 +24,7 @@ export default defineConfig({
presetUno(),
presetIcons(),
presetWebFonts({
downloadLocally: true,
provider: 'google',
fonts: {
sans: 'Lato',
Expand Down

0 comments on commit 1290478

Please sign in to comment.