@unocss/preset-web-fonts 0.51.6 → 0.51.8
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 +23 -24
- package/dist/index.d.ts +9 -4
- package/dist/index.mjs +23 -25
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -4,12 +4,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
const core = require('@unocss/core');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
function createGoogleProvider(name, host) {
|
|
7
|
+
function createGoogleCompatibleProvider(name, host) {
|
|
9
8
|
return {
|
|
10
9
|
name,
|
|
11
10
|
getImportUrl(fonts) {
|
|
12
|
-
const strings = fonts.
|
|
11
|
+
const strings = fonts.map((i) => {
|
|
13
12
|
let name2 = i.name.replace(/\s+/g, "+");
|
|
14
13
|
if (i.weights?.length) {
|
|
15
14
|
name2 += i.italic ? `:ital,wght@${i.weights.flatMap((i2) => [`0,${i2}`, `1,${i2}`]).sort().join(";")}` : `:wght@${i.weights.sort().join(";")}`;
|
|
@@ -17,21 +16,19 @@ function createGoogleProvider(name, host) {
|
|
|
17
16
|
return `family=${name2}`;
|
|
18
17
|
}).join("&");
|
|
19
18
|
return `${host}/css2?${strings}&display=swap`;
|
|
20
|
-
},
|
|
21
|
-
getFontName(font) {
|
|
22
|
-
return `"${font.name}"`;
|
|
23
19
|
}
|
|
24
20
|
};
|
|
25
21
|
}
|
|
22
|
+
const GoogleFontsProvider = createGoogleCompatibleProvider("google", "https://fonts.googleapis.com");
|
|
26
23
|
|
|
27
|
-
const BunnyFontsProvider =
|
|
24
|
+
const BunnyFontsProvider = createGoogleCompatibleProvider("bunny", "https://fonts.bunny.net");
|
|
28
25
|
|
|
29
26
|
const FontshareProvider = createFontshareProvider("fontshare", "https://api.fontshare.com");
|
|
30
27
|
function createFontshareProvider(name, host) {
|
|
31
28
|
return {
|
|
32
29
|
name,
|
|
33
30
|
getImportUrl(fonts) {
|
|
34
|
-
const strings = fonts.
|
|
31
|
+
const strings = fonts.map((f) => {
|
|
35
32
|
let name2 = f.name.replace(/\s+/g, "-").toLocaleLowerCase();
|
|
36
33
|
if (f.weights?.length)
|
|
37
34
|
name2 += `@${f.weights.flatMap((w) => f.italic ? Number(w) + 1 : w).sort().join()}`;
|
|
@@ -40,9 +37,6 @@ function createFontshareProvider(name, host) {
|
|
|
40
37
|
return `f[]=${name2}`;
|
|
41
38
|
}).join("&");
|
|
42
39
|
return `${host}/v2/css?${strings}&display=swap`;
|
|
43
|
-
},
|
|
44
|
-
getFontName(font) {
|
|
45
|
-
return `"${font.name}"`;
|
|
46
40
|
}
|
|
47
41
|
};
|
|
48
42
|
}
|
|
@@ -57,24 +51,29 @@ const NoneProvider = {
|
|
|
57
51
|
}
|
|
58
52
|
};
|
|
59
53
|
|
|
54
|
+
const builtinProviders = {
|
|
55
|
+
google: GoogleFontsProvider,
|
|
56
|
+
bunny: BunnyFontsProvider,
|
|
57
|
+
fontshare: FontshareProvider,
|
|
58
|
+
none: NoneProvider
|
|
59
|
+
};
|
|
60
|
+
function resolveProvider(provider) {
|
|
61
|
+
if (typeof provider === "string")
|
|
62
|
+
return builtinProviders[provider];
|
|
63
|
+
return provider;
|
|
64
|
+
}
|
|
60
65
|
function normalizedFontMeta(meta, defaultProvider) {
|
|
61
66
|
if (typeof meta !== "string") {
|
|
62
|
-
meta.provider = meta.provider
|
|
67
|
+
meta.provider = resolveProvider(meta.provider || defaultProvider);
|
|
63
68
|
return meta;
|
|
64
69
|
}
|
|
65
70
|
const [name, weights = ""] = meta.split(":");
|
|
66
71
|
return {
|
|
67
72
|
name,
|
|
68
73
|
weights: weights.split(/[,;]\s*/).filter(Boolean),
|
|
69
|
-
provider: defaultProvider
|
|
74
|
+
provider: resolveProvider(defaultProvider)
|
|
70
75
|
};
|
|
71
76
|
}
|
|
72
|
-
const providers = {
|
|
73
|
-
google: GoogleFontsProvider,
|
|
74
|
-
bunny: BunnyFontsProvider,
|
|
75
|
-
fontshare: FontshareProvider,
|
|
76
|
-
none: NoneProvider
|
|
77
|
-
};
|
|
78
77
|
function preset(options = {}) {
|
|
79
78
|
const {
|
|
80
79
|
provider: defaultProvider = "google",
|
|
@@ -105,16 +104,15 @@ function preset(options = {}) {
|
|
|
105
104
|
return `@import url('${url}')`;
|
|
106
105
|
}
|
|
107
106
|
}
|
|
107
|
+
const enabledProviders = new Set(fonts.map((i) => i.provider));
|
|
108
108
|
const preset2 = {
|
|
109
109
|
name: "@unocss/preset-web-fonts",
|
|
110
110
|
preflights: [
|
|
111
111
|
{
|
|
112
112
|
async getCSS() {
|
|
113
|
-
const names = new Set(fonts.map((i) => i.provider || defaultProvider));
|
|
114
113
|
const preflights = [];
|
|
115
|
-
for (const
|
|
116
|
-
const fontsForProvider = fonts.filter((i) => i.provider === name);
|
|
117
|
-
const provider = providers[name];
|
|
114
|
+
for (const provider of enabledProviders) {
|
|
115
|
+
const fontsForProvider = fonts.filter((i) => i.provider.name === provider.name);
|
|
118
116
|
if (provider.getImportUrl) {
|
|
119
117
|
const url = provider.getImportUrl(fontsForProvider);
|
|
120
118
|
if (url)
|
|
@@ -132,7 +130,7 @@ function preset(options = {}) {
|
|
|
132
130
|
if (!theme[themeKey])
|
|
133
131
|
theme[themeKey] = {};
|
|
134
132
|
const obj = Object.fromEntries(
|
|
135
|
-
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) =>
|
|
133
|
+
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => f.provider.getFontName?.(f) ?? `"${f.name}"`)])
|
|
136
134
|
);
|
|
137
135
|
for (const key of Object.keys(obj)) {
|
|
138
136
|
if (typeof theme[themeKey][key] === "string")
|
|
@@ -145,5 +143,6 @@ function preset(options = {}) {
|
|
|
145
143
|
return preset2;
|
|
146
144
|
}
|
|
147
145
|
|
|
146
|
+
exports.createGoogleProvider = createGoogleCompatibleProvider;
|
|
148
147
|
exports["default"] = preset;
|
|
149
148
|
exports.normalizedFontMeta = normalizedFontMeta;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Preset } from '@unocss/core';
|
|
2
2
|
|
|
3
|
-
type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none';
|
|
3
|
+
type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none' | Provider;
|
|
4
4
|
interface WebFontMeta {
|
|
5
5
|
name: string;
|
|
6
6
|
weights?: (string | number)[];
|
|
@@ -11,6 +11,9 @@ interface WebFontMeta {
|
|
|
11
11
|
*/
|
|
12
12
|
provider?: WebFontsProviders;
|
|
13
13
|
}
|
|
14
|
+
interface ResolvedWebFontMeta extends Omit<WebFontMeta, 'provider'> {
|
|
15
|
+
provider: Provider;
|
|
16
|
+
}
|
|
14
17
|
interface WebFontsOptions {
|
|
15
18
|
/**
|
|
16
19
|
* Provider service of the web fonts
|
|
@@ -49,10 +52,12 @@ interface Provider {
|
|
|
49
52
|
name: WebFontsProviders;
|
|
50
53
|
getPreflight?(fonts: WebFontMeta[]): string;
|
|
51
54
|
getImportUrl?(fonts: WebFontMeta[]): string | undefined;
|
|
52
|
-
getFontName(font: WebFontMeta): string;
|
|
55
|
+
getFontName?(font: WebFontMeta): string;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
declare function
|
|
58
|
+
declare function createGoogleCompatibleProvider(name: WebFontsProviders, host: string): Provider;
|
|
59
|
+
|
|
60
|
+
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
56
61
|
declare function preset(options?: WebFontsOptions): Preset<any>;
|
|
57
62
|
|
|
58
|
-
export { Provider, WebFontMeta, WebFontsOptions, WebFontsProviders, preset as default, normalizedFontMeta };
|
|
63
|
+
export { Provider, ResolvedWebFontMeta, WebFontMeta, WebFontsOptions, WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, preset as default, normalizedFontMeta };
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { toArray } from '@unocss/core';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
function createGoogleProvider(name, host) {
|
|
3
|
+
function createGoogleCompatibleProvider(name, host) {
|
|
5
4
|
return {
|
|
6
5
|
name,
|
|
7
6
|
getImportUrl(fonts) {
|
|
8
|
-
const strings = fonts.
|
|
7
|
+
const strings = fonts.map((i) => {
|
|
9
8
|
let name2 = i.name.replace(/\s+/g, "+");
|
|
10
9
|
if (i.weights?.length) {
|
|
11
10
|
name2 += i.italic ? `:ital,wght@${i.weights.flatMap((i2) => [`0,${i2}`, `1,${i2}`]).sort().join(";")}` : `:wght@${i.weights.sort().join(";")}`;
|
|
@@ -13,21 +12,19 @@ function createGoogleProvider(name, host) {
|
|
|
13
12
|
return `family=${name2}`;
|
|
14
13
|
}).join("&");
|
|
15
14
|
return `${host}/css2?${strings}&display=swap`;
|
|
16
|
-
},
|
|
17
|
-
getFontName(font) {
|
|
18
|
-
return `"${font.name}"`;
|
|
19
15
|
}
|
|
20
16
|
};
|
|
21
17
|
}
|
|
18
|
+
const GoogleFontsProvider = createGoogleCompatibleProvider("google", "https://fonts.googleapis.com");
|
|
22
19
|
|
|
23
|
-
const BunnyFontsProvider =
|
|
20
|
+
const BunnyFontsProvider = createGoogleCompatibleProvider("bunny", "https://fonts.bunny.net");
|
|
24
21
|
|
|
25
22
|
const FontshareProvider = createFontshareProvider("fontshare", "https://api.fontshare.com");
|
|
26
23
|
function createFontshareProvider(name, host) {
|
|
27
24
|
return {
|
|
28
25
|
name,
|
|
29
26
|
getImportUrl(fonts) {
|
|
30
|
-
const strings = fonts.
|
|
27
|
+
const strings = fonts.map((f) => {
|
|
31
28
|
let name2 = f.name.replace(/\s+/g, "-").toLocaleLowerCase();
|
|
32
29
|
if (f.weights?.length)
|
|
33
30
|
name2 += `@${f.weights.flatMap((w) => f.italic ? Number(w) + 1 : w).sort().join()}`;
|
|
@@ -36,9 +33,6 @@ function createFontshareProvider(name, host) {
|
|
|
36
33
|
return `f[]=${name2}`;
|
|
37
34
|
}).join("&");
|
|
38
35
|
return `${host}/v2/css?${strings}&display=swap`;
|
|
39
|
-
},
|
|
40
|
-
getFontName(font) {
|
|
41
|
-
return `"${font.name}"`;
|
|
42
36
|
}
|
|
43
37
|
};
|
|
44
38
|
}
|
|
@@ -53,24 +47,29 @@ const NoneProvider = {
|
|
|
53
47
|
}
|
|
54
48
|
};
|
|
55
49
|
|
|
50
|
+
const builtinProviders = {
|
|
51
|
+
google: GoogleFontsProvider,
|
|
52
|
+
bunny: BunnyFontsProvider,
|
|
53
|
+
fontshare: FontshareProvider,
|
|
54
|
+
none: NoneProvider
|
|
55
|
+
};
|
|
56
|
+
function resolveProvider(provider) {
|
|
57
|
+
if (typeof provider === "string")
|
|
58
|
+
return builtinProviders[provider];
|
|
59
|
+
return provider;
|
|
60
|
+
}
|
|
56
61
|
function normalizedFontMeta(meta, defaultProvider) {
|
|
57
62
|
if (typeof meta !== "string") {
|
|
58
|
-
meta.provider = meta.provider
|
|
63
|
+
meta.provider = resolveProvider(meta.provider || defaultProvider);
|
|
59
64
|
return meta;
|
|
60
65
|
}
|
|
61
66
|
const [name, weights = ""] = meta.split(":");
|
|
62
67
|
return {
|
|
63
68
|
name,
|
|
64
69
|
weights: weights.split(/[,;]\s*/).filter(Boolean),
|
|
65
|
-
provider: defaultProvider
|
|
70
|
+
provider: resolveProvider(defaultProvider)
|
|
66
71
|
};
|
|
67
72
|
}
|
|
68
|
-
const providers = {
|
|
69
|
-
google: GoogleFontsProvider,
|
|
70
|
-
bunny: BunnyFontsProvider,
|
|
71
|
-
fontshare: FontshareProvider,
|
|
72
|
-
none: NoneProvider
|
|
73
|
-
};
|
|
74
73
|
function preset(options = {}) {
|
|
75
74
|
const {
|
|
76
75
|
provider: defaultProvider = "google",
|
|
@@ -101,16 +100,15 @@ function preset(options = {}) {
|
|
|
101
100
|
return `@import url('${url}')`;
|
|
102
101
|
}
|
|
103
102
|
}
|
|
103
|
+
const enabledProviders = new Set(fonts.map((i) => i.provider));
|
|
104
104
|
const preset2 = {
|
|
105
105
|
name: "@unocss/preset-web-fonts",
|
|
106
106
|
preflights: [
|
|
107
107
|
{
|
|
108
108
|
async getCSS() {
|
|
109
|
-
const names = new Set(fonts.map((i) => i.provider || defaultProvider));
|
|
110
109
|
const preflights = [];
|
|
111
|
-
for (const
|
|
112
|
-
const fontsForProvider = fonts.filter((i) => i.provider === name);
|
|
113
|
-
const provider = providers[name];
|
|
110
|
+
for (const provider of enabledProviders) {
|
|
111
|
+
const fontsForProvider = fonts.filter((i) => i.provider.name === provider.name);
|
|
114
112
|
if (provider.getImportUrl) {
|
|
115
113
|
const url = provider.getImportUrl(fontsForProvider);
|
|
116
114
|
if (url)
|
|
@@ -128,7 +126,7 @@ function preset(options = {}) {
|
|
|
128
126
|
if (!theme[themeKey])
|
|
129
127
|
theme[themeKey] = {};
|
|
130
128
|
const obj = Object.fromEntries(
|
|
131
|
-
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) =>
|
|
129
|
+
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => f.provider.getFontName?.(f) ?? `"${f.name}"`)])
|
|
132
130
|
);
|
|
133
131
|
for (const key of Object.keys(obj)) {
|
|
134
132
|
if (typeof theme[themeKey][key] === "string")
|
|
@@ -141,4 +139,4 @@ function preset(options = {}) {
|
|
|
141
139
|
return preset2;
|
|
142
140
|
}
|
|
143
141
|
|
|
144
|
-
export { preset as default, normalizedFontMeta };
|
|
142
|
+
export { createGoogleCompatibleProvider as createGoogleProvider, preset as default, normalizedFontMeta };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/preset-web-fonts",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.8",
|
|
4
4
|
"description": "Web Fonts support for Uno CSS",
|
|
5
5
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"ofetch": "^1.0.1",
|
|
41
|
-
"@unocss/core": "0.51.
|
|
41
|
+
"@unocss/core": "0.51.8"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "unbuild",
|