/all route

This commit is contained in:
2026-02-04 19:37:53 +00:00
parent cbea8da007
commit 8a68243ccb

View File

@@ -112,6 +112,14 @@ const parseFontData = (css: string) => {
}; };
}; };
const addFontDisplaySwap = (css: string) =>
css.replace(/@font-face\s*{[^}]*}/gms, (block) => {
if (/font-display\s*:/i.test(block)) {
return block;
}
return block.replace(/}\s*$/, "\n\tfont-display: swap;\n}");
});
const buildFontCatalog = async () => { const buildFontCatalog = async () => {
const entries = await readdir(cssDir, { withFileTypes: true }); const entries = await readdir(cssDir, { withFileTypes: true });
const cssFiles = entries const cssFiles = entries
@@ -184,9 +192,7 @@ const buildFontCatalog = async () => {
} }
} }
const importCss = importUrls const importCss = "@import url(\"/all\");";
.map((url) => `@import url("${url}");`)
.join("\n");
return { return {
cards: cards.join("\n"), cards: cards.join("\n"),
@@ -246,7 +252,7 @@ const cssRoutes = async () => {
const filePath = join(cssDir, entry.name); const filePath = join(cssDir, entry.name);
app.get(route, async (c) => { app.get(route, async (c) => {
const css = await Bun.file(filePath).text(); const css = addFontDisplaySwap(await Bun.file(filePath).text());
return c.text(css, 200, { return c.text(css, 200, {
"Content-Type": "text/css; charset=utf-8", "Content-Type": "text/css; charset=utf-8",
"Cache-Control": cssCacheControl, "Cache-Control": cssCacheControl,
@@ -257,6 +263,26 @@ const cssRoutes = async () => {
await cssRoutes(); await cssRoutes();
app.get("/all", async (c) => {
const entries = await readdir(cssDir, { withFileTypes: true });
const cssFiles = entries
.filter((entry) => entry.isFile() && entry.name.endsWith(".css"))
.map((entry) => entry.name)
.sort((a, b) => a.localeCompare(b));
const combined = await Promise.all(
cssFiles.map(async (fileName) => {
const filePath = join(cssDir, fileName);
return addFontDisplaySwap(await Bun.file(filePath).text());
}),
);
return c.text(combined.join("\n"), 200, {
"Content-Type": "text/css; charset=utf-8",
"Cache-Control": cssCacheControl,
});
});
const port = Number(Bun.env.PORT ?? 1553); const port = Number(Bun.env.PORT ?? 1553);
export default { export default {