Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions e2e-tests/development-runtime/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ module.exports = {
variants: [`300`, `400`, `500`],
},
],
selfHosted: [
{
family: `Open Sans`,
urls: {
woff2: `/OpenSans400.woff2`,
Copy link
Owner

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.

src: {
                woff2: path.join('static', `OpenSans400.woff2'),
}

What do you think?

},
fontStyle: `normal`,
fontWeight: 400,
},
],
},
},
},
Expand Down
Binary file not shown.
14 changes: 14 additions & 0 deletions gatsby-plugin-webfonts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ module.exports = {
//strategy: 'selfHosted' // 'base64' || 'cdn'
},
],
selfHosted: [
{
family: "Open Sans",
urls: {
woff2: `/OpenSans400.woff2`,
woff: `/OpenSans400.woff`,
truetype: `/OpenSans400.ttf`,
},
//fontStyle: 'normal'
//fontDisplay: 'swap',
//fontWeight: 400,
//cssProperty: 'value'
},
],
},
//formats: ['woff2', 'woff'],
//useMinify: true,
Expand Down
38 changes: 38 additions & 0 deletions gatsby-plugin-webfonts/src/modules/selfHosted.js
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, {
Copy link
Owner

Choose a reason for hiding this comment

The 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,
};
}
2 changes: 2 additions & 0 deletions gatsby-plugin-webfonts/src/web-fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's exactly how it was intended

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options have to be passed here. For example for the pathprefix...
See https://github.com/hupe1980/gatsby-plugin-webfonts/tree/master/e2e-tests/path-prefix-prod-runtime

};

const merge = async css => {
Expand Down