@unocss/preset-web-fonts 0.58.9 → 0.59.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/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/preset-web-fonts",
3
- "version": "0.58.9",
3
+ "type": "module",
4
+ "version": "0.59.0",
4
5
  "description": "Web Fonts support for Uno CSS",
5
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
7
  "license": "MIT",
@@ -24,12 +25,11 @@
24
25
  "sideEffects": false,
25
26
  "exports": {
26
27
  ".": {
27
- "types": "./dist/index.d.ts",
28
- "import": "./dist/index.mjs",
29
- "require": "./dist/index.cjs"
28
+ "types": "./dist/index.d.mts",
29
+ "default": "./dist/index.mjs"
30
30
  }
31
31
  },
32
- "main": "dist/index.cjs",
32
+ "main": "dist/index.mjs",
33
33
  "module": "dist/index.mjs",
34
34
  "types": "dist/index.d.ts",
35
35
  "files": [
@@ -38,7 +38,7 @@
38
38
  ],
39
39
  "dependencies": {
40
40
  "ofetch": "^1.3.4",
41
- "@unocss/core": "0.58.9"
41
+ "@unocss/core": "0.59.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "unbuild",
package/dist/index.cjs DELETED
@@ -1,184 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const core = require('@unocss/core');
6
-
7
- const LAYER_IMPORTS = "imports";
8
-
9
- function createBunnyFontsProvider(name, host) {
10
- return {
11
- name,
12
- getImportUrl(fonts) {
13
- const fontFamilies = fonts.map((font) => {
14
- const { name: name2, weights, italic } = font;
15
- const formattedName = name2.toLowerCase().replace(/\s/g, "-");
16
- if (!weights?.length)
17
- return `${formattedName}${italic ? ":i" : ""}`;
18
- let weightsAsString = weights.map((weight) => weight.toString());
19
- const weightsHaveItalic = weightsAsString.some((weight) => weight.endsWith("i"));
20
- if (!weightsHaveItalic && italic)
21
- weightsAsString = weightsAsString.map((weight) => weight += "i");
22
- return `${formattedName}:${weightsAsString.join(",")}`;
23
- });
24
- return `${host}/css?family=${fontFamilies.join("|")}&display=swap`;
25
- }
26
- };
27
- }
28
- const BunnyFontsProvider = createBunnyFontsProvider(
29
- "bunny",
30
- "https://fonts.bunny.net"
31
- );
32
-
33
- function createGoogleCompatibleProvider(name, host) {
34
- return {
35
- name,
36
- getImportUrl(fonts) {
37
- const sort = (weights) => {
38
- const firstW = weights.map((w) => w[0]);
39
- const lastW = weights.map((w) => w[1]);
40
- return `${firstW.join(";")};${lastW.join(";")}`;
41
- };
42
- const strings = fonts.map((i) => {
43
- let name2 = i.name.replace(/\s+/g, "+");
44
- if (i.weights?.length) {
45
- name2 += i.italic ? `:ital,wght@${sort(i.weights.map((w) => [`0,${w}`, `1,${w}`]))}` : `:wght@${i.weights.join(";")}`;
46
- }
47
- return `family=${name2}`;
48
- }).join("&");
49
- return `${host}/css2?${strings}&display=swap`;
50
- }
51
- };
52
- }
53
- const GoogleFontsProvider = createGoogleCompatibleProvider("google", "https://fonts.googleapis.com");
54
-
55
- const FontshareProvider = createFontshareProvider("fontshare", "https://api.fontshare.com");
56
- function createFontshareProvider(name, host) {
57
- return {
58
- name,
59
- getImportUrl(fonts) {
60
- const strings = fonts.map((f) => {
61
- let name2 = f.name.replace(/\s+/g, "-").toLocaleLowerCase();
62
- if (f.weights?.length)
63
- name2 += `@${f.weights.flatMap((w) => f.italic ? Number(w) + 1 : w).sort().join()}`;
64
- else
65
- name2 += `@${f.italic ? 2 : 1}`;
66
- return `f[]=${name2}`;
67
- }).join("&");
68
- return `${host}/v2/css?${strings}&display=swap`;
69
- }
70
- };
71
- }
72
-
73
- const NoneProvider = {
74
- name: "none",
75
- getPreflight() {
76
- return "";
77
- },
78
- getFontName(font) {
79
- return font.name;
80
- }
81
- };
82
-
83
- const builtinProviders = {
84
- google: GoogleFontsProvider,
85
- bunny: BunnyFontsProvider,
86
- fontshare: FontshareProvider,
87
- none: NoneProvider
88
- };
89
- function resolveProvider(provider) {
90
- if (typeof provider === "string")
91
- return builtinProviders[provider];
92
- return provider;
93
- }
94
- function normalizedFontMeta(meta, defaultProvider) {
95
- if (typeof meta !== "string") {
96
- meta.provider = resolveProvider(meta.provider || defaultProvider);
97
- if (meta.weights)
98
- meta.weights = [...new Set(meta.weights.sort((a, b) => a.toString().localeCompare(b.toString(), "en", { numeric: true })))];
99
- return meta;
100
- }
101
- const [name, weights = ""] = meta.split(":");
102
- return {
103
- name,
104
- weights: [...new Set(weights.split(/[,;]\s*/).filter(Boolean).sort((a, b) => a.localeCompare(b, "en", { numeric: true })))],
105
- provider: resolveProvider(defaultProvider)
106
- };
107
- }
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}');`;
135
- }
136
- }
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));
152
- }
153
- return preflights.filter(Boolean).join("\n");
154
- },
155
- layer: inlineImports ? void 0 : LAYER_IMPORTS
156
- }
157
- ]
158
- };
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));
181
-
182
- exports.createGoogleProvider = createGoogleCompatibleProvider;
183
- exports.default = presetWebFonts;
184
- exports.normalizedFontMeta = normalizedFontMeta;
package/dist/index.d.cts DELETED
@@ -1,69 +0,0 @@
1
- import * as _unocss_core from '@unocss/core';
2
-
3
- type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none' | Provider;
4
- interface WebFontMeta {
5
- name: string;
6
- weights?: (string | number)[];
7
- italic?: boolean;
8
- /**
9
- * Override the provider
10
- * @default <matches root config>
11
- */
12
- provider?: WebFontsProviders;
13
- }
14
- interface ResolvedWebFontMeta extends Omit<WebFontMeta, 'provider'> {
15
- provider: Provider;
16
- }
17
- interface WebFontsOptions {
18
- /**
19
- * Provider service of the web fonts
20
- * @default 'google'
21
- */
22
- provider?: WebFontsProviders;
23
- /**
24
- * The fonts
25
- */
26
- fonts?: Record<string, WebFontMeta | string | (WebFontMeta | string)[]>;
27
- /**
28
- * Extend fonts to the theme object
29
- * @default true
30
- */
31
- extendTheme?: boolean;
32
- /**
33
- * Key for the theme object
34
- *
35
- * @default 'fontFamily'
36
- */
37
- themeKey?: string;
38
- /**
39
- * Inline CSS @import()
40
- *
41
- * @default true
42
- */
43
- inlineImports?: boolean;
44
- /**
45
- * Custom fetch function
46
- *
47
- * @default undefined
48
- */
49
- customFetch?: (url: string) => Promise<any>;
50
- }
51
- interface Provider {
52
- name: WebFontsProviders;
53
- getPreflight?: (fonts: WebFontMeta[]) => string;
54
- getImportUrl?: (fonts: WebFontMeta[]) => string | undefined;
55
- getFontName?: (font: WebFontMeta) => string;
56
- }
57
-
58
- declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
59
-
60
- declare function createGoogleCompatibleProvider(name: WebFontsProviders, host: string): Provider;
61
-
62
- /**
63
- * Preset for using web fonts by provide just the names.
64
- *
65
- * @see https://unocss.dev/presets/web-fonts
66
- */
67
- declare const presetWebFonts: _unocss_core.PresetFactory<any, WebFontsOptions>;
68
-
69
- export { type Provider, type ResolvedWebFontMeta, type WebFontMeta, type WebFontsOptions, type WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, normalizedFontMeta };