@unocss/preset-web-fonts 0.56.4 → 0.57.0
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 +69 -66
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.mjs +71 -67
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -105,77 +105,80 @@ function normalizedFontMeta(meta, defaultProvider) {
|
|
|
105
105
|
provider: resolveProvider(defaultProvider)
|
|
106
106
|
};
|
|
107
107
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
108
|
+
function createWebFontPreset(fetcher) {
|
|
109
|
+
return (options = {}) => {
|
|
110
|
+
const {
|
|
111
|
+
provider: defaultProvider = "google",
|
|
112
|
+
extendTheme = true,
|
|
113
|
+
inlineImports = true,
|
|
114
|
+
themeKey = "fontFamily",
|
|
115
|
+
customFetch = fetcher
|
|
116
|
+
} = options;
|
|
117
|
+
const fontObject = Object.fromEntries(
|
|
118
|
+
Object.entries(options.fonts || {}).map(([name, meta]) => [name, core.toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))])
|
|
119
|
+
);
|
|
120
|
+
const fonts = Object.values(fontObject).flatMap((i) => i);
|
|
121
|
+
const importCache = {};
|
|
122
|
+
async function importUrl(url) {
|
|
123
|
+
if (inlineImports) {
|
|
124
|
+
if (!importCache[url]) {
|
|
125
|
+
importCache[url] = customFetch(url).catch((e) => {
|
|
126
|
+
console.error("Failed to fetch web fonts");
|
|
127
|
+
console.error(e);
|
|
128
|
+
if (typeof process !== "undefined" && process.env.CI)
|
|
129
|
+
throw e;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return await importCache[url];
|
|
133
|
+
} else {
|
|
134
|
+
return `@import url('${url}');`;
|
|
132
135
|
}
|
|
133
|
-
return await importCache[url];
|
|
134
|
-
} else {
|
|
135
|
-
return `@import url('${url}');`;
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
137
|
+
const enabledProviders = new Set(fonts.map((i) => i.provider));
|
|
138
|
+
const preset = {
|
|
139
|
+
name: "@unocss/preset-web-fonts",
|
|
140
|
+
preflights: [
|
|
141
|
+
{
|
|
142
|
+
async getCSS() {
|
|
143
|
+
const preflights = [];
|
|
144
|
+
for (const provider of enabledProviders) {
|
|
145
|
+
const fontsForProvider = fonts.filter((i) => i.provider.name === provider.name);
|
|
146
|
+
if (provider.getImportUrl) {
|
|
147
|
+
const url = provider.getImportUrl(fontsForProvider);
|
|
148
|
+
if (url)
|
|
149
|
+
preflights.push(await importUrl(url));
|
|
150
|
+
}
|
|
151
|
+
preflights.push(provider.getPreflight?.(fontsForProvider));
|
|
151
152
|
}
|
|
152
|
-
preflights.
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
]
|
|
159
|
-
};
|
|
160
|
-
if (extendTheme) {
|
|
161
|
-
preset.extendTheme = (theme) => {
|
|
162
|
-
if (!theme[themeKey])
|
|
163
|
-
theme[themeKey] = {};
|
|
164
|
-
const obj = Object.fromEntries(
|
|
165
|
-
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => f.provider.getFontName?.(f) ?? `"${f.name}"`)])
|
|
166
|
-
);
|
|
167
|
-
for (const key of Object.keys(obj)) {
|
|
168
|
-
if (typeof theme[themeKey][key] === "string")
|
|
169
|
-
theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
|
|
170
|
-
else
|
|
171
|
-
theme[themeKey][key] = obj[key].join(",");
|
|
172
|
-
}
|
|
153
|
+
return preflights.filter(Boolean).join("\n");
|
|
154
|
+
},
|
|
155
|
+
layer: inlineImports ? void 0 : LAYER_IMPORTS
|
|
156
|
+
}
|
|
157
|
+
]
|
|
173
158
|
};
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
159
|
+
if (extendTheme) {
|
|
160
|
+
preset.extendTheme = (theme) => {
|
|
161
|
+
if (!theme[themeKey])
|
|
162
|
+
theme[themeKey] = {};
|
|
163
|
+
const obj = Object.fromEntries(
|
|
164
|
+
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => f.provider.getFontName?.(f) ?? `"${f.name}"`)])
|
|
165
|
+
);
|
|
166
|
+
for (const key of Object.keys(obj)) {
|
|
167
|
+
if (typeof theme[themeKey][key] === "string")
|
|
168
|
+
theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
|
|
169
|
+
else
|
|
170
|
+
theme[themeKey][key] = obj[key].join(",");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
return preset;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const userAgentWoff2 = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
|
|
179
|
+
const defaultFetch = async (url) => (await import('ofetch')).$fetch(url, { headers: { "User-Agent": userAgentWoff2 }, retry: 3 });
|
|
180
|
+
const presetWebFonts = core.definePreset(createWebFontPreset(defaultFetch));
|
|
177
181
|
|
|
178
182
|
exports.createGoogleProvider = createGoogleCompatibleProvider;
|
|
179
183
|
exports.default = presetWebFonts;
|
|
180
184
|
exports.normalizedFontMeta = normalizedFontMeta;
|
|
181
|
-
exports.presetWebFonts = presetWebFonts;
|
package/dist/index.d.cts
CHANGED
|
@@ -55,9 +55,10 @@ interface Provider {
|
|
|
55
55
|
getFontName?(font: WebFontMeta): string;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
59
|
+
|
|
58
60
|
declare function createGoogleCompatibleProvider(name: WebFontsProviders, host: string): Provider;
|
|
59
61
|
|
|
60
|
-
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
61
62
|
declare const presetWebFonts: _unocss_core.PresetFactory<any, WebFontsOptions>;
|
|
62
63
|
|
|
63
|
-
export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta
|
|
64
|
+
export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta };
|
package/dist/index.d.mts
CHANGED
|
@@ -55,9 +55,10 @@ interface Provider {
|
|
|
55
55
|
getFontName?(font: WebFontMeta): string;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
59
|
+
|
|
58
60
|
declare function createGoogleCompatibleProvider(name: WebFontsProviders, host: string): Provider;
|
|
59
61
|
|
|
60
|
-
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
61
62
|
declare const presetWebFonts: _unocss_core.PresetFactory<any, WebFontsOptions>;
|
|
62
63
|
|
|
63
|
-
export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta
|
|
64
|
+
export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta };
|
package/dist/index.d.ts
CHANGED
|
@@ -55,9 +55,10 @@ interface Provider {
|
|
|
55
55
|
getFontName?(font: WebFontMeta): string;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
59
|
+
|
|
58
60
|
declare function createGoogleCompatibleProvider(name: WebFontsProviders, host: string): Provider;
|
|
59
61
|
|
|
60
|
-
declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
|
|
61
62
|
declare const presetWebFonts: _unocss_core.PresetFactory<any, WebFontsOptions>;
|
|
62
63
|
|
|
63
|
-
export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta
|
|
64
|
+
export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toArray, definePreset } from '@unocss/core';
|
|
2
2
|
|
|
3
3
|
const LAYER_IMPORTS = "imports";
|
|
4
4
|
|
|
@@ -101,74 +101,78 @@ function normalizedFontMeta(meta, defaultProvider) {
|
|
|
101
101
|
provider: resolveProvider(defaultProvider)
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
104
|
+
function createWebFontPreset(fetcher) {
|
|
105
|
+
return (options = {}) => {
|
|
106
|
+
const {
|
|
107
|
+
provider: defaultProvider = "google",
|
|
108
|
+
extendTheme = true,
|
|
109
|
+
inlineImports = true,
|
|
110
|
+
themeKey = "fontFamily",
|
|
111
|
+
customFetch = fetcher
|
|
112
|
+
} = options;
|
|
113
|
+
const fontObject = Object.fromEntries(
|
|
114
|
+
Object.entries(options.fonts || {}).map(([name, meta]) => [name, toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))])
|
|
115
|
+
);
|
|
116
|
+
const fonts = Object.values(fontObject).flatMap((i) => i);
|
|
117
|
+
const importCache = {};
|
|
118
|
+
async function importUrl(url) {
|
|
119
|
+
if (inlineImports) {
|
|
120
|
+
if (!importCache[url]) {
|
|
121
|
+
importCache[url] = customFetch(url).catch((e) => {
|
|
122
|
+
console.error("Failed to fetch web fonts");
|
|
123
|
+
console.error(e);
|
|
124
|
+
if (typeof process !== "undefined" && process.env.CI)
|
|
125
|
+
throw e;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return await importCache[url];
|
|
129
|
+
} else {
|
|
130
|
+
return `@import url('${url}');`;
|
|
128
131
|
}
|
|
129
|
-
return await importCache[url];
|
|
130
|
-
} else {
|
|
131
|
-
return `@import url('${url}');`;
|
|
132
132
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
133
|
+
const enabledProviders = new Set(fonts.map((i) => i.provider));
|
|
134
|
+
const preset = {
|
|
135
|
+
name: "@unocss/preset-web-fonts",
|
|
136
|
+
preflights: [
|
|
137
|
+
{
|
|
138
|
+
async getCSS() {
|
|
139
|
+
const preflights = [];
|
|
140
|
+
for (const provider of enabledProviders) {
|
|
141
|
+
const fontsForProvider = fonts.filter((i) => i.provider.name === provider.name);
|
|
142
|
+
if (provider.getImportUrl) {
|
|
143
|
+
const url = provider.getImportUrl(fontsForProvider);
|
|
144
|
+
if (url)
|
|
145
|
+
preflights.push(await importUrl(url));
|
|
146
|
+
}
|
|
147
|
+
preflights.push(provider.getPreflight?.(fontsForProvider));
|
|
147
148
|
}
|
|
148
|
-
preflights.
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
]
|
|
155
|
-
};
|
|
156
|
-
if (extendTheme) {
|
|
157
|
-
preset.extendTheme = (theme) => {
|
|
158
|
-
if (!theme[themeKey])
|
|
159
|
-
theme[themeKey] = {};
|
|
160
|
-
const obj = Object.fromEntries(
|
|
161
|
-
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => f.provider.getFontName?.(f) ?? `"${f.name}"`)])
|
|
162
|
-
);
|
|
163
|
-
for (const key of Object.keys(obj)) {
|
|
164
|
-
if (typeof theme[themeKey][key] === "string")
|
|
165
|
-
theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
|
|
166
|
-
else
|
|
167
|
-
theme[themeKey][key] = obj[key].join(",");
|
|
168
|
-
}
|
|
149
|
+
return preflights.filter(Boolean).join("\n");
|
|
150
|
+
},
|
|
151
|
+
layer: inlineImports ? void 0 : LAYER_IMPORTS
|
|
152
|
+
}
|
|
153
|
+
]
|
|
169
154
|
};
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
155
|
+
if (extendTheme) {
|
|
156
|
+
preset.extendTheme = (theme) => {
|
|
157
|
+
if (!theme[themeKey])
|
|
158
|
+
theme[themeKey] = {};
|
|
159
|
+
const obj = Object.fromEntries(
|
|
160
|
+
Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => f.provider.getFontName?.(f) ?? `"${f.name}"`)])
|
|
161
|
+
);
|
|
162
|
+
for (const key of Object.keys(obj)) {
|
|
163
|
+
if (typeof theme[themeKey][key] === "string")
|
|
164
|
+
theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
|
|
165
|
+
else
|
|
166
|
+
theme[themeKey][key] = obj[key].join(",");
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return preset;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const userAgentWoff2 = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
|
|
175
|
+
const defaultFetch = async (url) => (await import('ofetch')).$fetch(url, { headers: { "User-Agent": userAgentWoff2 }, retry: 3 });
|
|
176
|
+
const presetWebFonts = definePreset(createWebFontPreset(defaultFetch));
|
|
173
177
|
|
|
174
|
-
export { createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta
|
|
178
|
+
export { createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/preset-web-fonts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.57.0",
|
|
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.3.3",
|
|
41
|
-
"@unocss/core": "0.
|
|
41
|
+
"@unocss/core": "0.57.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "unbuild",
|