Skip to content
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

feat(cloudflare): generated wrangler configuration #2949

Merged
merged 16 commits into from
Feb 3, 2025
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Dec 20, 2024

Nitro has introduced an experimental (opt-in) way to generate/extend wrangler.toml for cloudflare deployments (#2353, #2355) (However, overwriting the user config is not ideal or recommended, so it remain experimental)

The end goal is to leverage Cloudflare platform features such as Node compat v2 (hybrid) and Async Context with zero-config and ensure we can reliably enable these features.

Thanks to the huge huge efforts and collab with the Cloudflare team and @petebacondarwin, a new solution (~> cloudflare/workers-sdk#7442) is coming to allow frameworks like Nitro to generate a merged wrangler.toml in dist and config that redirects/points wrangler CLI and pages CI to it.

Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#generated-wrangler-configuration

Remarks

  • cloudflare.noWranglerDeployConfig: true can be used to opt-out from generating deploy-config in .wrangler/deploy/config.json
  • User config wrangler.toml|json is read and always merged with the highest precedence
  • Modules and frameworks can extend config by extending cloudflare.wrangler object
  • Nitro preset adds defaults
    • compatibility_date defaults to compatibilityDate from framework config
    • compatibility_flags defaults to ["nodejs_compat", "no_nodejs_compat_v2"]

Pages

pnpm nitro build playground --preset cloudflare_pages

dist/_worker.js/wrangler.json:

{
  "compatibility_date": "2024-09-19",
  "pages_build_output_dir": ".."
}

Modules

pnpm nitro build playground --preset cloudflare_module

.output/server/wrangler.json:

{
  "compatibility_date": "2024-09-19",
  "main": "index.mjs",
  "assets": {
    "binding": "ASSETS",
    "directory": "../public"
  }
}

@petebacondarwin
Copy link

petebacondarwin commented Jan 8, 2025

Given this is using the cloudflare_pages plugin, this should not have an assets section in the user's wrangler.toml.
Nor should it have Durable Objects defined, since these are not valid in Pages projects.

This is likely to break the Pages CI.

Not a problem in real life because the user wouldn't have that. But in terms of testing, I will remove that from the wrangler.toml

@petebacondarwin
Copy link

For Pages projects that have a wrangler.toml it is required to have a pages_build_output_dir field in order that the config is considered for deployment. Otherwise it is completely ignored.

Depending on what you want the DX, you could either require the user to add that field themselves, or you could add it as part of the generation of wrangler.json. I think the latter is better, since I believe you want to generate a wrangler.json even if there is no user wrangler.toml.

@pi0 pi0 changed the title feat(cloudflare): experimental config redirection support feat(cloudflare): generated wrangler configuration Jan 30, 2025
src/presets/cloudflare/preset.ts Outdated Show resolved Hide resolved
@pi0 pi0 marked this pull request as ready for review February 3, 2025 13:43
@pi0 pi0 merged commit e9e920a into v2 Feb 3, 2025
4 checks passed
@pi0 pi0 deleted the feat/cf-gen-wrangler branch February 3, 2025 13:47
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.

3 participants