Skip to content

Commit

Permalink
feat: allow customising font override name
Browse files Browse the repository at this point in the history
resolves #19
  • Loading branch information
danielroe committed Oct 16, 2022
1 parent 349f549 commit fa3470b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -52,6 +52,8 @@ const options = {
fallbacks: ['BlinkMacSystemFont', 'Segoe UI', 'Helvetica Neue', 'Arial', 'Noto Sans'],
// You may need to resolve assets like `/fonts/Roboto.woff2` to a particular directory
resolvePath: (id) => 'file:///path/to/public/dir' + id,
// overrideName: (originalName) => `${name} override`
// sourcemap: false
}

// Vite
Expand Down
9 changes: 6 additions & 3 deletions src/transform.ts
Expand Up @@ -16,20 +16,23 @@ interface FontaineTransformOptions {
css?: { value?: string }
fallbacks: string[]
resolvePath?: (path: string) => string | URL
/** this should produce an unquoted font family name */
overrideName?: (name: string) => string
sourcemap?: boolean
}

export const FontaineTransform = createUnplugin(
(options: FontaineTransformOptions) => {
const cssContext = (options.css = options.css || {})
cssContext.value = ''
options.resolvePath = options.resolvePath || (id => id)
const resolvePath = options.resolvePath || (id => id)
const overrideName = options.overrideName || generateOverrideName

function readMetricsFromId(path: string, importer: string) {
const resolvedPath =
isAbsolute(importer) && path.startsWith('.')
? join(importer, path)
: options.resolvePath!(path)
: resolvePath(path)
return readMetrics(resolvedPath)
}

Expand Down Expand Up @@ -59,7 +62,7 @@ export const FontaineTransform = createUnplugin(

if (metrics) {
const fontFace = generateFontFace(metrics, {
name: generateOverrideName(family),
name: overrideName(family),
fallbacks: options.fallbacks,
})
cssContext.value += fontFace
Expand Down

0 comments on commit fa3470b

Please sign in to comment.