@unocss/preset-web-fonts 0.21.2 → 0.22.3

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 CHANGED
@@ -6,7 +6,7 @@ const core = require('@unocss/core');
6
6
 
7
7
  const GoogleFontsProvider = {
8
8
  name: "google",
9
- getPreflight(fonts) {
9
+ getImportUrl(fonts) {
10
10
  const strings = fonts.filter((i) => i.provider === "google").map((i) => {
11
11
  let name = i.name.replace(/\s+/g, "+");
12
12
  if (i.weights?.length) {
@@ -14,7 +14,7 @@ const GoogleFontsProvider = {
14
14
  }
15
15
  return `family=${name}`;
16
16
  }).join("&");
17
- return `@import url('https://fonts.googleapis.com/css2?${strings}&display=swap');`;
17
+ return `https://fonts.googleapis.com/css2?${strings}&display=swap`;
18
18
  },
19
19
  getFontName(font) {
20
20
  return `"${font.name}"`;
@@ -52,10 +52,31 @@ const preset = (options = {}) => {
52
52
  const {
53
53
  provider: defaultProvider = "google",
54
54
  extendTheme = true,
55
+ inlineImports = true,
55
56
  themeKey = "fontFamily"
56
57
  } = options;
57
58
  const fontObject = Object.fromEntries(Object.entries(options.fonts || {}).map(([name, meta]) => [name, core.toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))]));
58
59
  const fonts = Object.values(fontObject).flatMap((i) => i);
60
+ const importCache = {};
61
+ async function importUrl(url) {
62
+ if (inlineImports) {
63
+ if (!importCache[url]) {
64
+ try {
65
+ const { default: axios } = await import('axios');
66
+ const { data } = await axios.get(url, { headers: {} });
67
+ importCache[url] = data;
68
+ } catch (e) {
69
+ console.error("Failed to fetch web fonts");
70
+ console.error(e);
71
+ if (typeof process !== "undefined" && process.env.CI)
72
+ throw e;
73
+ }
74
+ }
75
+ return importCache[url];
76
+ } else {
77
+ return `@import url('${url}')`;
78
+ }
79
+ }
59
80
  const preset2 = {
60
81
  name: "@unocss/preset-web-fonts",
61
82
  layers: {
@@ -63,12 +84,18 @@ const preset = (options = {}) => {
63
84
  },
64
85
  preflights: [
65
86
  {
66
- getCSS() {
87
+ async getCSS() {
67
88
  const names = new Set(fonts.map((i) => i.provider || defaultProvider));
68
89
  const preflights = [];
69
90
  for (const name of names) {
91
+ const fontsForProvider = fonts.filter((i) => i.provider === name);
70
92
  const provider = providers[name];
71
- preflights.push(provider.getPreflight(fonts.filter((i) => i.provider === name)));
93
+ if (provider.getImportUrl) {
94
+ const url = provider.getImportUrl(fontsForProvider);
95
+ if (url)
96
+ preflights.push(await importUrl(url));
97
+ }
98
+ preflights.push(provider.getPreflight?.(fontsForProvider));
72
99
  }
73
100
  return preflights.filter(Boolean).join("\n");
74
101
  },
package/dist/index.d.ts CHANGED
@@ -32,10 +32,17 @@ interface WebFontsOptions {
32
32
  * @default 'fontFamily'
33
33
  */
34
34
  themeKey?: string;
35
+ /**
36
+ * Inline CSS @import()
37
+ *
38
+ * @default true
39
+ */
40
+ inlineImports?: boolean;
35
41
  }
36
42
  interface Provider {
37
43
  name: WebFontsProviders;
38
- getPreflight(fonts: WebFontMeta[]): string;
44
+ getPreflight?(fonts: WebFontMeta[]): string;
45
+ getImportUrl?(fonts: WebFontMeta[]): string | undefined;
39
46
  getFontName(font: WebFontMeta): string;
40
47
  }
41
48
 
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { toArray } from '@unocss/core';
2
2
 
3
3
  const GoogleFontsProvider = {
4
4
  name: "google",
5
- getPreflight(fonts) {
5
+ getImportUrl(fonts) {
6
6
  const strings = fonts.filter((i) => i.provider === "google").map((i) => {
7
7
  let name = i.name.replace(/\s+/g, "+");
8
8
  if (i.weights?.length) {
@@ -10,7 +10,7 @@ const GoogleFontsProvider = {
10
10
  }
11
11
  return `family=${name}`;
12
12
  }).join("&");
13
- return `@import url('https://fonts.googleapis.com/css2?${strings}&display=swap');`;
13
+ return `https://fonts.googleapis.com/css2?${strings}&display=swap`;
14
14
  },
15
15
  getFontName(font) {
16
16
  return `"${font.name}"`;
@@ -48,10 +48,31 @@ const preset = (options = {}) => {
48
48
  const {
49
49
  provider: defaultProvider = "google",
50
50
  extendTheme = true,
51
+ inlineImports = true,
51
52
  themeKey = "fontFamily"
52
53
  } = options;
53
54
  const fontObject = Object.fromEntries(Object.entries(options.fonts || {}).map(([name, meta]) => [name, toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))]));
54
55
  const fonts = Object.values(fontObject).flatMap((i) => i);
56
+ const importCache = {};
57
+ async function importUrl(url) {
58
+ if (inlineImports) {
59
+ if (!importCache[url]) {
60
+ try {
61
+ const { default: axios } = await import('axios');
62
+ const { data } = await axios.get(url, { headers: {} });
63
+ importCache[url] = data;
64
+ } catch (e) {
65
+ console.error("Failed to fetch web fonts");
66
+ console.error(e);
67
+ if (typeof process !== "undefined" && process.env.CI)
68
+ throw e;
69
+ }
70
+ }
71
+ return importCache[url];
72
+ } else {
73
+ return `@import url('${url}')`;
74
+ }
75
+ }
55
76
  const preset2 = {
56
77
  name: "@unocss/preset-web-fonts",
57
78
  layers: {
@@ -59,12 +80,18 @@ const preset = (options = {}) => {
59
80
  },
60
81
  preflights: [
61
82
  {
62
- getCSS() {
83
+ async getCSS() {
63
84
  const names = new Set(fonts.map((i) => i.provider || defaultProvider));
64
85
  const preflights = [];
65
86
  for (const name of names) {
87
+ const fontsForProvider = fonts.filter((i) => i.provider === name);
66
88
  const provider = providers[name];
67
- preflights.push(provider.getPreflight(fonts.filter((i) => i.provider === name)));
89
+ if (provider.getImportUrl) {
90
+ const url = provider.getImportUrl(fontsForProvider);
91
+ if (url)
92
+ preflights.push(await importUrl(url));
93
+ }
94
+ preflights.push(provider.getPreflight?.(fontsForProvider));
68
95
  }
69
96
  return preflights.filter(Boolean).join("\n");
70
97
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/preset-web-fonts",
3
- "version": "0.21.2",
3
+ "version": "0.22.3",
4
4
  "description": "Web Fonts support for Uno CSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -42,7 +42,8 @@
42
42
  "*.css"
43
43
  ],
44
44
  "dependencies": {
45
- "@unocss/core": "0.21.2"
45
+ "axios": "^0.24.0",
46
+ "@unocss/core": "0.22.3"
46
47
  },
47
48
  "scripts": {
48
49
  "build": "unbuild",