Skip to content

Commit b2f9b98

Browse files
committed
turbopack plugins
1 parent 0060399 commit b2f9b98

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/cloudflare/src/cli/build/open-next/createServerBundle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { patchResRevalidate } from "../patches/plugins/res-revalidate.js";
3030
import { patchUseCacheIO } from "../patches/plugins/use-cache.js";
3131
import { normalizePath } from "../utils/index.js";
3232
import { copyWorkerdPackages } from "../utils/workerd.js";
33+
import { inlineChunksPatch } from "../patches/plugins/turbopack.js";
3334

3435
interface CodeCustomization {
3536
// These patches are meant to apply on user and next generated code
@@ -210,6 +211,7 @@ async function generateBundle(
210211
// Cloudflare specific patches
211212
patchResRevalidate,
212213
patchUseCacheIO,
214+
inlineChunksPatch,
213215
...additionalCodePatches,
214216
]);
215217

@@ -287,8 +289,8 @@ async function generateBundle(
287289
alias: {
288290
...(isBundled
289291
? {
290-
"next/dist/server/next-server.js": "./next-server.runtime.prod.js",
291-
}
292+
"next/dist/server/next-server.js": "./next-server.runtime.prod.js",
293+
}
292294
: {}),
293295
},
294296
},
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2+
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher";
3+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
4+
5+
const inlineChunksRule = `
6+
rule:
7+
kind: call_expression
8+
pattern: require(resolved)
9+
fix:
10+
requireChunk(chunkPath)
11+
`;
12+
13+
export const inlineChunksPatch: CodePatcher = {
14+
name: "inline-turbopack-chunks",
15+
patches: [
16+
{
17+
versions: ">=16.0.0",
18+
pathFilter: getCrossPlatformPathRegex(
19+
String.raw`\[turbopack\]_runtime\.js$`,
20+
{
21+
escape: false,
22+
}
23+
),
24+
contentFilter: /loadRuntimeChunkPath/,
25+
patchCode: async ({ code, tracedFiles }) => {
26+
const patched = patchCode(code, inlineChunksRule);
27+
28+
return `${patched}\n${inlineChunksFn(tracedFiles)}`;
29+
},
30+
},
31+
],
32+
};
33+
34+
function getInlinableChunks(tracedFiles: string[]) {
35+
const chunks = new Set<string>();
36+
for (const file of tracedFiles) {
37+
if (file.includes(".next/server/chunks/") && !file.includes("[turbopack]_runtime.js")) {
38+
chunks.add(file);
39+
}
40+
}
41+
return chunks;
42+
}
43+
44+
function inlineChunksFn(tracedFiles: string[]) {
45+
// From the outputs, we extract every chunks
46+
const chunks = getInlinableChunks(tracedFiles);
47+
return `
48+
function requireChunk(chunkPath) {
49+
switch(chunkPath) {
50+
${Array.from(chunks).map(chunk => ` case "${
51+
// we only want the path after /path/to/.next/
52+
chunk.replace(/.*\.next\//, "")
53+
}": return require("${chunk}");`).join("\n")}
54+
default:
55+
throw new Error(\`Not found \${chunkPath}\`);
56+
}
57+
}
58+
`;
59+
}

0 commit comments

Comments
 (0)