Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preset-web-fonts): add customFetch option #2000

Merged
merged 5 commits into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/preset-web-fonts/README.md
Expand Up @@ -73,8 +73,47 @@ Currently supported Providers:
- `bunny` - [Privacy-Friendly Google Fonts](https://fonts.bunny.net/)
- `fontshare` - [Quality Font Service by ITF](https://www.fontshare.com/)

### Custom request function
Use your own request to fetch font source
MinatoHikari marked this conversation as resolved.
Show resolved Hide resolved

```ts
import presetWebFonts from '@unocss/preset-web-fonts'
import presetUno from '@unocss/preset-uno'
import axios from 'axios'
import ProxyAgent from 'proxy-agent'

Unocss({
presets: [
presetUno(),
presetWebFonts({
// use axios with an https proxy
customRequest: (url: string) => axios.get(url, { httpsAgent: new ProxyAgent('https://localhost:7890') }),
provider: 'google',
fonts: {
sans: 'Roboto',
mono: ['Fira Code', 'Fira Mono:400,700'],
// custom ones
lobster: 'Lobster',
lato: [
{
name: 'Lato',
weights: ['400', '700'],
italic: true,
},
{
name: 'sans-serif',
provider: 'none',
},
],
},
}),
],
})
```

PR welcome to add more providers 🙌


MinatoHikari marked this conversation as resolved.
Show resolved Hide resolved
## Configuration

Refer to the [type definition](https://github.com/unocss/unocss/blob/main/packages/preset-web-fonts/src/types.ts#L4) for all configurations available.
Expand Down
5 changes: 4 additions & 1 deletion packages/preset-web-fonts/src/index.ts
Expand Up @@ -35,6 +35,7 @@ const preset = (options: WebFontsOptions = {}): Preset<any> => {
extendTheme = true,
inlineImports = true,
themeKey = 'fontFamily',
customRequest,
} = options

const fontObject = Object.fromEntries(
Expand All @@ -49,7 +50,9 @@ const preset = (options: WebFontsOptions = {}): Preset<any> => {
if (inlineImports) {
if (!importCache[url]) {
const { $fetch } = await import('ohmyfetch')
importCache[url] = $fetch(url, { headers: {}, retry: 3 })
importCache[url] = (customRequest
? customRequest(url)
: $fetch(url, { headers: {}, retry: 3 }))
.catch((e) => {
console.error('Failed to fetch web fonts')
console.error(e)
Expand Down
7 changes: 7 additions & 0 deletions packages/preset-web-fonts/src/types.ts
Expand Up @@ -42,6 +42,13 @@ export interface WebFontsOptions {
* @default true
*/
inlineImports?: boolean

/**
* Inline CSS @import()
*
* @default undefined
*/
customRequest?: (url: string) => Promise<any>
}

export interface Provider {
Expand Down