Skip to content

fix deploying Cloudflare:sockets and cloudflare email workers #723

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

franz101
Copy link

Based on #661

Copy link

changeset-bot bot commented Jun 10, 2025

🦋 Changeset detected

Latest commit: 7c25c83

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 Minor

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

@@ -104,7 +104,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
// Apply updater updates, must be the last plugin
updater.plugin,
] as Plugin[],
external: ["./middleware/handler.mjs"],
external: ["./middleware/handler.mjs", "cloudflare:sockets", "cloudflare:email"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Would cloudflare:* work here ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it should, also should it have node:*?

Copy link
Contributor

@vicb vicb Jun 11, 2025

Choose a reason for hiding this comment

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

Yes, it should, also should it have node:*?

That is taken care of by platform: "node",

But we should have tests for cloudflare:* :)

Copy link
Author

Choose a reason for hiding this comment

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

how would you do this test?
open-next build worker.ts?

import "cloudflare:email";

export async function handler({
    return {}
}

@vicb
Copy link
Contributor

vicb commented Jun 10, 2025

Thanks for the change, could you please add a patch changeset (using pnpm changeset)

Copy link

pkg-pr-new bot commented Jun 10, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@723

commit: 8172457

@franz101
Copy link
Author

Currently running issues, when running

import 'cloudflare:email' in an API file

Maybe I forgot server-only or could be related to the next config:
vercel/next.js#50177 (reply in thread)

@franz101
Copy link
Author

@vicb @vicb

need some support. it looks like opennext just bundles it into the following:
{var e5=Error("Cannot find module 'cloudflare:email'");throw e5.code="MODULE_NOT_FOUND",e5}) even with external libraries set

vercel/next.js#50177 (comment)

@sommeeeer
Copy link
Collaborator

What if you add it to serverExternalPackages in your next config?

@franz101
Copy link
Author

What if you add it to serverExternalPackages in your next config?

Same error:
https://github.com/franz101/opennext-cf-email

@franz101
Copy link
Author

@franz101
Copy link
Author

Requires this plugin:


export const builtInModulesPlugin = {
	name: 'built-in:modules',
	setup(build) {
		build.onResolve(
			{ filter: /^(node:|cloudflare:|async_hooks)/ },
			({ kind, path }) => {
				/**
				 * This plugin converts `require("<PREFIX>:*")` calls, those are the only ones that need
				 * updating (esm imports to "<PREFIX>:*" are totally valid), so here we tag with the
				 * built-in-modules namespace only imports that are require calls.
				 */
				return kind === 'require-call'
					? { path, namespace: 'built-in-modules' }
					: undefined;
			},
		);

		/**
		 * We convert the imports we tagged with the built-in-modules namespace so that instead of
		 * `require("<PREFIX>:*")` they import from `export * from "<PREFIX>:*";`
		 */
		build.onLoad(
			{ filter: /.*/, namespace: 'built-in-modules' },
			({ path }) => {
				return {
					contents: `export * from '${path}'`,
					loader: 'js',
				};
			},
		);
	},
};
  webpack: (config) => {
      config.externals.push('cloudflare:email');
      config.externals.push('cloudflare:socket');
      config.externalsType = 'commonjs';
    return config;
  }
};```

@franz101
Copy link
Author

how would you design the test? it needs to be run with wrangler dev afaik

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.

4 participants