fontaine 0.2.1 → 0.2.3
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/dist/index.cjs +33 -20
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +33 -20
- package/package.json +11 -11
package/dist/index.cjs
CHANGED
|
@@ -9,15 +9,22 @@ const ufo = require('ufo');
|
|
|
9
9
|
const scule = require('scule');
|
|
10
10
|
const pathe = require('pathe');
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
function* parseFontFace(css) {
|
|
13
13
|
const fontFamily = css.match(FAMILY_RE)?.groups.fontFamily;
|
|
14
|
-
const src = css.match(SOURCE_RE)?.groups.src;
|
|
15
14
|
const family = withoutQuotes(fontFamily?.split(",")[0] || "");
|
|
16
|
-
const
|
|
17
|
-
src?.split(",")
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
for (const match of css.matchAll(SOURCE_RE)) {
|
|
16
|
+
const sources = match.groups.src?.split(",") || [];
|
|
17
|
+
for (const entry of sources) {
|
|
18
|
+
for (const url of entry.matchAll(URL_RE)) {
|
|
19
|
+
const source = withoutQuotes(url.groups?.url || "");
|
|
20
|
+
if (source) {
|
|
21
|
+
yield { family, source };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
yield { family: "", source: "" };
|
|
27
|
+
}
|
|
21
28
|
const generateOverrideName = (name) => {
|
|
22
29
|
const firstFamily = withoutQuotes(name.split(",").shift());
|
|
23
30
|
return `${firstFamily} override`;
|
|
@@ -58,10 +65,12 @@ const FAMILY_RE = magicRegexp.createRegExp(
|
|
|
58
65
|
magicRegexp.exactly("font-family:").and(magicRegexp.whitespace.optionally()).and(magicRegexp.charNotIn(";}").times.any().as("fontFamily"))
|
|
59
66
|
);
|
|
60
67
|
const SOURCE_RE = magicRegexp.createRegExp(
|
|
61
|
-
magicRegexp.exactly("src:").and(magicRegexp.whitespace.optionally()).and(magicRegexp.charNotIn(";}").times.any().as("src"))
|
|
68
|
+
magicRegexp.exactly("src:").and(magicRegexp.whitespace.optionally()).and(magicRegexp.charNotIn(";}").times.any().as("src")),
|
|
69
|
+
["g"]
|
|
62
70
|
);
|
|
63
71
|
const URL_RE = magicRegexp.createRegExp(
|
|
64
|
-
magicRegexp.exactly("url(").and(magicRegexp.charNotIn(")").times.any().as("url")).and(")")
|
|
72
|
+
magicRegexp.exactly("url(").and(magicRegexp.charNotIn(")").times.any().as("url")).and(")"),
|
|
73
|
+
["g"]
|
|
65
74
|
);
|
|
66
75
|
|
|
67
76
|
const metricCache = {};
|
|
@@ -94,6 +103,7 @@ async function readMetrics(_source) {
|
|
|
94
103
|
return metrics;
|
|
95
104
|
}
|
|
96
105
|
|
|
106
|
+
const supportedExtensions = ["woff2", "woff", "ttf"];
|
|
97
107
|
const FontaineTransform = unplugin.createUnplugin(
|
|
98
108
|
(options) => {
|
|
99
109
|
const cssContext = options.css = options.css || {};
|
|
@@ -119,17 +129,20 @@ const FontaineTransform = unplugin.createUnplugin(
|
|
|
119
129
|
if (match.index === void 0 || !matchContent)
|
|
120
130
|
continue;
|
|
121
131
|
faceRanges.push([match.index, match.index + matchContent.length]);
|
|
122
|
-
const { family, source }
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
132
|
+
for (const { family, source } of parseFontFace(matchContent)) {
|
|
133
|
+
if (!family)
|
|
134
|
+
continue;
|
|
135
|
+
if (!supportedExtensions.some((e) => source?.endsWith(e)))
|
|
136
|
+
continue;
|
|
137
|
+
const metrics = await getMetricsForFamily(family) || source && await readMetricsFromId(source, id).catch(() => null);
|
|
138
|
+
if (metrics) {
|
|
139
|
+
const fontFace = generateFontFace(metrics, {
|
|
140
|
+
name: overrideName(family),
|
|
141
|
+
fallbacks: options.fallbacks
|
|
142
|
+
});
|
|
143
|
+
cssContext.value += fontFace;
|
|
144
|
+
s.appendLeft(match.index, fontFace);
|
|
145
|
+
}
|
|
133
146
|
}
|
|
134
147
|
}
|
|
135
148
|
for (const match of code.matchAll(FONT_FAMILY_RE)) {
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ interface FontaineTransformOptions {
|
|
|
11
11
|
overrideName?: (name: string) => string;
|
|
12
12
|
sourcemap?: boolean;
|
|
13
13
|
}
|
|
14
|
-
declare const FontaineTransform: unplugin.UnpluginInstance<FontaineTransformOptions>;
|
|
14
|
+
declare const FontaineTransform: unplugin.UnpluginInstance<FontaineTransformOptions, false>;
|
|
15
15
|
|
|
16
16
|
declare const generateOverrideName: (name: string) => string;
|
|
17
17
|
interface GenerateOptions {
|
package/dist/index.mjs
CHANGED
|
@@ -7,15 +7,22 @@ import { parseURL } from 'ufo';
|
|
|
7
7
|
import { camelCase } from 'scule';
|
|
8
8
|
import { isAbsolute, join } from 'pathe';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
function* parseFontFace(css) {
|
|
11
11
|
const fontFamily = css.match(FAMILY_RE)?.groups.fontFamily;
|
|
12
|
-
const src = css.match(SOURCE_RE)?.groups.src;
|
|
13
12
|
const family = withoutQuotes(fontFamily?.split(",")[0] || "");
|
|
14
|
-
const
|
|
15
|
-
src?.split(",")
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
for (const match of css.matchAll(SOURCE_RE)) {
|
|
14
|
+
const sources = match.groups.src?.split(",") || [];
|
|
15
|
+
for (const entry of sources) {
|
|
16
|
+
for (const url of entry.matchAll(URL_RE)) {
|
|
17
|
+
const source = withoutQuotes(url.groups?.url || "");
|
|
18
|
+
if (source) {
|
|
19
|
+
yield { family, source };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
yield { family: "", source: "" };
|
|
25
|
+
}
|
|
19
26
|
const generateOverrideName = (name) => {
|
|
20
27
|
const firstFamily = withoutQuotes(name.split(",").shift());
|
|
21
28
|
return `${firstFamily} override`;
|
|
@@ -56,10 +63,12 @@ const FAMILY_RE = createRegExp(
|
|
|
56
63
|
exactly("font-family:").and(whitespace.optionally()).and(charNotIn(";}").times.any().as("fontFamily"))
|
|
57
64
|
);
|
|
58
65
|
const SOURCE_RE = createRegExp(
|
|
59
|
-
exactly("src:").and(whitespace.optionally()).and(charNotIn(";}").times.any().as("src"))
|
|
66
|
+
exactly("src:").and(whitespace.optionally()).and(charNotIn(";}").times.any().as("src")),
|
|
67
|
+
["g"]
|
|
60
68
|
);
|
|
61
69
|
const URL_RE = createRegExp(
|
|
62
|
-
exactly("url(").and(charNotIn(")").times.any().as("url")).and(")")
|
|
70
|
+
exactly("url(").and(charNotIn(")").times.any().as("url")).and(")"),
|
|
71
|
+
["g"]
|
|
63
72
|
);
|
|
64
73
|
|
|
65
74
|
const metricCache = {};
|
|
@@ -92,6 +101,7 @@ async function readMetrics(_source) {
|
|
|
92
101
|
return metrics;
|
|
93
102
|
}
|
|
94
103
|
|
|
104
|
+
const supportedExtensions = ["woff2", "woff", "ttf"];
|
|
95
105
|
const FontaineTransform = createUnplugin(
|
|
96
106
|
(options) => {
|
|
97
107
|
const cssContext = options.css = options.css || {};
|
|
@@ -117,17 +127,20 @@ const FontaineTransform = createUnplugin(
|
|
|
117
127
|
if (match.index === void 0 || !matchContent)
|
|
118
128
|
continue;
|
|
119
129
|
faceRanges.push([match.index, match.index + matchContent.length]);
|
|
120
|
-
const { family, source }
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
for (const { family, source } of parseFontFace(matchContent)) {
|
|
131
|
+
if (!family)
|
|
132
|
+
continue;
|
|
133
|
+
if (!supportedExtensions.some((e) => source?.endsWith(e)))
|
|
134
|
+
continue;
|
|
135
|
+
const metrics = await getMetricsForFamily(family) || source && await readMetricsFromId(source, id).catch(() => null);
|
|
136
|
+
if (metrics) {
|
|
137
|
+
const fontFace = generateFontFace(metrics, {
|
|
138
|
+
name: overrideName(family),
|
|
139
|
+
fallbacks: options.fallbacks
|
|
140
|
+
});
|
|
141
|
+
cssContext.value += fontFace;
|
|
142
|
+
s.appendLeft(match.index, fontFace);
|
|
143
|
+
}
|
|
131
144
|
}
|
|
132
145
|
}
|
|
133
146
|
for (const match of code.matchAll(FONT_FAMILY_RE)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fontaine",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Automatic font fallback based on font metrics",
|
|
5
5
|
"repository": "unjs/fontaine",
|
|
6
6
|
"keywords": [
|
|
@@ -48,23 +48,23 @@
|
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@capsizecss/metrics": "^0.3.0",
|
|
50
50
|
"@capsizecss/unpack": "^0.1.0",
|
|
51
|
-
"magic-regexp": "^0.
|
|
51
|
+
"magic-regexp": "^0.6.0",
|
|
52
52
|
"magic-string": "^0.26.7",
|
|
53
53
|
"pathe": "^0.3.9",
|
|
54
54
|
"scule": "^0.3.2",
|
|
55
55
|
"ufo": "^0.8.6",
|
|
56
|
-
"unplugin": "^0.10.
|
|
56
|
+
"unplugin": "^0.10.2"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@nuxtjs/eslint-config-typescript": "latest",
|
|
60
60
|
"@release-it/conventional-changelog": "latest",
|
|
61
|
-
"@types/node": "
|
|
61
|
+
"@types/node": "^18.11.8",
|
|
62
62
|
"@types/serve-handler": "^6.1.1",
|
|
63
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
64
|
-
"@typescript-eslint/parser": "^5.
|
|
65
|
-
"@vitest/coverage-c8": "^0.24.
|
|
63
|
+
"@typescript-eslint/eslint-plugin": "^5.41.0",
|
|
64
|
+
"@typescript-eslint/parser": "^5.41.0",
|
|
65
|
+
"@vitest/coverage-c8": "^0.24.4",
|
|
66
66
|
"conventional-changelog-conventionalcommits": "latest",
|
|
67
|
-
"eslint": "
|
|
67
|
+
"eslint": "^8.26.0",
|
|
68
68
|
"eslint-config-prettier": "latest",
|
|
69
69
|
"eslint-plugin-prettier": "latest",
|
|
70
70
|
"execa": "^6.1.0",
|
|
@@ -77,11 +77,11 @@
|
|
|
77
77
|
"serve-handler": "^6.1.3",
|
|
78
78
|
"typescript": "latest",
|
|
79
79
|
"unbuild": "latest",
|
|
80
|
-
"vite": "
|
|
81
|
-
"vitest": "
|
|
80
|
+
"vite": "^3.2.2",
|
|
81
|
+
"vitest": "^0.24.4"
|
|
82
82
|
},
|
|
83
83
|
"resolutions": {
|
|
84
84
|
"fontaine": "link:."
|
|
85
85
|
},
|
|
86
|
-
"packageManager": "pnpm@7.
|
|
86
|
+
"packageManager": "pnpm@7.14.1"
|
|
87
87
|
}
|