Skip to content

Commit 06034f2

Browse files
feat(gatsby-plugin-subfont): Make async, add configurable opti… (#21768)
Add gatsby-plugin-subfont configurable option feature Update subfont version Co-authored-by: Ward Peeters <[email protected]>
1 parent a49df73 commit 06034f2

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

packages/gatsby-plugin-subfont/README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,38 @@ If you want the ability to run font subsetting locally you'l need Python and ins
1717
```javascript
1818
// In your gatsby-config.js
1919
module.exports = {
20-
plugins: [`gatsby-plugin-subfont`],
20+
plugins: [
21+
{
22+
resolve: `gatsby-plugin-subfont`,
23+
options: {
24+
silent: true,
25+
fallback: false,
26+
inlineFonts: true,
27+
},
28+
},
29+
],
2130
}
2231
```
32+
33+
## Options
34+
35+
See [subfont](https://github.com/Munter/subfont/blob/4b5a59afd17008ca35b6c32b52e3e922159e22fc/lib/subfont.js#L10) for a full list of options.
36+
37+
| Name | Default | Description |
38+
| --------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------- |
39+
| `root` | `public` | Path to your web root |
40+
| `canonicalRoot` | | URI root where the site will be deployed. Must be either an absolute, a protocol-relative, or a root-relative url |
41+
| `output` | | Directory where results should be written to | | |
42+
| `fallbacks` | `true` | Include fallbacks so the original font will be loaded when dynamic content gets injected at runtime. |
43+
| `dynamic` | `false` | Also trace the usage of fonts in a headless browser with JavaScript enabled |
44+
| `inPlace` | `true` | Modify HTML-files in-place. Only use on build artifacts |
45+
| `inlineFonts` | `false` | Inline fonts as data-URIs inside the @font-face declaration |
46+
| `inlineCss` | `true` | Inline CSS that declares the @font-face for the subset fonts |
47+
| `fontDisplay` | `swap` | Injects a font-display value into the @font-face CSS. Valid values: auto, block, swap, fallback, optional |
48+
| `formats` | `['woff2', 'woff']` | Font formats to use when subsetting. [choices: "woff2", "woff", "truetype"] |
49+
| `subsetPerPage` | `false` | Create a unique subset for each page. |
50+
| `recursive` | `false` | Crawl all HTML-pages linked with relative and root relative links. This stays inside your domain |
51+
| `silent` | `true` | Do not write anything to stdout |
52+
| `debug` | `false` | Verbose insights into font glyph detection |
53+
| `dryRun` | `false` | Don't write anything to disk |
54+
| `inputFiles` | `['public/index.html']` | htmlFile(s) or url(s) |

packages/gatsby-plugin-subfont/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
},
2525
"license": "MIT",
2626
"dependencies": {
27-
"@babel/runtime": "^7.7.6",
28-
"shell-escape": "^0.2.0",
29-
"subfont": "^3.7.1"
27+
"subfont": "^4.2.0"
3028
},
3129
"devDependencies": {
3230
"@babel/cli": "^7.7.5",
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
const path = require(`path`)
2-
const { execSync } = require(`child_process`)
3-
const shellescape = require(`shell-escape`)
2+
const subfont = require(`subfont`)
43

5-
exports.onPostBuild = ({ store }) => {
4+
exports.onPostBuild = async ({ store, reporter }, options) => {
65
const root = path.join(store.getState().program.directory, `public`)
7-
// TODO make this configurable
8-
const urlPaths = [`/`]
9-
const baseArgs = [
10-
`node_modules/.bin/subfont`,
11-
`-i`,
12-
`--no-recursive`,
13-
`--inline-css`,
14-
`--root`,
15-
`file://${root}`,
16-
]
17-
const args = baseArgs.concat(
18-
urlPaths.map(currentPath => path.join(root, currentPath, `index.html`))
6+
const subfontConsole = {
7+
log: reporter.info,
8+
warn: reporter.warn,
9+
error: reporter.error,
10+
}
11+
12+
await subfont(
13+
{
14+
root,
15+
inPlace: true,
16+
inlineCss: true,
17+
silent: true,
18+
inputFiles: [path.join(root, `index.html`)],
19+
...options,
20+
},
21+
subfontConsole
1922
)
20-
const command = shellescape(args)
21-
execSync(command)
2223
}

0 commit comments

Comments
 (0)