Skip to content

Commit 8e8a97e

Browse files
committed
fix: detect Turbopack builds and set TURBOPACK env accordingly
When building with Turbopack (default in Next.js 16), the standalone output only includes *-turbo.runtime.prod.js files, not the webpack variants. The previous behavior of unconditionally setting TURBOPACK=false caused runtime errors because module.compiled.js would try to load non-existent runtime files. This change: - Detects Turbopack builds by checking if only turbo runtime files exist in the traced files - Sets TURBOPACK=true for Turbopack builds so the correct runtime is loaded - Keeps TURBOPACK=false for Webpack builds (existing behavior) Fixes runtime error: Cannot find module 'next/dist/compiled/next-server/pages.runtime.prod.js'
1 parent 47a84a7 commit 8e8a97e

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

packages/open-next/src/build/patch/patches/patchEnvVar.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createPatchCode } from "../astCodePatcher.js";
2-
import type { CodePatcher } from "../codePatcher";
1+
import { createPatchCode, patchCode } from "../astCodePatcher.js";
2+
import type { CodePatcher, PatchCodeFn } from "../codePatcher.js";
33

44
/**
55
* Creates a rule to replace `process.env.${envVar}` by `value` in the condition of if statements
@@ -21,6 +21,54 @@ fix:
2121
'${value}'
2222
`;
2323

24+
/**
25+
* Detects if the build was done with Turbopack by checking for turbo runtime files
26+
* in the traced files list.
27+
*
28+
* Turbopack builds include files like:
29+
* - pages-turbo.runtime.prod.js
30+
* - app-page-turbo.runtime.prod.js
31+
*
32+
* Webpack builds include files like:
33+
* - pages.runtime.prod.js
34+
* - app-page.runtime.prod.js
35+
*/
36+
function isTurbopackBuild(tracedFiles: string[]): boolean {
37+
// Check for turbo runtime files (contain "-turbo" followed by . or - in the name)
38+
// Examples: pages-turbo.runtime.prod.js, app-page-turbo-experimental.runtime.prod.js
39+
const turboFiles = tracedFiles.filter((file) =>
40+
/-turbo[.-].*\.runtime\.(prod|dev)\.js$/.test(file),
41+
);
42+
const hasTurboRuntime = turboFiles.length > 0;
43+
44+
// Check for non-turbo runtime files (end with .runtime.prod.js but don't have -turbo)
45+
// Examples: pages.runtime.prod.js, app-page.runtime.prod.js
46+
const nonTurboFiles = tracedFiles.filter(
47+
(file) =>
48+
/\.runtime\.(prod|dev)\.js$/.test(file) && !/-turbo[.-]/.test(file),
49+
);
50+
const hasNonTurboRuntime = nonTurboFiles.length > 0;
51+
52+
// If we have turbo runtimes but no non-turbo runtimes, it's a Turbopack build
53+
// This handles the case where Turbopack only includes turbo runtime files
54+
if (hasTurboRuntime && !hasNonTurboRuntime) {
55+
return true;
56+
}
57+
58+
return false;
59+
}
60+
61+
/**
62+
* Creates a patch function that sets TURBOPACK env var based on the build type.
63+
* For Turbopack builds, it sets TURBOPACK to true; for Webpack builds, it sets it to false.
64+
* This ensures the correct runtime files are loaded at runtime.
65+
*/
66+
const createTurbopackPatch: PatchCodeFn = async ({ code, tracedFiles }) => {
67+
const isTurbo = isTurbopackBuild(tracedFiles);
68+
const value = isTurbo ? "true" : "false";
69+
return patchCode(code, envVarRuleCreator("TURBOPACK", value));
70+
};
71+
2472
export const patchEnvVars: CodePatcher = {
2573
name: "patch-env-vars",
2674
patches: [
@@ -39,12 +87,14 @@ export const patchEnvVars: CodePatcher = {
3987
contentFilter: /process\.env\.NODE_ENV/,
4088
patchCode: createPatchCode(envVarRuleCreator("NODE_ENV", '"production"')),
4189
},
42-
// This patch will set `TURBOPACK` env to false to avoid loading turbopack related deps at runtime
90+
// This patch will set `TURBOPACK` env based on whether the build was done with Turbopack.
91+
// For Turbopack builds, only *-turbo.runtime.prod.js files exist, so we need TURBOPACK=true.
92+
// For Webpack builds, only non-turbo runtime files exist, so we need TURBOPACK=false.
4393
{
4494
versions: ">=15.0.0",
4595
pathFilter: /module\.compiled\.js$/,
4696
contentFilter: /process\.env\.TURBOPACK/,
47-
patchCode: createPatchCode(envVarRuleCreator("TURBOPACK", "false")),
97+
patchCode: createTurbopackPatch,
4898
},
4999
],
50100
};

0 commit comments

Comments
 (0)