@unocss/preset-web-fonts 0.47.6 → 0.48.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/README.md CHANGED
@@ -75,6 +75,32 @@ Currently supported Providers:
75
75
 
76
76
  PR welcome to add more providers 🙌
77
77
 
78
+ ### Custom fetch function
79
+
80
+ Use your own function to fetch font source.
81
+
82
+ ```ts
83
+ import presetWebFonts from '@unocss/preset-web-fonts'
84
+ import presetUno from '@unocss/preset-uno'
85
+ import axios from 'axios'
86
+ import ProxyAgent from 'proxy-agent'
87
+
88
+ Unocss({
89
+ presets: [
90
+ presetUno(),
91
+ presetWebFonts({
92
+ // use axios with an https proxy
93
+ customFetch: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }),
94
+ provider: 'google',
95
+ fonts: {
96
+ sans: 'Roboto',
97
+ mono: ['Fira Code', 'Fira Mono:400,700'],
98
+ },
99
+ }),
100
+ ],
101
+ })
102
+ ```
103
+
78
104
  ## Configuration
79
105
 
80
106
  Refer to the [type definition](https://github.com/unocss/unocss/blob/main/packages/preset-web-fonts/src/types.ts#L4) for all configurations available.
package/dist/index.cjs CHANGED
@@ -80,7 +80,8 @@ const preset = (options = {}) => {
80
80
  provider: defaultProvider = "google",
81
81
  extendTheme = true,
82
82
  inlineImports = true,
83
- themeKey = "fontFamily"
83
+ themeKey = "fontFamily",
84
+ customFetch
84
85
  } = options;
85
86
  const fontObject = Object.fromEntries(
86
87
  Object.entries(options.fonts || {}).map(([name, meta]) => [name, core.toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))])
@@ -90,8 +91,8 @@ const preset = (options = {}) => {
90
91
  async function importUrl(url) {
91
92
  if (inlineImports) {
92
93
  if (!importCache[url]) {
93
- const { $fetch } = await import('ohmyfetch');
94
- importCache[url] = $fetch(url, { headers: {}, retry: 3 }).catch((e) => {
94
+ const promise = customFetch ? customFetch(url) : (await import('ohmyfetch')).$fetch(url, { headers: {}, retry: 3 });
95
+ importCache[url] = promise.catch((e) => {
95
96
  console.error("Failed to fetch web fonts");
96
97
  console.error(e);
97
98
  if (typeof process !== "undefined" && process.env.CI)
package/dist/index.d.ts CHANGED
@@ -38,6 +38,12 @@ interface WebFontsOptions {
38
38
  * @default true
39
39
  */
40
40
  inlineImports?: boolean;
41
+ /**
42
+ * Custom fetch function
43
+ *
44
+ * @default undefined
45
+ */
46
+ customFetch?: (url: string) => Promise<any>;
41
47
  }
42
48
  interface Provider {
43
49
  name: WebFontsProviders;
package/dist/index.mjs CHANGED
@@ -76,7 +76,8 @@ const preset = (options = {}) => {
76
76
  provider: defaultProvider = "google",
77
77
  extendTheme = true,
78
78
  inlineImports = true,
79
- themeKey = "fontFamily"
79
+ themeKey = "fontFamily",
80
+ customFetch
80
81
  } = options;
81
82
  const fontObject = Object.fromEntries(
82
83
  Object.entries(options.fonts || {}).map(([name, meta]) => [name, toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))])
@@ -86,8 +87,8 @@ const preset = (options = {}) => {
86
87
  async function importUrl(url) {
87
88
  if (inlineImports) {
88
89
  if (!importCache[url]) {
89
- const { $fetch } = await import('ohmyfetch');
90
- importCache[url] = $fetch(url, { headers: {}, retry: 3 }).catch((e) => {
90
+ const promise = customFetch ? customFetch(url) : (await import('ohmyfetch')).$fetch(url, { headers: {}, retry: 3 });
91
+ importCache[url] = promise.catch((e) => {
91
92
  console.error("Failed to fetch web fonts");
92
93
  console.error(e);
93
94
  if (typeof process !== "undefined" && process.env.CI)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/preset-web-fonts",
3
- "version": "0.47.6",
3
+ "version": "0.48.1",
4
4
  "description": "Web Fonts support for Uno CSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -37,7 +37,7 @@
37
37
  "*.css"
38
38
  ],
39
39
  "dependencies": {
40
- "@unocss/core": "0.47.6",
40
+ "@unocss/core": "0.48.1",
41
41
  "ohmyfetch": "^0.4.21"
42
42
  },
43
43
  "scripts": {