Skip to content

Commit e985c50

Browse files
committed
fixup! og with turbopack
1 parent 74a40ad commit e985c50

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

packages/cloudflare/src/cli/build/patches/ast/patch-vercel-og-library.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
2424
for (const traceInfoPath of globSync(path.join(appBuildOutputPath, ".next/server/**/*.nft.json"), {
2525
windowsPathsNoEscape: true,
2626
})) {
27+
let edgeFilePatched = false;
28+
2729
const traceInfo: TraceInfo = JSON.parse(readFileSync(traceInfoPath, { encoding: "utf8" }));
2830
const tracedNodePath = traceInfo.files.find((p) => p.endsWith("@vercel/og/index.node.js"));
2931

@@ -40,17 +42,23 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
4042
);
4143

4244
copyFileSync(tracedEdgePath, outputEdgePath);
45+
}
4346

47+
if (!edgeFilePatched) {
48+
edgeFilePatched = true;
4449
// Change font fetches in the library to use imports.
4550
const node = parseFile(outputEdgePath);
4651
const { edits, matches } = patchVercelOgFallbackFont(node);
4752
writeFileSync(outputEdgePath, node.commitEdits(edits));
4853

49-
const fontFileName = matches[0]!.getMatch("PATH")!.text();
50-
renameSync(path.join(outputDir, fontFileName), path.join(outputDir, `${fontFileName}.bin`));
54+
if (matches.length > 0) {
55+
const fontFileName = matches[0]!.getMatch("PATH")!.text();
56+
renameSync(path.join(outputDir, fontFileName), path.join(outputDir, `${fontFileName}.bin`));
57+
}
5158
}
5259

5360
// Change node imports for the library to edge imports.
61+
// This is only useful when tubopack is not used to bundle the function.
5462
const routeFilePath = traceInfoPath.replace(appBuildOutputPath, packagePath).replace(".nft.json", "");
5563

5664
const node = parseFile(routeFilePath);

packages/cloudflare/src/cli/build/patches/plugins/turbopack.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,26 @@ export const inlineChunksPatch: CodePatcher = {
2020
}),
2121
contentFilter: /loadRuntimeChunkPath/,
2222
patchCode: async ({ code, tracedFiles }) => {
23-
const patched = patchCode(code, inlineChunksRule);
23+
let patched = patchCode(code, inlineExternalImportRule);
24+
patched = patchCode(patched, inlineChunksRule);
2425

2526
return `${patched}\n${inlineChunksFn(tracedFiles)}`;
2627
},
2728
},
2829
],
2930
};
3031

31-
function getInlinableChunks(tracedFiles: string[]) {
32+
function getInlinableChunks(tracedFiles: string[]): string[] {
3233
const chunks = new Set<string>();
3334
for (const file of tracedFiles) {
34-
if (file.includes(".next/server/chunks/") && !file.includes("[turbopack]_runtime.js")) {
35+
if (file === "[turbopack]_runtime.js") {
36+
continue;
37+
}
38+
if (file.includes(".next/server/chunks/")) {
3539
chunks.add(file);
3640
}
3741
}
38-
return chunks;
42+
return Array.from(chunks);
3943
}
4044

4145
function inlineChunksFn(tracedFiles: string[]) {
@@ -44,12 +48,12 @@ function inlineChunksFn(tracedFiles: string[]) {
4448
return `
4549
function requireChunk(chunkPath) {
4650
switch(chunkPath) {
47-
${Array.from(chunks)
51+
${chunks
4852
.map(
4953
(chunk) =>
5054
` case "${
5155
// we only want the path after /path/to/.next/
52-
chunk.replace(/.*\.next\//, "")
56+
chunk.replace(/.*\/\.next\//, "")
5357
}": return require("${chunk}");`
5458
)
5559
.join("\n")}
@@ -59,3 +63,20 @@ ${Array.from(chunks)
5963
}
6064
`;
6165
}
66+
67+
const inlineExternalImportRule = `
68+
rule:
69+
pattern: "$RAW = await import($ID)"
70+
inside:
71+
regex: "externalImport"
72+
kind: function_declaration
73+
stopBy: end
74+
fix: |-
75+
switch ($ID) {
76+
case "next/dist/compiled/@vercel/og/index.node.js":
77+
$RAW = await import("next/dist/compiled/@vercel/og/index.edge.js");
78+
break;
79+
default:
80+
$RAW = await import($ID);
81+
}
82+
`;

0 commit comments

Comments
 (0)