@unocss/preset-web-fonts 66.6.8 → 66.7.0-beta.1

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.d.mts CHANGED
@@ -1,18 +1,19 @@
1
- import { a as WebFontProcessor, i as WebFontMeta, n as Provider, o as WebFontsOptions, r as ResolvedWebFontMeta, s as WebFontsProviders, t as Axes } from "./types-o6CvBLpT.mjs";
2
- import * as _$_unocss_core0 from "@unocss/core";
3
-
1
+ import { a as WebFontProcessor, i as WebFontMeta, n as Provider, o as WebFontsOptions, r as ResolvedWebFontMeta, s as WebFontsProviders, t as Axes } from "./types-Bbb4MXF_.mjs";
4
2
  //#region src/preset.d.ts
5
3
  declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
6
4
  //#endregion
7
5
  //#region src/providers/google.d.ts
8
6
  declare function createGoogleCompatibleProvider(name: WebFontsProviders, host: string): Provider;
9
7
  //#endregion
8
+ //#region src/providers/zeoseven.d.ts
9
+ declare const ZeoSevenFontsProvider: Provider;
10
+ //#endregion
10
11
  //#region src/index.d.ts
11
12
  /**
12
13
  * Preset for using web fonts by provide just the names.
13
14
  *
14
15
  * @see https://unocss.dev/presets/web-fonts
15
16
  */
16
- declare const presetWebFonts: _$_unocss_core0.PresetFactory<any, WebFontsOptions>;
17
+ declare const presetWebFonts: import("@unocss/core").PresetFactory<any, WebFontsOptions>;
17
18
  //#endregion
18
- export { Axes, Provider, ResolvedWebFontMeta, WebFontMeta, WebFontProcessor, WebFontsOptions, WebFontsProviders, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, presetWebFonts, normalizedFontMeta };
19
+ export { Axes, Provider, ResolvedWebFontMeta, WebFontMeta, WebFontProcessor, WebFontsOptions, WebFontsProviders, ZeoSevenFontsProvider, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, presetWebFonts, normalizedFontMeta };
package/dist/index.mjs CHANGED
@@ -201,6 +201,52 @@ function getVariableWght(axes) {
201
201
  return `${axes.min} ${axes.max}`;
202
202
  }
203
203
  //#endregion
204
+ //#region src/providers/none.ts
205
+ const NoneProvider = {
206
+ name: "none",
207
+ getPreflight() {
208
+ return "";
209
+ },
210
+ getFontName(font) {
211
+ return font.name;
212
+ }
213
+ };
214
+ //#endregion
215
+ //#region src/providers/zeoseven.ts
216
+ function parseFontName(name) {
217
+ const atIndex = name.lastIndexOf("@");
218
+ if (atIndex === -1) throw new Error(`[unocss] ZeoSeven provider requires font ID in format "FontFamily@id", e.g. "LXGW WenKai@292". Got: "${name}"`);
219
+ return {
220
+ family: name.slice(0, atIndex).trim(),
221
+ id: name.slice(atIndex + 1).trim()
222
+ };
223
+ }
224
+ function createZeoSevenProvider(name, host) {
225
+ return {
226
+ name,
227
+ getFontName(font) {
228
+ const { family } = parseFontName(font.name);
229
+ return `"${family}"`;
230
+ },
231
+ async getPreflight(fonts, fetcher) {
232
+ const cssParts = [];
233
+ for (const font of fonts) {
234
+ const { id } = parseFontName(font.name);
235
+ const baseUrl = `${host}/${id}/main/`;
236
+ const url = `${baseUrl}result.css`;
237
+ try {
238
+ const processed = (await fetcher(url)).replace(/url\("\.\/([^"]+)"\)/g, `url("${baseUrl}$1")`);
239
+ cssParts.push(processed);
240
+ } catch {
241
+ throw new Error(`[unocss] Failed to fetch ZeoSeven font CSS for ID "${id}"`);
242
+ }
243
+ }
244
+ return cssParts.join("\n");
245
+ }
246
+ };
247
+ }
248
+ const ZeoSevenFontsProvider = createZeoSevenProvider("zeoseven", "https://fontsapi.zeoseven.com");
249
+ //#endregion
204
250
  //#region src/preset.ts
