Skip to content

Commit

Permalink
feat: support passing options to font providers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 20, 2024
1 parent 3dc5f54 commit 306d2df
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default defineNuxtModule<ModuleOptions>({
},
defaults: {
defaults: {},
local: {},
google: { strategy: 'public' },
providers: {
local,
google,
Expand All @@ -55,7 +57,7 @@ export default defineNuxtModule<ModuleOptions>({
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)
Expand Down
1 change: 1 addition & 0 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const fontAPI = $fetch.create({
})

let fonts: FontIndexMeta[]

export default {
async setup () {
// TODO: Fetch and cache possible Google fonts
Expand Down
4 changes: 2 additions & 2 deletions src/providers/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(),
Expand Down
28 changes: 16 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,12 +48,12 @@ export interface ResolveFontFacesOptions {
subsets: string[]
}

export interface FontProvider {
export interface FontProvider<FontProviderOptions = Record<string, unknown>> {
/**
* 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<void>
setup?: (options: FontProviderOptions, nuxt: Nuxt) => Awaitable<void>
/**
* Resolve data for `@font-face` declarations.
*
Expand All @@ -67,6 +67,9 @@ export interface FontProvider {
*/
fonts: FontFaceData | FontFaceData[]
}>
// resolveFontMetrics?: (fontFamily: string, fonts: FontFaceData[], options: ResolveFontFacesOptions) => Awaitable<void | {
// fallbackName: string
// }>
}

export type FontProviderName = (string & {}) | 'google' | 'local' | 'none'
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 306d2df

Please sign in to comment.