Skip to content

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 4 commits into from
Apr 29, 2025
Merged

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Apr 18, 2025

Should help with #548

This PR change bundleServer() to use the "workerd" build condition to allow including code specifically created for workerd.

When a package uses the "workerd" build conditions, we copy all the files from the source package - Next would not trace/copy files for the workerd runtime.

Note that the packages have to be declared in the serverExternalPackages of the Next config. Otherwise they are bundled during the Next build and not in the nft.json files as a result OpenNext can not detect them. This behavior is not ideal, happy to hear about any better idea!

TODO

  • add more comments
  • add some docs
  • use an @opennext/aws release in the package.json

/cc @ka-raa-ge @mhart

Update packages/cloudflare/src/cli/build/utils/workerd.spec.ts

Co-authored-by: conico974 <nicodorseuil@yahoo.fr>
fixup! feedback
fixup! bump aws to 3.5.8
Copy link

changeset-bot bot commented Apr 18, 2025

🦋 Changeset detected

Latest commit: 558633d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Apr 18, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/cloudflare@593

commit: 558633d

@vicb vicb requested review from conico974 and james-elicx April 18, 2025 12:46
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);
Copy link
Collaborator

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")

Copy link
Contributor Author

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 in serverExternalPackages - if not there it is bundled.

I think the warning would trigger for react-dom and packages in server-external-packages.json

Copy link
Collaborator

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.json
That'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

Copy link
Collaborator

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

@vicb vicb force-pushed the vb-branch-14 branch 2 times, most recently from 46a43d4 to cf2806c Compare April 29, 2025 13:01
@vicb vicb requested a review from conico974 April 29, 2025 13:58
@vicb
Copy link
Contributor Author

vicb commented Apr 29, 2025

I have updated the PR so that we can disabled using the workerd condition if it causes troubles.

Can I please get a review?

Copy link
Collaborator

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
We should add something in the docs for this. And probably something related to that in the troubleshooting sections

@vicb
Copy link
Contributor Author

vicb commented Apr 29, 2025

LGTM We should add something in the docs for this. And probably something related to that in the troubleshooting sections

I am thinking about updating https://opennext.js.org/cloudflare/troubleshooting#my-app-fails-to-build-when-i-import-a-specific-npm-package for now.

We should list the packages we know using the workerd condition: postgres for now

Thanks for the review

@vicb vicb merged commit faca3e1 into main Apr 29, 2025
7 checks passed
@vicb vicb deleted the vb-branch-14 branch April 29, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants