|
1 | 1 | import adapter from "@sveltejs/adapter-vercel"; |
2 | 2 | import { sveltekit } from "@sveltejs/kit/vite"; |
| 3 | +import { cpSync, existsSync, readdirSync, statSync } from "node:fs"; |
| 4 | +import { createRequire } from "node:module"; |
| 5 | +import { dirname, join } from "node:path"; |
3 | 6 | import tailwindcss from "@tailwindcss/vite"; |
4 | 7 | import { defineConfig } from "vite"; |
5 | 8 |
|
| 9 | +// satori 0.33 shapes text with harfbuzzjs, which reads hb.wasm from beside its |
| 10 | +// own hb.js at runtime via a computed path. @vercel/nft cannot see that read, |
| 11 | +// so the Vercel function ships hb.js without hb.wasm and /api/og dies with |
| 12 | +// ENOENT -> 500. Copy the wasm next to every traced copy of hb.js. |
| 13 | +const requireFromHere = createRequire(import.meta.url); |
| 14 | + |
| 15 | +function findHarfbuzzDirs(root: string): string[] { |
| 16 | + const found: string[] = []; |
| 17 | + const walk = (dir: string): void => { |
| 18 | + for (const name of readdirSync(dir)) { |
| 19 | + const full = join(dir, name); |
| 20 | + if (name === "hb.js" && dir.endsWith("harfbuzzjs")) { |
| 21 | + found.push(dir); |
| 22 | + } else if (statSync(full).isDirectory()) { |
| 23 | + walk(full); |
| 24 | + } |
| 25 | + } |
| 26 | + }; |
| 27 | + walk(root); |
| 28 | + return found; |
| 29 | +} |
| 30 | + |
| 31 | +function withHarfbuzzWasm(base: ReturnType<typeof adapter>) { |
| 32 | + return { |
| 33 | + ...base, |
| 34 | + async adapt(builder: Parameters<typeof base.adapt>[0]) { |
| 35 | + await base.adapt(builder); |
| 36 | + const functionsDir = join(process.cwd(), ".vercel/output/functions"); |
| 37 | + if (!existsSync(functionsDir)) return; |
| 38 | + const satoriDir = dirname(requireFromHere.resolve("satori/package.json")); |
| 39 | + const wasm = join( |
| 40 | + dirname( |
| 41 | + requireFromHere.resolve("harfbuzzjs/package.json", { |
| 42 | + paths: [satoriDir], |
| 43 | + }), |
| 44 | + ), |
| 45 | + "hb.wasm", |
| 46 | + ); |
| 47 | + for (const dir of findHarfbuzzDirs(functionsDir)) { |
| 48 | + cpSync(wasm, join(dir, "hb.wasm")); |
| 49 | + } |
| 50 | + }, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
6 | 54 | export default defineConfig({ |
7 | 55 | plugins: [ |
8 | 56 | tailwindcss(), |
9 | 57 | sveltekit({ |
10 | | - adapter: adapter(), |
| 58 | + adapter: withHarfbuzzWasm(adapter()), |
11 | 59 | alias: { |
12 | 60 | // Kit 3 removed the built-in $lib; the config alias still works |
13 | 61 | // (deprecated) and keeps every existing `$lib/...` import resolving. |
|
0 commit comments