-
Notifications
You must be signed in to change notification settings - Fork 182
fix: detect Turbopack builds and set TURBOPACK env accordingly #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
relsunkaev
wants to merge
1
commit into
opennextjs:main
Choose a base branch
from
relsunkaev:fix/turbopack-runtime-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+20
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { createPatchCode } from "../astCodePatcher.js"; | ||
| import type { CodePatcher } from "../codePatcher"; | ||
| import { createPatchCode, patchCode } from "../astCodePatcher.js"; | ||
| import type { CodePatcher, PatchCodeFn } from "../codePatcher.js"; | ||
|
|
||
| /** | ||
| * Creates a rule to replace `process.env.${envVar}` by `value` in the condition of if statements | ||
|
|
@@ -21,6 +21,22 @@ fix: | |
| '${value}' | ||
| `; | ||
|
|
||
| function isTurbopackBuild(tracedFiles: string[]): boolean { | ||
| const hasTurboRuntime = tracedFiles.some((file) => | ||
| /-turbo[.-].*\.runtime\.(prod|dev)\.js$/.test(file), | ||
| ); | ||
| const hasNonTurboRuntime = tracedFiles.some( | ||
| (file) => | ||
| /\.runtime\.(prod|dev)\.js$/.test(file) && !/-turbo[.-]/.test(file), | ||
| ); | ||
| return hasTurboRuntime && !hasNonTurboRuntime; | ||
| } | ||
|
|
||
| const createTurbopackPatch: PatchCodeFn = async ({ code, tracedFiles }) => { | ||
| const value = isTurbopackBuild(tracedFiles) ? "true" : "false"; | ||
| return patchCode(code, envVarRuleCreator("TURBOPACK", value)); | ||
| }; | ||
|
|
||
| export const patchEnvVars: CodePatcher = { | ||
| name: "patch-env-vars", | ||
| patches: [ | ||
|
|
@@ -39,12 +55,12 @@ export const patchEnvVars: CodePatcher = { | |
| contentFilter: /process\.env\.NODE_ENV/, | ||
| patchCode: createPatchCode(envVarRuleCreator("NODE_ENV", '"production"')), | ||
| }, | ||
| // This patch will set `TURBOPACK` env to false to avoid loading turbopack related deps at runtime | ||
| // Turbopack builds only include *-turbo.runtime.prod.js files, so we detect and set accordingly | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I don't think we really care about how we detect a turbopack build in this comment? Maybe revert to the inlined version of the code and remove the comment altogether when you add the helper? |
||
| { | ||
| versions: ">=15.0.0", | ||
| pathFilter: /module\.compiled\.js$/, | ||
| contentFilter: /process\.env\.TURBOPACK/, | ||
| patchCode: createPatchCode(envVarRuleCreator("TURBOPACK", "false")), | ||
| patchCode: createTurbopackPatch, | ||
| }, | ||
| ], | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to extract this into an helper
We use this check in the cloudflare adapter