-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for self-hosted fonts #10
Changes from all commits
39ccce1
a02f77d
bc0d4fe
3bdfa8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import postcss from "postcss"; | ||
import postcssJs from "postcss-js"; | ||
|
||
const defaultFontOptions = { | ||
fontDisplay: `swap`, | ||
}; | ||
|
||
const getFontFace = async font => { | ||
const { family, urls, ...cssProperties } = createFontOptions(font); | ||
|
||
const { css } = await postcss().process(cssProperties, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Top! |
||
parser: postcssJs, | ||
from: undefined, | ||
}); | ||
|
||
const src = Object.entries(urls) | ||
.map(([format, url]) => `url("${url}") format("${format}")`) | ||
.join(); | ||
|
||
return ` | ||
@font-face { | ||
font-family: "${family}"; | ||
src: ${src}; | ||
${css} | ||
} | ||
`; | ||
}; | ||
|
||
export default function selfHosted() { | ||
return fonts => Promise.all(fonts.map(getFontFace)); | ||
} | ||
|
||
function createFontOptions(options) { | ||
return { | ||
...defaultFontOptions, | ||
...options, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,12 @@ import postcss from "postcss"; | |
import cssnano from "cssnano"; | ||
|
||
import google from "./modules/google"; | ||
import selfHosted from "./modules/selfHosted"; | ||
|
||
export default async function webFonts(options) { | ||
const modules = { | ||
google: google(options), | ||
selfHosted: selfHosted(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's exactly how it was intended There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The options have to be passed here. For example for the pathprefix... |
||
}; | ||
|
||
const merge = async css => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We assume the fonts are in static folder and copied to the app root.
Here I am not sure if it would be better to specify the font file source.
What do you think?