-
Notifications
You must be signed in to change notification settings - Fork 78
feat: turbopack support #983
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74a40ad
feat: turbopack support
conico974 e985c50
fixup! og with turbopack
vicb c4586a1
fixup! increase timeout for R2 population
vicb 60c7f62
fixup! add comments, rename
vicb a78799b
fixup! handle feedback
vicb 28910e7
fixup! lock file
vicb cf8c1da
fixup! process.env.TURBOPACK
vicb 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/cloudflare": minor | ||
| --- | ||
|
|
||
| feat: turbopack support |
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
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
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
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
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
89 changes: 89 additions & 0 deletions
89
packages/cloudflare/src/cli/build/patches/plugins/turbopack.ts
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
| import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js"; | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
|
||
| const inlineChunksRule = ` | ||
| rule: | ||
| kind: call_expression | ||
| pattern: require(resolved) | ||
| fix: | ||
| requireChunk(chunkPath) | ||
| `; | ||
|
|
||
| export const patchTurbopackRuntime: CodePatcher = { | ||
| name: "inline-turbopack-chunks", | ||
| patches: [ | ||
| { | ||
| versions: ">=15.0.0", | ||
| pathFilter: getCrossPlatformPathRegex(String.raw`\[turbopack\]_runtime\.js$`, { | ||
| escape: false, | ||
| }), | ||
| contentFilter: /loadRuntimeChunkPath/, | ||
| patchCode: async ({ code, tracedFiles }) => { | ||
| let patched = patchCode(code, inlineExternalImportRule); | ||
| patched = patchCode(patched, inlineChunksRule); | ||
|
|
||
| return `${patched}\n${inlineChunksFn(tracedFiles)}`; | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| function getInlinableChunks(tracedFiles: string[]): string[] { | ||
| const chunks = new Set<string>(); | ||
| for (const file of tracedFiles) { | ||
| if (file === "[turbopack]_runtime.js") { | ||
| continue; | ||
| } | ||
| if (file.includes(".next/server/chunks/")) { | ||
| chunks.add(file); | ||
| } | ||
| } | ||
| return Array.from(chunks); | ||
| } | ||
|
|
||
| function inlineChunksFn(tracedFiles: string[]) { | ||
| // From the outputs, we extract every chunks | ||
| const chunks = getInlinableChunks(tracedFiles); | ||
| return ` | ||
| function requireChunk(chunkPath) { | ||
| switch(chunkPath) { | ||
| ${chunks | ||
| .map( | ||
| (chunk) => | ||
| ` case "${ | ||
| // we only want the path after /path/to/.next/ | ||
| chunk.replace(/.*\/\.next\//, "") | ||
| }": return require("${chunk}");` | ||
| ) | ||
| .join("\n")} | ||
| default: | ||
| throw new Error(\`Not found \${chunkPath}\`); | ||
| } | ||
| } | ||
| `; | ||
| } | ||
|
|
||
| // Turbopack imports `og` via `externalImport`. | ||
vicb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // We patch it to: | ||
| // - add the explicit path so that the file is inlined by wrangler | ||
| // - use the edge version of the module instead of the node version. | ||
| // | ||
| // Modules that are not inlined (no added to the switch), would generate an error similar to: | ||
| // Failed to load external module path/to/module: Error: No such module "path/to/module" | ||
| const inlineExternalImportRule = ` | ||
| rule: | ||
| pattern: "$RAW = await import($ID)" | ||
| inside: | ||
| regex: "externalImport" | ||
| kind: function_declaration | ||
| stopBy: end | ||
| fix: |- | ||
| switch ($ID) { | ||
| case "next/dist/compiled/@vercel/og/index.node.js": | ||
| $RAW = await import("next/dist/compiled/@vercel/og/index.edge.js"); | ||
| break; | ||
| default: | ||
| $RAW = await import($ID); | ||
| } | ||
| `; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.