-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
39 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/// <reference path=".astro/types.d.ts" /> | ||
/// <reference types="astro/client" /> | ||
/// <reference types="astro-emotion/client" /> | ||
/// <reference types="astro-emotion/client" /> | ||
|
||
declare module 'font:*' { | ||
const href: string | ||
export default href | ||
} |
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,83 @@ | ||
import path from "node:path" | ||
import crypto from "node:crypto" | ||
import fs from "node:fs" | ||
import type { Plugin } from "vite" | ||
|
||
export default function (): Plugin { | ||
let root: string | ||
let metadata: Record<string, string> | ||
function metadataFilePath() { | ||
return path.join(root, "node_modules", ".fonts", "metadata.json") | ||
} | ||
function readMetadata() { | ||
if (metadata) return metadata | ||
const metadataFilePath = path.join(root, "node_modules", ".fonts", "metadata.json") | ||
try { | ||
const metadataText = fs.readFileSync(metadataFilePath, "utf8") | ||
metadata = JSON.parse(metadataText) | ||
} catch { | ||
fs.writeFileSync(metadataFilePath, "{}") | ||
metadata = {} | ||
} | ||
return metadata | ||
} | ||
function writeMetadata() { | ||
fs.writeFileSync(metadataFilePath(), JSON.stringify(metadata, null, 4)) | ||
} | ||
const resolveId: Plugin["resolveId"] = async function (id) { | ||
if (id.startsWith("font:") === false && id.startsWith("/font:") === false) { | ||
return | ||
} | ||
const query = id.startsWith("font:") ? id.slice(5) : id.slice(6) | ||
try { | ||
const metadata = readMetadata() | ||
const filePath = metadata[query] | ||
if (filePath) return filePath | ||
} | ||
catch { | ||
// continue | ||
} | ||
const cssResponse = await fetch(`https://fonts.googleapis.com/css2?${query}`, { | ||
headers: { | ||
"User-Agent": "AppleWebKit/537.36 Chrome/130.0.0.0" | ||
} | ||
}) | ||
const cssText = await cssResponse.text() | ||
const [ fontPath ] = /(?<=url\()https:\/\/fonts.gstatic.com\/(\S)+(?=\))/.exec(cssText)! | ||
const fontResponse = await fetch(fontPath) | ||
const fontBuffer = await fontResponse.arrayBuffer() | ||
const fontBytes = new Uint8Array(fontBuffer) | ||
const hash = crypto.hash("md5", fontBytes) | ||
const filePath = path.join(root, "node_modules", ".fonts", `${hash}.woff2`) | ||
fs.mkdirSync(path.dirname(filePath)) | ||
fs.writeFileSync(filePath, fontBytes) | ||
const metadata = readMetadata() | ||
metadata[query] = filePath | ||
writeMetadata() | ||
return filePath | ||
} | ||
return { | ||
name: "font-loader", | ||
config() { | ||
return { | ||
resolve: { | ||
alias: [{ | ||
find: /font:/, | ||
replacement: "font:", | ||
// url() in css can only be hooked into this way, | ||
// and even then the url must start with a forward | ||
// slash - dunno why, just vite being vite | ||
customResolver: { resolveId } | ||
}] | ||
} | ||
} | ||
}, | ||
configureServer({ config }) { | ||
root = config.root | ||
}, | ||
configResolved(config) { | ||
root = config.root | ||
}, | ||
resolveId | ||
} | ||
} |
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.