-
Notifications
You must be signed in to change notification settings - Fork 73
Allow using external packages with a workerd build condition #593
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
4 commits
Select commit
Hold shift + click to select a range
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": patch | ||
--- | ||
|
||
Use the workerd build condition by default |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./cloudflare-context.js"; | ||
export { defineCloudflareConfig } from "./config.js"; | ||
export { defineCloudflareConfig, type OpenNextConfig } from "./config.js"; |
2 changes: 1 addition & 1 deletion
2
packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.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
2 changes: 1 addition & 1 deletion
2
packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { hasBuildCondition } from "./workerd"; | ||
|
||
describe("hasBuildConditions", () => { | ||
test("undefined", () => { | ||
expect(hasBuildCondition(undefined, "workerd")).toBe(false); | ||
}); | ||
|
||
test("top level", () => { | ||
const exports = { | ||
workerd: "./path/to/workerd.js", | ||
default: "./path/to/default.js", | ||
}; | ||
|
||
expect(hasBuildCondition(exports, "workerd")).toBe(true); | ||
expect(hasBuildCondition(exports, "default")).toBe(true); | ||
expect(hasBuildCondition(exports, "module")).toBe(false); | ||
}); | ||
|
||
test("nested", () => { | ||
const exports = { | ||
".": "/path/to/index.js", | ||
"./server": { | ||
"react-server": { | ||
workerd: "./server.edge.js", | ||
}, | ||
default: "./server.js", | ||
}, | ||
}; | ||
|
||
expect(hasBuildCondition(exports, "workerd")).toBe(true); | ||
expect(hasBuildCondition(exports, "default")).toBe(true); | ||
expect(hasBuildCondition(exports, "module")).toBe(false); | ||
}); | ||
|
||
test("only consider leaves", () => { | ||
const exports = { | ||
".": "/path/to/index.js", | ||
"./server": { | ||
workerd: { | ||
default: "./server.edge.js", | ||
}, | ||
}, | ||
}; | ||
|
||
expect(hasBuildCondition(exports, "workerd")).toBe(false); | ||
}); | ||
}); |
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,57 @@ | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
|
||
import { loadConfig } from "@opennextjs/aws/adapters/config/util.js"; | ||
import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; | ||
import logger from "@opennextjs/aws/logger.js"; | ||
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
||
/** | ||
* Return whether the passed export map has the given condition | ||
*/ | ||
export function hasBuildCondition( | ||
exports: { [key: string]: unknown } | undefined, | ||
condition: string | ||
): boolean { | ||
if (!exports) { | ||
return false; | ||
} | ||
for (const [key, value] of Object.entries(exports)) { | ||
if (typeof value === "object" && value != null) { | ||
if (hasBuildCondition(value as { [key: string]: unknown }, condition)) { | ||
return true; | ||
} | ||
} else { | ||
if (key === condition) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export async function copyWorkerdPackages(options: BuildOptions, nodePackages: Map<string, string>) { | ||
const isNodeModuleRegex = getCrossPlatformPathRegex(`.*/node_modules/(?<pkg>.*)`, { escape: false }); | ||
|
||
// Copy full external packages when they use "workerd" build condition | ||
const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next")); | ||
const externalPackages = nextConfig.serverExternalPackages ?? []; | ||
for (const [src, dst] of nodePackages.entries()) { | ||
try { | ||
const { exports } = JSON.parse(await fs.readFile(path.join(src, "package.json"), "utf8")); | ||
const match = src.match(isNodeModuleRegex); | ||
if ( | ||
match?.groups?.pkg && | ||
externalPackages.includes(match.groups.pkg) && | ||
hasBuildCondition(exports, "workerd") | ||
) { | ||
logger.debug( | ||
`Copying package using a workerd condition: ${path.relative(options.appPath, src)} -> ${path.relative(options.appPath, dst)}` | ||
); | ||
fs.cp(src, dst, { recursive: true, force: true }); | ||
vicb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} catch { | ||
logger.error(`Failed to copy ${src}`); | ||
} | ||
} | ||
} |
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
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.
We should warn the user if
!externalPackages.includes(match.groups.pkg) && hasBuildCondition(exports, "workerd")
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.
I'm not sure what the best thing to do is here, do you have something in mind?
hasBuildCondition(...)
check for traced packages (nodePackages
).When I test with
postgres
, it would not be traced unless added inserverExternalPackages
- if not there it is bundled.I think the warning would trigger for
react-dom
and packages in server-external-packages.jsonThere 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.
Ho yeah right.
I think we should still add the warning here, at least for the packages you mentionned (we should probably filter some like
react-dom
)One thing we could do is maintain a list of known deps that cause trouble like
postgres
and look if they're in the user package.jsonThat's not ideal, but at least it will help a bit.
Another option would be to look at the lockfile, but it will show a lot of false positive (especially for monorepo) and would be complex to maintain (especially for bun and the binary lockfile)
I can't think of anything else at the moment
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.
FYI, i don't consider this blocking, we could figure this out later if you prefer