Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dda90c2

Browse files
authored
feat(preset-web-fonts): add fetch option to LocalFontProcessor (#4894)
1 parent 30bcc3d commit dda90c2

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

docs/presets/web-fonts.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ export default defineConfig({
236236
fontAssetsDir: 'public/assets/fonts',
237237

238238
// Base URL to serve the fonts from the client
239-
fontServeBaseUrl: '/assets/fonts'
239+
fontServeBaseUrl: '/assets/fonts',
240+
241+
// Custom fetch function to download the fonts
242+
fetch: async url => axios.get(url)
240243
})
241244
}),
242245
],

packages-presets/preset-web-fonts/src/local.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export interface LocalFontProcessorOptions {
4040
* @default '/assets/fonts'
4141
*/
4242
fontServeBaseUrl?: string
43+
44+
/**
45+
* Custom fetch function to provide the font data.
46+
*/
47+
fetch?: typeof fetch
4348
}
4449

4550
export function createLocalFontProcessor(options?: LocalFontProcessorOptions): WebFontProcessor {
@@ -50,7 +55,8 @@ export function createLocalFontProcessor(options?: LocalFontProcessorOptions): W
5055
const fontServeBaseUrl = options?.fontServeBaseUrl || '/assets/fonts'
5156

5257
async function _downloadFont(url: string, assetPath: string) {
53-
const response = await fetch(url)
58+
const fetcher = options?.fetch ?? fetch
59+
const response = await fetcher(url)
5460
.then(r => r.arrayBuffer())
5561
await fsp.mkdir(fontAssetsDir, { recursive: true })
5662
await fsp.writeFile(assetPath, Buffer.from(response))

0 commit comments

Comments
 (0)