Skip to content

Commit 74a40ad

Browse files
conico974vicb
authored andcommitted
feat: turbopack support
1 parent aefcb03 commit 74a40ad

File tree

6 files changed

+337
-10
lines changed

6 files changed

+337
-10
lines changed

.changeset/silver-phones-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
feat: turbopack support

examples/e2e/app-router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"openbuild": "node ../../packages/open-next/dist/index.js build --streaming --build-command \"npx turbo build\"",
77
"dev": "next dev --turbopack --port 3001",
8-
"build": "next build",
8+
"build": "next build --turbopack",
99
"start": "next start --port 3001",
1010
"lint": "next lint",
1111
"clean": "rm -rf .turbo node_modules .next .open-next",

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
144144
// We make sure that environment variables that Next.js expects are properly defined
145145
"process.env.NEXT_RUNTIME": '"nodejs"',
146146
"process.env.NODE_ENV": '"production"',
147-
// The 2 following defines are used to reduce the bundle size by removing unnecessary code
148-
// Next uses different precompiled renderers (i.e. `app-page.runtime.prod.js`) based on if you use `TURBOPACK` or some experimental React features
149-
// Turbopack is not supported for build at the moment, so we disable it
150-
"process.env.TURBOPACK": "false",
151147
// This define should be safe to use for Next 14.2+, earlier versions (13.5 and less) will cause trouble
152148
"process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
153149
// Fix `res.validate` in Next 15.4 (together with the `route-module` patch)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type { Plugin } from "esbuild";
2727

2828
import { getOpenNextConfig } from "../../../api/config.js";
2929
import { patchResRevalidate } from "../patches/plugins/res-revalidate.js";
30+
import { inlineChunksPatch } from "../patches/plugins/turbopack.js";
3031
import { patchUseCacheIO } from "../patches/plugins/use-cache.js";
3132
import { normalizePath } from "../utils/index.js";
3233
import { copyWorkerdPackages } from "../utils/workerd.js";
@@ -210,6 +211,7 @@ async function generateBundle(
210211
// Cloudflare specific patches
211212
patchResRevalidate,
212213
patchUseCacheIO,
214+
inlineChunksPatch,
213215
...additionalCodePatches,
214216
]);
215217

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2+
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js";
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: ">=15.0.0",
18+
pathFilter: getCrossPlatformPathRegex(String.raw`\[turbopack\]_runtime\.js$`, {
19+
escape: false,
20+
}),
21+
contentFilter: /loadRuntimeChunkPath/,
22+
patchCode: async ({ code, tracedFiles }) => {
23+
const patched = patchCode(code, inlineChunksRule);
24+
25+
return `${patched}\n${inlineChunksFn(tracedFiles)}`;
26+
},
27+
},
28+
],
29+
};
30+
31+
function getInlinableChunks(tracedFiles: string[]) {
32+
const chunks = new Set<string>();
33+
for (const file of tracedFiles) {
34+
if (file.includes(".next/server/chunks/") && !file.includes("[turbopack]_runtime.js")) {
35+
chunks.add(file);
36+
}
37+
}
38+
return chunks;
39+
}
40+
41+
function inlineChunksFn(tracedFiles: string[]) {
42+
// From the outputs, we extract every chunks
43+
const chunks = getInlinableChunks(tracedFiles);
44+
return `
45+
function requireChunk(chunkPath) {
46+
switch(chunkPath) {
47+
${Array.from(chunks)
48+
.map(
49+
(chunk) =>
50+
` case "${
51+
// we only want the path after /path/to/.next/
52+
chunk.replace(/.*\.next\//, "")
53+
}": return require("${chunk}");`
54+
)
55+
.join("\n")}
56+
default:
57+
throw new Error(\`Not found \${chunkPath}\`);
58+
}
59+
}
60+
`;
61+
}

0 commit comments

Comments
 (0)