Skip to content

Commit

Permalink
feat: support priority and provider options
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 9, 2024
1 parent a824bed commit bd8da26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
})
```
Expand Down
11 changes: 9 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,27 @@ export default defineNuxtModule<ModuleOptions>({
}

const providers = await resolveProviders(options.providers)
const prioritisedProviders = new Set<string>()

// Allow registering and disabling providers
nuxt.hook('modules:done', async () => {
await nuxt.callHook('fonts:providers', providers)
const setups: Array<void | Promise<void>> = []
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)
Expand Down Expand Up @@ -170,7 +177,7 @@ export default defineNuxtModule<ModuleOptions>({
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)
Expand Down
10 changes: 7 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bd8da26

Please sign in to comment.