fontaine 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/index.cjs +4 -1
- package/dist/index.mjs +5 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -86,11 +86,31 @@ function fontainePlugin(_context, _options) {
|
|
|
86
86
|
},
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
+
|
|
90
|
+
// Gatsby config - gatsby-node.js
|
|
91
|
+
const { FontaineTransform } = require('fontaine')
|
|
92
|
+
|
|
93
|
+
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
|
|
94
|
+
const config = getConfig()
|
|
95
|
+
config.plugins.push(FontaineTransform.webpack(options))
|
|
96
|
+
actions.replaceWebpackConfig(config)
|
|
97
|
+
}
|
|
89
98
|
```
|
|
90
99
|
|
|
91
100
|
> **Note**
|
|
92
101
|
> If you are using Nuxt, check out [nuxt-font-metrics](https://github.com/danielroe/nuxt-font-metrics) which uses `fontaine` under the hood.
|
|
93
102
|
|
|
103
|
+
If your custom font is used through the mechanism of CSS variables, you'll need to make a tweak to your CSS variables to give fontaine a helping hand. Docusaurus is an example of this, it uses the `--ifm-font-family-base` variable to reference a custom font. In order that fontaine can connect the variable with the font, we need to add a `{Name of Font} override` suffix to that variable. What does this look like? Well imagine we were using the custom font Poppins which is referenced from the `--ifm-font-family-base` variable, we'd make the following adjustment:
|
|
104
|
+
|
|
105
|
+
```diff
|
|
106
|
+
:root {
|
|
107
|
+
/* ... */
|
|
108
|
+
- --ifm-font-family-base: 'Poppins';
|
|
109
|
+
+ --ifm-font-family-base: 'Poppins', 'Poppins override';
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Behind the scenes, there is a 'Poppins override' `@font-face` rule that has been created by fontaine. By manually adding this override font family to our CSS variable, we make our site use the fallback `@font-face` rule with the correct font metrics that fontaine generates.
|
|
113
|
+
|
|
94
114
|
## How it works
|
|
95
115
|
|
|
96
116
|
`fontaine` will scan your `@font-face` rules and generate fallback rules with the correct metrics. For example:
|
package/dist/index.cjs
CHANGED
|
@@ -109,7 +109,7 @@ const FontaineTransform = unplugin.createUnplugin(
|
|
|
109
109
|
enforce: "pre",
|
|
110
110
|
transformInclude(id) {
|
|
111
111
|
const { pathname } = ufo.parseURL(id);
|
|
112
|
-
return
|
|
112
|
+
return CSS_RE.test(pathname) || CSS_RE.test(id);
|
|
113
113
|
},
|
|
114
114
|
async transform(code, id) {
|
|
115
115
|
const s = new MagicString(code);
|
|
@@ -169,6 +169,9 @@ const FONT_FAMILY_RE = magicRegexp.createRegExp(
|
|
|
169
169
|
magicRegexp.charNotIn(";}").times.any().as("family").after(magicRegexp.exactly("font-family:").and(magicRegexp.whitespace.times.any())).before(magicRegexp.charIn(";}").or(magicRegexp.exactly("").at.lineEnd())),
|
|
170
170
|
["g"]
|
|
171
171
|
);
|
|
172
|
+
const CSS_RE = magicRegexp.createRegExp(
|
|
173
|
+
magicRegexp.exactly(".").and(magicRegexp.anyOf("sass", "css", "scss")).at.lineEnd()
|
|
174
|
+
);
|
|
172
175
|
|
|
173
176
|
exports.FontaineTransform = FontaineTransform;
|
|
174
177
|
exports.generateFontFace = generateFontFace;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createUnplugin } from 'unplugin';
|
|
2
|
-
import { createRegExp, charIn, exactly, whitespace, charNotIn } from 'magic-regexp';
|
|
2
|
+
import { createRegExp, charIn, exactly, whitespace, charNotIn, anyOf } from 'magic-regexp';
|
|
3
3
|
import MagicString from 'magic-string';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
5
|
import { fromFile, fromUrl } from '@capsizecss/unpack';
|
|
@@ -107,7 +107,7 @@ const FontaineTransform = createUnplugin(
|
|
|
107
107
|
enforce: "pre",
|
|
108
108
|
transformInclude(id) {
|
|
109
109
|
const { pathname } = parseURL(id);
|
|
110
|
-
return
|
|
110
|
+
return CSS_RE.test(pathname) || CSS_RE.test(id);
|
|
111
111
|
},
|
|
112
112
|
async transform(code, id) {
|
|
113
113
|
const s = new MagicString(code);
|
|
@@ -167,5 +167,8 @@ const FONT_FAMILY_RE = createRegExp(
|
|
|
167
167
|
charNotIn(";}").times.any().as("family").after(exactly("font-family:").and(whitespace.times.any())).before(charIn(";}").or(exactly("").at.lineEnd())),
|
|
168
168
|
["g"]
|
|
169
169
|
);
|
|
170
|
+
const CSS_RE = createRegExp(
|
|
171
|
+
exactly(".").and(anyOf("sass", "css", "scss")).at.lineEnd()
|
|
172
|
+
);
|
|
170
173
|
|
|
171
174
|
export { FontaineTransform, generateFontFace, generateOverrideName, getMetricsForFamily, readMetrics };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fontaine",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Automatic font fallback based on font metrics",
|
|
5
5
|
"repository": "unjs/fontaine",
|
|
6
6
|
"keywords": [
|
|
@@ -53,15 +53,15 @@
|
|
|
53
53
|
"pathe": "^0.3.9",
|
|
54
54
|
"scule": "^0.3.2",
|
|
55
55
|
"ufo": "^0.8.6",
|
|
56
|
-
"unplugin": "^0.
|
|
56
|
+
"unplugin": "^0.10.0"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@nuxtjs/eslint-config-typescript": "latest",
|
|
60
60
|
"@release-it/conventional-changelog": "latest",
|
|
61
61
|
"@types/node": "latest",
|
|
62
62
|
"@types/serve-handler": "^6.1.1",
|
|
63
|
-
"@typescript-eslint/eslint-plugin": "^5.40.
|
|
64
|
-
"@typescript-eslint/parser": "^5.40.
|
|
63
|
+
"@typescript-eslint/eslint-plugin": "^5.40.1",
|
|
64
|
+
"@typescript-eslint/parser": "^5.40.1",
|
|
65
65
|
"@vitest/coverage-c8": "^0.24.3",
|
|
66
66
|
"conventional-changelog-conventionalcommits": "latest",
|
|
67
67
|
"eslint": "latest",
|