feat: add vite build and transform step for base64 inlining

This commit is contained in:
Bart van der Braak 2024-01-21 23:16:14 +01:00
parent 19971636d4
commit ecc83f4a4f
2 changed files with 37 additions and 18 deletions

View file

@ -4,23 +4,23 @@ import { html as toReactNode } from 'satori-html';
import { OgImage } from '$lib/components/site'; import { OgImage } from '$lib/components/site';
import GeistRegular from '$lib/assets/og/Geist-Regular.woff'; import GeistRegular from '$lib/assets/og/Geist-Regular.woff';
import GeistBold from '$lib/assets/og/Geist-Bold.woff'; import GeistBold from '$lib/assets/og/Geist-Bold.woff';
import Me from '$lib/assets/og/me.jpg'; import imageData from '$lib/assets/og/me.jpg?base64';
import { readFile } from 'fs/promises'; // import { readFile } from 'fs/promises';
import path from 'path'; // import path from 'path';
const height = 630; const height = 630;
const width = 1200; const width = 1200;
const getImageData = async () => { // const getImageData = async () => {
// try { // // try {
const imagePath = path.join(process.cwd(), Me); // const imagePath = path.join(process.cwd(), Me);
const meImage = await readFile(imagePath); // const meImage = await readFile(imagePath);
return Buffer.from(meImage).toString('base64'); // return Buffer.from(meImage).toString('base64');
// } catch (error) { // // } catch (error) {
// console.error('Error reading image:', error); // // console.error('Error reading image:', error);
// throw error; // // throw error;
// } // // }
}; // };
/** @type {import('./$types').RequestHandler} */ /** @type {import('./$types').RequestHandler} */
export const GET = async ({ url }) => { export const GET = async ({ url }) => {
@ -28,7 +28,7 @@ export const GET = async ({ url }) => {
const title = url.searchParams.get('title') ?? undefined; const title = url.searchParams.get('title') ?? undefined;
const subTitle = url.searchParams.get('subTitle') ?? undefined; const subTitle = url.searchParams.get('subTitle') ?? undefined;
const imageData = await getImageData(); // const imageData = await getImageData();
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = (OgImage as any).render({ title, subTitle, imageData }); const result = (OgImage as any).render({ title, subTitle, imageData });
@ -62,7 +62,8 @@ export const GET = async ({ url }) => {
return new Response(image.asPng(), { return new Response(image.asPng(), {
headers: { headers: {
'content-type': 'image/png' 'content-type': 'image/png',
'cache-control': 'public, max-age=86400, immutable'
} }
}); });
// } catch (error) { // } catch (error) {

View file

@ -4,9 +4,11 @@ import { defineConfig } from 'vite';
import fs from 'fs'; import fs from 'fs';
export default defineConfig({ export default defineConfig({
plugins: [enhancedImages(), sveltekit(), rawFonts(['.woff'])], plugins: [enhancedImages(), sveltekit(), rawFonts(['.woff']), base64()],
ssr: { server: {
noExternal: ['three'] fs: {
allow: ['./']
}
}, },
define: { define: {
'import.meta.env.VERCEL_URL': JSON.stringify(process.env.VERCEL_URL) 'import.meta.env.VERCEL_URL': JSON.stringify(process.env.VERCEL_URL)
@ -24,3 +26,19 @@ function rawFonts(ext: string[]) {
} }
}; };
} }
function base64() {
return {
name: 'vite-plugin-base64-loader',
transform(_code: string, id: string) {
const [path, query] = id.split('?');
if (query !== 'base64') {
return null;
}
const base64 = fs.readFileSync(path, { encoding: 'base64' });
return { code: `export default ${JSON.stringify(base64)}`, map: null };
}
};
}