From bd8da2637540508afb419a428b506165bb897772 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 9 Mar 2024 08:56:48 +0100 Subject: [PATCH] feat: support `priority` and `provider` options --- README.md | 4 ++++ src/module.ts | 11 +++++++++-- src/types.ts | 10 +++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c93d183..ea06294 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,10 @@ export default defineNuxtConfig({ // Or you can disable a built-in provider google: false, }, + // You can customize the order in which providers are checked. + priority: ['bunny', 'google'], + // You can also set a single provider, which is a shortcut for disabling all but one provider + provider: 'fontshare', } }) ``` diff --git a/src/module.ts b/src/module.ts index bab62c8..96b95b6 100644 --- a/src/module.ts +++ b/src/module.ts @@ -108,6 +108,7 @@ export default defineNuxtModule({ } const providers = await resolveProviders(options.providers) + const prioritisedProviders = new Set() // Allow registering and disabling providers nuxt.hook('modules:done', async () => { @@ -115,13 +116,19 @@ export default defineNuxtModule({ const setups: Array> = [] for (const key in providers) { const provider = providers[key]! - if (options.providers?.[key] === false) { + if (options.providers?.[key] === false || (options.provider && options.provider !== key)) { delete providers[key] } else if (provider.setup) { setups.push(provider.setup(options[key as 'google' | 'local'] || {}, nuxt)) } } await Promise.all(setups) + for (const val of options.priority || []) { + if (val in providers) prioritisedProviders.add(val) + } + for (const provider in providers) { + prioritisedProviders.add(provider) + } }) const { normalizeFontData } = setupPublicAssetStrategy(options.assets) @@ -170,7 +177,7 @@ export default defineNuxtModule({ logger.warn(`Unknown provider \`${override.provider}\` for font family \`${fontFamily}\`. Falling back to default providers.`) } - for (const key in providers) { + for (const key of prioritisedProviders) { const provider = providers[key]! if (provider.resolveFontFaces) { const result = await provider.resolveFontFaces(fontFamily, defaults) diff --git a/src/types.ts b/src/types.ts index 98ce889..7883b51 100644 --- a/src/types.ts +++ b/src/types.ts @@ -145,11 +145,15 @@ export interface ModuleOptions { /** * An ordered list of providers to check when resolving font families. * + * After checking these providers, Nuxt Fonts will proceed by checking the + * * Default behaviour is to check all user providers in the order they were defined, and then all built-in providers. */ - // priority?: string[] - // TODO: support default provider - // provider?: FontProviderName + priority?: string[] + /** + * In some cases you may wish to use only one font provider. This is equivalent to disabling all other font providers. + */ + provider?: FontProviderName } export interface ModuleHooks {