1- import { copyFileSync , existsSync , readFileSync , renameSync , rmSync , writeFileSync } from "node:fs" ;
1+ import { copyFileSync , existsSync , readFileSync , renameSync , writeFileSync } from "node:fs" ;
22import path from "node:path" ;
33
44import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
@@ -13,11 +13,6 @@ type TraceInfo = { version: number; files: string[] };
1313/**
1414 * Patches the usage of @vercel/og to be compatible with Cloudflare Workers.
1515 *
16- * This involves;
17- * - Ensuring the edge version is available in the OpenNext node_modules.
18- * - Changing node imports for the library to edge imports.
19- * - Changing font fetches in the library to use .bin imports.
20- *
2116 * @param buildOpts Build options.
2217 */
2318export function patchVercelOgLibrary ( buildOpts : BuildOptions ) {
@@ -34,15 +29,16 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
3429 const outputDir = path . join ( packagePath , "node_modules/next/dist/compiled/@vercel/og" ) ;
3530 const outputEdgePath = path . join ( outputDir , "index.edge.js" ) ;
3631
32+ // Ensure the edge version is available in the OpenNext node_modules.
3733 if ( ! existsSync ( outputEdgePath ) ) {
3834 const tracedEdgePath = path . join (
3935 path . dirname ( traceInfoPath ) ,
4036 tracedNodePath . replace ( "index.node.js" , "index.edge.js" )
4137 ) ;
4238
4339 copyFileSync ( tracedEdgePath , outputEdgePath ) ;
44- rmSync ( outputEdgePath . replace ( "index.edge.js" , "index.node.js" ) ) ;
4540
41+ // Change font fetches in the library to use imports.
4642 const node = parseFile ( outputEdgePath ) ;
4743 const { edits, matches } = patchVercelOgFallbackFont ( node ) ;
4844 writeFileSync ( outputEdgePath , node . commitEdits ( edits ) ) ;
@@ -51,6 +47,7 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
5147 renameSync ( path . join ( outputDir , fontFileName ) , path . join ( outputDir , `${ fontFileName } .bin` ) ) ;
5248 }
5349
50+ // Change node imports for the library to edge imports.
5451 const routeFilePath = traceInfoPath . replace ( appBuildOutputPath , packagePath ) . replace ( ".nft.json" , "" ) ;
5552
5653 const node = parseFile ( routeFilePath ) ;
0 commit comments