@unocss/preset-web-fonts 0.44.7 → 0.45.4
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 +2 -1
- package/dist/index.cjs +32 -24
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +32 -24
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -70,12 +70,13 @@ Currently supported Providers:
|
|
|
70
70
|
|
|
71
71
|
- `none` - do nothing, treat the font as system font
|
|
72
72
|
- `google` - [Google Fonts](https://fonts.google.com/)
|
|
73
|
+
- `bunny` - [Privacy-Friendly Google Fonts](https://fonts.bunny.net/)
|
|
73
74
|
|
|
74
75
|
PR welcome to add more providers 🙌
|
|
75
76
|
|
|
76
77
|
## Configuration
|
|
77
78
|
|
|
78
|
-
Refer to the [type definition](https://github.com/unocss/unocss/blob/main/packages/preset-web-fonts/src/types.ts#L4) for all configurations
|
|
79
|
+
Refer to the [type definition](https://github.com/unocss/unocss/blob/main/packages/preset-web-fonts/src/types.ts#L4) for all configurations available.
|
|
79
80
|
|
|
80
81
|
## License
|
|
81
82
|
|
package/dist/index.cjs
CHANGED
|
@@ -4,22 +4,27 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
const core = require('@unocss/core');
|
|
6
6
|
|
|
7
|
-
const GoogleFontsProvider =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
7
|
+
const GoogleFontsProvider = createGoogleProvider("google", "https://fonts.googleapis.com");
|
|
8
|
+
function createGoogleProvider(name, host) {
|
|
9
|
+
return {
|
|
10
|
+
name,
|
|
11
|
+
getImportUrl(fonts) {
|
|
12
|
+
const strings = fonts.filter((i) => i.provider === name).map((i) => {
|
|
13
|
+
let name2 = i.name.replace(/\s+/g, "+");
|
|
14
|
+
if (i.weights?.length) {
|
|
15
|
+
name2 += i.italic ? `:ital,wght@${i.weights.flatMap((i2) => [`0,${i2}`, `1,${i2}`]).sort().join(";")}` : `:wght@${i.weights.sort().join(";")}`;
|
|
16
|
+
}
|
|
17
|
+
return `family=${name2}`;
|
|
18
|
+
}).join("&");
|
|
19
|
+
return `${host}/css2?${strings}&display=swap`;
|
|
20
|
+
},
|
|
21
|
+
getFontName(font) {
|
|
22
|
+
return `"${font.name}"`;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const BunnyFontsProvider = createGoogleProvider("bunny", "https://fonts.bunny.net");
|
|
23
28
|
|
|
24
29
|
const NoneProvider = {
|
|
25
30
|
name: "none",
|
|
@@ -45,6 +50,7 @@ function normalizedFontMeta(meta, defaultProvider) {
|
|
|
45
50
|
}
|
|
46
51
|
const providers = {
|
|
47
52
|
google: GoogleFontsProvider,
|
|
53
|
+
bunny: BunnyFontsProvider,
|
|
48
54
|
none: NoneProvider
|
|
49
55
|
};
|
|
50
56
|
const preset = (options = {}) => {
|
|
@@ -54,23 +60,23 @@ const preset = (options = {}) => {
|
|
|
54
60
|
inlineImports = true,
|
|
55
61
|
themeKey = "fontFamily"
|
|
56
62
|
} = options;
|
|
57
|
-
const fontObject = Object.fromEntries(
|
|
63
|
+
const fontObject = Object.fromEntries(
|
|
64
|
+
Object.entries(options.fonts || {}).map(([name, meta]) => [name, core.toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))])
|
|
65
|
+
);
|
|
58
66
|
const fonts = Object.values(fontObject).flatMap((i) => i);
|
|
59
67
|
const importCache = {};
|
|
60
68
|
async function importUrl(url) {
|
|
61
69
|
if (inlineImports) {
|
|
62
70
|
if (!importCache[url]) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
importCache[url] = await $fetch(url, { headers: {} });
|
|
66
|
-
} catch (e) {
|
|
71
|
+
const { $fetch } = await import('ohmyfetch');
|
|
72
|
+
importCache[url] = $fetch(url, { headers: {}, retry: 3 }).catch((e) => {
|
|
67
73
|
console.error("Failed to fetch web fonts");
|
|
68
74
|
console.error(e);
|
|
69
75
|
if (typeof process !== "undefined" && process.env.CI)
|
|
70
76
|
throw e;
|
|
71
|
-
}
|
|
77
|
+
});
|
|
72
78
|
}
|
|
73
|
-
return importCache[url];
|
|
79
|
+
return await importCache[url];
|
|
74
80
|
} else {
|
|
75
81
|
return `@import url('${url}')`;
|
|
76
82
|
}
|
|
@@ -101,7 +107,9 @@ const preset = (options = {}) => {
|
|
|
101
107
|
preset2.extendTheme = (theme) => {
|
|
102
108
|
if (!theme[themeKey])
|
|
103
109
|
theme[themeKey] = {};
|
|
104
|
-
const obj = Object.fromEntries(
|
|
110
|
+
const obj = Object.fromEntries(
|
|
111
|
+
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => providers[f.provider || defaultProvider].getFontName(f))])
|
|
112
|
+
);
|
|
105
113
|
for (const key of Object.keys(obj)) {
|
|
106
114
|
if (typeof theme[themeKey][key] === "string")
|
|
107
115
|
theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import { toArray } from '@unocss/core';
|
|
2
2
|
|
|
3
|
-
const GoogleFontsProvider =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
3
|
+
const GoogleFontsProvider = createGoogleProvider("google", "https://fonts.googleapis.com");
|
|
4
|
+
function createGoogleProvider(name, host) {
|
|
5
|
+
return {
|
|
6
|
+
name,
|
|
7
|
+
getImportUrl(fonts) {
|
|
8
|
+
const strings = fonts.filter((i) => i.provider === name).map((i) => {
|
|
9
|
+
let name2 = i.name.replace(/\s+/g, "+");
|
|
10
|
+
if (i.weights?.length) {
|
|
11
|
+
name2 += i.italic ? `:ital,wght@${i.weights.flatMap((i2) => [`0,${i2}`, `1,${i2}`]).sort().join(";")}` : `:wght@${i.weights.sort().join(";")}`;
|
|
12
|
+
}
|
|
13
|
+
return `family=${name2}`;
|
|
14
|
+
}).join("&");
|
|
15
|
+
return `${host}/css2?${strings}&display=swap`;
|
|
16
|
+
},
|
|
17
|
+
getFontName(font) {
|
|
18
|
+
return `"${font.name}"`;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const BunnyFontsProvider = createGoogleProvider("bunny", "https://fonts.bunny.net");
|
|
19
24
|
|
|
20
25
|
const NoneProvider = {
|
|
21
26
|
name: "none",
|
|
@@ -41,6 +46,7 @@ function normalizedFontMeta(meta, defaultProvider) {
|
|
|
41
46
|
}
|
|
42
47
|
const providers = {
|
|
43
48
|
google: GoogleFontsProvider,
|
|
49
|
+
bunny: BunnyFontsProvider,
|
|
44
50
|
none: NoneProvider
|
|
45
51
|
};
|
|
46
52
|
const preset = (options = {}) => {
|
|
@@ -50,23 +56,23 @@ const preset = (options = {}) => {
|
|
|
50
56
|
inlineImports = true,
|
|
51
57
|
themeKey = "fontFamily"
|
|
52
58
|
} = options;
|
|
53
|
-
const fontObject = Object.fromEntries(
|
|
59
|
+
const fontObject = Object.fromEntries(
|
|
60
|
+
Object.entries(options.fonts || {}).map(([name, meta]) => [name, toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))])
|
|
61
|
+
);
|
|
54
62
|
const fonts = Object.values(fontObject).flatMap((i) => i);
|
|
55
63
|
const importCache = {};
|
|
56
64
|
async function importUrl(url) {
|
|
57
65
|
if (inlineImports) {
|
|
58
66
|
if (!importCache[url]) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
importCache[url] = await $fetch(url, { headers: {} });
|
|
62
|
-
} catch (e) {
|
|
67
|
+
const { $fetch } = await import('ohmyfetch');
|
|
68
|
+
importCache[url] = $fetch(url, { headers: {}, retry: 3 }).catch((e) => {
|
|
63
69
|
console.error("Failed to fetch web fonts");
|
|
64
70
|
console.error(e);
|
|
65
71
|
if (typeof process !== "undefined" && process.env.CI)
|
|
66
72
|
throw e;
|
|
67
|
-
}
|
|
73
|
+
});
|
|
68
74
|
}
|
|
69
|
-
return importCache[url];
|
|
75
|
+
return await importCache[url];
|
|
70
76
|
} else {
|
|
71
77
|
return `@import url('${url}')`;
|
|
72
78
|
}
|
|
@@ -97,7 +103,9 @@ const preset = (options = {}) => {
|
|
|
97
103
|
preset2.extendTheme = (theme) => {
|
|
98
104
|
if (!theme[themeKey])
|
|
99
105
|
theme[themeKey] = {};
|
|
100
|
-
const obj = Object.fromEntries(
|
|
106
|
+
const obj = Object.fromEntries(
|
|
107
|
+
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => providers[f.provider || defaultProvider].getFontName(f))])
|
|
108
|
+
);
|
|
101
109
|
for (const key of Object.keys(obj)) {
|
|
102
110
|
if (typeof theme[themeKey][key] === "string")
|
|
103
111
|
theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/preset-web-fonts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.45.4",
|
|
4
4
|
"description": "Web Fonts support for Uno CSS",
|
|
5
5
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"*.css"
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@unocss/core": "0.
|
|
45
|
+
"@unocss/core": "0.45.4",
|
|
46
46
|
"ohmyfetch": "^0.4.18"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|