-
-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web-fonts): add local font support to web font preset
WIP
- Loading branch information
Showing
5 changed files
with
103 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Inspired by: | ||
* https://github.com/feat-agency/vite-plugin-webfont-dl/blob/master/src/downloader.ts | ||
*/ | ||
|
||
import { mkdir, stat, writeFile } from 'node:fs/promises' | ||
import { Buffer } from 'node:buffer' | ||
import { dirname, resolve } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import type { WebFontsOptions } from './types' | ||
|
||
const fontUrlRegex = /[-a-z0-9@:%_+.~#?&/=]+\.(?:woff2?|eot|ttf|otf|svg)/gi | ||
|
||
const dir = dirname(fileURLToPath(import.meta.url)) | ||
|
||
const cachedUrls = new Map<string, string>() | ||
|
||
export async function useLocalFont(css: string, fetch: WebFontsOptions['customFetch']) { | ||
const allUrls = css.match(fontUrlRegex)?.map(url => url.trim()) || [] | ||
const newUrls = [] | ||
for (const url of allUrls) { | ||
if (cachedUrls.has(url)) | ||
css = css.replace(url, cachedUrls.get(url)!) | ||
else | ||
newUrls.push(url) | ||
} | ||
|
||
if (newUrls.length === 0) | ||
return css | ||
|
||
async function saveFont(url: string, path: string) { | ||
const response = await fetch!(url, { headers: { responseType: 'arraybuffer' } }) as ArrayBuffer | ||
const content = new Uint8Array(response) | ||
await writeFile(path, Buffer.from(content)) | ||
return { url, path } | ||
} | ||
|
||
// FIXME Currently hardcoded to the playground folder | ||
const fontsFolder = resolve(dir, '../../../playground/assets/fonts') | ||
await mkdir(fontsFolder, { recursive: true }) | ||
|
||
const fontAlreadyDownloaded = async (path: string) => await stat(path).then(r => r.isFile()).catch(() => false) | ||
|
||
const promises = [] | ||
|
||
for (const url of newUrls) { | ||
const path = resolve(fontsFolder, url.split('/').pop()!) | ||
const ignore = await fontAlreadyDownloaded(path) | ||
if (ignore) | ||
cachedUrls.set(url, path) | ||
else | ||
promises.push(saveFont(url, path)) | ||
} | ||
|
||
const localFonts = await Promise.all(promises) | ||
|
||
for (const localFont of localFonts) { | ||
css = css.replace(localFont.url, localFont.path) | ||
cachedUrls.set(localFont.url, localFont.path) | ||
} | ||
|
||
return css | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters