|
| 1 | +import { mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import { BuildOptions } from "@opennextjs/aws/build/helper.js"; |
| 5 | +import mockFs from "mock-fs"; |
| 6 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +import { patchVercelOgLibrary } from "./patch-vercel-og-library"; |
| 9 | + |
| 10 | +const nodeModulesVercelOgDir = "node_modules/.pnpm/next@14.2.11/node_modules/next/dist/compiled/@vercel/og"; |
| 11 | +const nextServerOgNftPath = "examples/api/.next/server/app/og/route.js.nft.json"; |
| 12 | +const openNextFunctionDir = "examples/api/.open-next/server-functions/default/examples/api"; |
| 13 | +const openNextOgRoutePath = path.join(openNextFunctionDir, ".next/server/app/og/route.js"); |
| 14 | +const openNextVercelOgDir = path.join(openNextFunctionDir, "node_modules/next/dist/compiled/@vercel/og"); |
| 15 | + |
| 16 | +const buildOpts = { |
| 17 | + appBuildOutputPath: "examples/api", |
| 18 | + monorepoRoot: "", |
| 19 | + outputDir: "examples/api/.open-next", |
| 20 | +} as BuildOptions; |
| 21 | + |
| 22 | +describe("patchVercelOgLibrary", () => { |
| 23 | + beforeAll(() => { |
| 24 | + mockFs(); |
| 25 | + |
| 26 | + mkdirSync(nodeModulesVercelOgDir, { recursive: true }); |
| 27 | + mkdirSync(path.dirname(nextServerOgNftPath), { recursive: true }); |
| 28 | + mkdirSync(path.dirname(openNextOgRoutePath), { recursive: true }); |
| 29 | + mkdirSync(openNextVercelOgDir, { recursive: true }); |
| 30 | + |
| 31 | + writeFileSync( |
| 32 | + nextServerOgNftPath, |
| 33 | + JSON.stringify({ version: 1, files: [`../../../../../../${nodeModulesVercelOgDir}/index.node.js`] }) |
| 34 | + ); |
| 35 | + writeFileSync( |
| 36 | + path.join(nodeModulesVercelOgDir, "index.edge.js"), |
| 37 | + `var fallbackFont = fetch(new URL("./noto-sans-v27-latin-regular.ttf", import.meta.url)).then((res) => res.arrayBuffer());` |
| 38 | + ); |
| 39 | + writeFileSync(openNextOgRoutePath, `e.exports=import("next/dist/compiled/@vercel/og/index.node.js")`); |
| 40 | + writeFileSync(path.join(openNextVercelOgDir, "index.node.js"), ""); |
| 41 | + writeFileSync(path.join(openNextVercelOgDir, "noto-sans-v27-latin-regular.ttf"), ""); |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(() => mockFs.restore()); |
| 45 | + |
| 46 | + it("should patch the open-next files correctly", () => { |
| 47 | + patchVercelOgLibrary(buildOpts); |
| 48 | + |
| 49 | + expect(readdirSync(openNextVercelOgDir)).toMatchInlineSnapshot(` |
| 50 | + [ |
| 51 | + "index.edge.js", |
| 52 | + "index.node.js", |
| 53 | + "noto-sans-v27-latin-regular.ttf.bin", |
| 54 | + ] |
| 55 | + `); |
| 56 | + |
| 57 | + expect(readFileSync(path.join(openNextVercelOgDir, "index.edge.js"), { encoding: "utf-8" })) |
| 58 | + .toMatchInlineSnapshot(` |
| 59 | + "async function getFallbackFont() { |
| 60 | + // .bin is used so that a loader does not need to be configured for .ttf files |
| 61 | + return (await import("./noto-sans-v27-latin-regular.ttf.bin")).default; |
| 62 | + } |
| 63 | +
|
| 64 | + var fallbackFont = getFallbackFont();" |
| 65 | + `); |
| 66 | + |
| 67 | + expect(readFileSync(openNextOgRoutePath, { encoding: "utf-8" })).toMatchInlineSnapshot( |
| 68 | + `"e.exports=import("next/dist/compiled/@vercel/og/index.edge.js")"` |
| 69 | + ); |
| 70 | + }); |
| 71 | +}); |
0 commit comments