205
251
  const builtinProviders = {
206
252
  google: GoogleFontsProvider,
@@ -208,15 +254,8 @@ const builtinProviders = {
208
254
  fontshare: FontshareProvider,
209
255
  fontsource: FontSourceProvider,
210
256
  coollabs: CoolLabsFontsProvider,
211
- none: {
212
- name: "none",
213
- getPreflight() {
214
- return "";
215
- },
216
- getFontName(font) {
217
- return font.name;
218
- }
219
- }
257
+ none: NoneProvider,
258
+ zeoseven: ZeoSevenFontsProvider
220
259
  };
221
260
  function resolveProvider(provider) {
222
261
  if (typeof provider === "string") return builtinProviders[provider];
@@ -332,4 +371,4 @@ const defaultFetch = async (url) => (await import("ofetch")).$fetch(url, {
332
371
  */
333
372
  const presetWebFonts = definePreset(createWebFontPreset(defaultFetch));
334
373
  //#endregion
335
- export { createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, presetWebFonts, normalizedFontMeta };
374
+ export { ZeoSevenFontsProvider, createGoogleCompatibleProvider as createGoogleProvider, presetWebFonts as default, presetWebFonts, normalizedFontMeta };
package/dist/local.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { a as WebFontProcessor } from "./types-o6CvBLpT.mjs";
1
+ import { a as WebFontProcessor } from "./types-Bbb4MXF_.mjs";
2
+ import { Buffer } from "node:buffer";
2
3
  import { fetch } from "ofetch";
3
4
 
4
5
  //#region src/local.d.ts
@@ -31,6 +32,12 @@ interface LocalFontProcessorOptions {
31
32
  * Custom fetch function to provide the font data.
32
33
  */
33
34
  fetch?: typeof fetch;
35
+ /**
36
+ * Callback invoked when a font file is downloaded during build.
37
+ * Receives the filename and file buffer, useful for emitting fonts
38
+ * directly to the build output instead of relying on public dir copy.
39
+ */
40
+ onDownload?: (filename: string, buf: Buffer) => void | Promise<void>;
34
41
  }
35
42
  declare function createLocalFontProcessor(options?: LocalFontProcessorOptions): WebFontProcessor;
36
43
  //#endregion
package/dist/local.mjs CHANGED
@@ -1,4 +1,3 @@
1
- import "@unocss/core";
2
1
  import { Buffer } from "node:buffer";
3
2
  import { createHash } from "node:crypto";
4
3
  import fs from "node:fs";
@@ -6,29 +5,9 @@ import fsp from "node:fs/promises";
6
5
  import { join, resolve } from "node:path";
7
6
  import process from "node:process";
8
7
  import { fetch } from "ofetch";
9
- //#region ../../virtual-shared/integration/src/utils.ts
10
- function replaceAsync(string, searchValue, replacer) {
11
- try {
12
- if (typeof replacer === "function") {
13
- const values = [];
14
- String.prototype.replace.call(string, searchValue, (...args) => {
15
- values.push(replacer(...args));
16
- return "";
17
- });
18
- return Promise.all(values).then((resolvedValues) => {
19
- return String.prototype.replace.call(string, searchValue, () => {
20
- return resolvedValues.shift() || "";
21
- });
22
- });
23
- } else return Promise.resolve(String.prototype.replace.call(string, searchValue, replacer));
24
- } catch (error) {
25
- return Promise.reject(error);
26
- }
27
- }
28
- //#endregion
29
8
  //#region src/local.ts
30
9
  const fontUrlRegex = /[-\w@:%+.~#?&/=]+\.(?:woff2?|eot|ttf|otf|svg)/gi;
31
- const urlProtocolRegex = /^[\s\w\0+.-]{2,}:([/\\]{1,2})/;
10
+ const urlProtocolRegex = /^[\s\w\0+.-]{2,}:[/\\]{1,2}/;
32
11
  function createLocalFontProcessor(options) {
33
12
  const cwd = options?.cwd || process.cwd();
34
13
  const cacheDir = resolve(cwd, options?.cacheDir || "node_modules/.cache/unocss/fonts");
@@ -36,8 +15,11 @@ function createLocalFontProcessor(options) {
36
15
  const fontServeBaseUrl = options?.fontServeBaseUrl || "/assets/fonts";
37
16
  async function _downloadFont(url, assetPath) {
38
17
  const response = await (options?.fetch ?? fetch)(url).then((r) => r.arrayBuffer());
18
+ const buf = Buffer.from(response);
19
+ const filename = assetPath.split(/[\\/]/).pop();
20
+ await options?.onDownload?.(filename, buf);
39
21
  await fsp.mkdir(fontAssetsDir, { recursive: true });
40
- await fsp.writeFile(assetPath, Buffer.from(response));
22
+ await fsp.writeFile(assetPath, buf);
41
23
  }
42
24
  const cache = /* @__PURE__ */ new Map();
43
25
  function downloadFont(url, assetPath) {
@@ -54,7 +36,9 @@ function createLocalFontProcessor(options) {
54
36
  return css;
55
37
  },
56
38
  async transformCSS(css) {
57
- return await replaceAsync(css, fontUrlRegex, async (url) => {
39
+ const matches = css.match(fontUrlRegex) || [];
40
+ const replacements = /* @__PURE__ */ new Map();
41
+ await Promise.all(Array.from(new Set(matches)).map(async (url) => {
58
42
  const hash = getHash(url);
59
43
  const ext = url.split(".").pop();
60
44
  let name = "";
@@ -63,8 +47,9 @@ function createLocalFontProcessor(options) {
63
47
  const filename = `${[name, hash].filter(Boolean).join("-")}.${ext}`;
64
48
  const assetPath = join(fontAssetsDir, filename);
65
49
  if (!fs.existsSync(assetPath)) await downloadFont(hasProtocol(url) ? url : withProtocol(url), assetPath);
66
- return `${fontServeBaseUrl}/${filename}`;
67
- });
50
+ replacements.set(url, `${fontServeBaseUrl}/${filename}`);
51
+ }));
52
+ return css.replace(fontUrlRegex, (match) => replacements.get(match) || match);
68
53
  }
69
54
  };
70
55
  }
@@ -1,7 +1,7 @@
1
1
  import { Arrayable, Awaitable } from "@unocss/core";
2
2
 
3
3
  //#region src/types.d.ts
4
- type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'fontsource' | 'coollabs' | 'none' | Provider;
4
+ type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'fontsource' | 'coollabs' | 'zeoseven' | 'none' | Provider;
5
5
  interface WebFontMeta {
6
6
  /**
7
7
  * The name of the font family
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/preset-web-fonts",
3
3
  "type": "module",
4
- "version": "66.6.8",
4
+ "version": "66.7.0-beta.1",
5
5
  "description": "Web Fonts support for Uno CSS",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  ],
43
43
  "dependencies": {
44
44
  "ofetch": "^1.5.1",
45
- "@unocss/core": "66.6.8"
45
+ "@unocss/core": "66.7.0-beta.1"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsdown --config-loader unrun",