From 306d2df05c5ca4adad4f3d8bdf9f7051fa8dd06b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 20 Feb 2024 13:09:04 +0000 Subject: [PATCH] feat: support passing options to font providers --- src/module.ts | 4 +++- src/providers/google.ts | 1 + src/providers/local.ts | 4 ++-- src/types.ts | 28 ++++++++++++++++------------ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/module.ts b/src/module.ts index 6542ab2..d32e89a 100644 --- a/src/module.ts +++ b/src/module.ts @@ -29,6 +29,8 @@ export default defineNuxtModule({ }, defaults: { defaults: {}, + local: {}, + google: { strategy: 'public' }, providers: { local, google, @@ -55,7 +57,7 @@ export default defineNuxtModule({ if (options.providers?.[key] === false) { delete providers[key] } else if (provider.setup) { - setups.push(provider.setup(nuxt)) + setups.push(provider.setup(options[key as 'google' | 'local'] || {}, nuxt)) } } await Promise.all(setups) diff --git a/src/providers/google.ts b/src/providers/google.ts index 0bfbbd4..8f10237 100644 --- a/src/providers/google.ts +++ b/src/providers/google.ts @@ -37,6 +37,7 @@ const fontAPI = $fetch.create({ }) let fonts: FontIndexMeta[] + export default { async setup () { // TODO: Fetch and cache possible Google fonts diff --git a/src/providers/local.ts b/src/providers/local.ts index 173ccb0..980c823 100644 --- a/src/providers/local.ts +++ b/src/providers/local.ts @@ -11,7 +11,7 @@ const providerContext = { } export default { - async setup (nuxt) { + async setup (_options, nuxt) { // Scan for all font files in public directories for (const layer of nuxt.options._layers) { const publicDir = join(layer.config.srcDir || layer.cwd, layer.config.dir?.public || 'public') @@ -93,7 +93,7 @@ const NON_WORD_RE = /[^\w\d]+/g export const isFontFile = (id: string) => FONT_RE.test(id) -function generateSlugs (path: string) { +function generateSlugs (path: string) { const name = filename(path) return [...new Set([ name.toLowerCase(), diff --git a/src/types.ts b/src/types.ts index 631f5d8..06c5969 100644 --- a/src/types.ts +++ b/src/types.ts @@ -35,10 +35,10 @@ export interface FontFaceData { // TODO: Font metric providers // export interface FontFaceAdjustments { -// ascent-override -// descent-override -// line-gap-override -// size-adjust +// ascentOverride?: string // ascent-override +// descentOverride?: string // descent-override +// lineGapOverride?: string // line-gap-override +// sizeAdjust?: string // size-adjust // } export interface ResolveFontFacesOptions { @@ -48,12 +48,12 @@ export interface ResolveFontFacesOptions { subsets: string[] } -export interface FontProvider { +export interface FontProvider> { /** * The setup function will be called before the first `resolveFontFaces` call and is a good * place to register any Nuxt hooks or setup any state. */ - setup?: (nuxt: Nuxt) => Awaitable + setup?: (options: FontProviderOptions, nuxt: Nuxt) => Awaitable /** * Resolve data for `@font-face` declarations. * @@ -67,6 +67,9 @@ export interface FontProvider { */ fonts: FontFaceData | FontFaceData[] }> + // resolveFontMetrics?: (fontFamily: string, fonts: FontFaceData[], options: ResolveFontFacesOptions) => Awaitable } export type FontProviderName = (string & {}) | 'google' | 'local' | 'none' @@ -119,12 +122,13 @@ export interface ModuleOptions { local?: FontProvider | string | false [key: string]: FontProvider | string | false | undefined } - // TODO: Provider options - // google?: { - // // TODO: allow customising download behaviour with nuxt/assets - // download?: string - // } - // local?: {} + /** Options passed directly to `google` font provider */ + google?: { + // TODO: implement this with nuxt/assets + strategy?: 'public' // | 'bundle' | 'proxy' + } + /** Options passed directly to `local` font provider (none currently) */ + local?: {} /** * An ordered list of providers to check when resolving font families. *