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: support local wrangler.toml #113

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Barbapapazes
Copy link
Contributor

@Barbapapazes Barbapapazes commented May 18, 2024

EDIT: Instead of using a config object, and to support production, the user wrangler configuration file will be read.

Hello 👋,

Actually, you use the module nitro-cloudflare-dev under the hood to access to the Wrangler environment API. To setup this module, you create a wrangler.tom file.

However, if I want to access to another binding like AI or Email, it's not possible since I do not have any access to the wrangler.toml file.

This PR allows to customize the wrangler file.

For example:

nuxt.config.ts

export default defineNuxtConfig({
  hub: {
    wranglerConfiguration: {
      ai: {
        binding: 'AI'
      }
    }
  },
})

Will write the following wrangler.toml file:

[[r2_buckets]]
binding = "BLOB"
bucket_name = "default"

[[kv_namespaces]]
binding = "KV"
id = "kv_default"

[[kv_namespaces]]
binding = "CACHE"
id = "cache_default"

[[d1_databases]]
binding = "DB"
database_name = "default"
database_id = "default"

[ai]
binding = "AI" // <-- Here

Then, I can access to the AI binding with the following code:

globalThis.__env__['AI']

With a console.log(globalThis) we got:

 __env__:
   { AI: ProxyStub { name: 'Ai', poisoned: false },
     DB: ProxyStub { name: 'D1Database', poisoned: false },
     KV: ProxyStub { name: 'KvNamespace', poisoned: false },
     CACHE: ProxyStub { name: 'KvNamespace', poisoned: false },
     BLOB: ProxyStub { name: 'R2Bucket', poisoned: false } },

Without the custom config, the AI binding disapears:

  __env__:
   { DB: ProxyStub { name: 'D1Database', poisoned: false },
     KV: ProxyStub { name: 'KvNamespace', poisoned: false },
     CACHE: ProxyStub { name: 'KvNamespace', poisoned: false },
     BLOB: ProxyStub { name: 'R2Bucket', poisoned: false } },

related to #84

@atinux
Copy link
Contributor

atinux commented May 21, 2024

Thank you for the PR and idea, your solution works well in development but we also need to think about production too.

@pi0 and I discussed and he worked on generating the wrangler.toml in Nitro directly for production, but still very alpha stage, see nitrojs/nitro#2353

I think we should instead start by supporting reading the rootDir wrangler.toml and merge it into .data/hub/wrangler.toml instead.

Then for production, in the modules:done hook, we pull the config from NuxtHub and add it to nuxt.options.nitro.cloudflare.wrangler so Nitro can merge with the local wrangler.toml

This is currently only in the Nitro nightly build ⚠️

Copy link
Contributor

atinux commented May 22, 2024

Happy to update your PR to read from the local wrangler.toml @Barbapapazes ?

@Barbapapazes
Copy link
Contributor Author

I think we should instead start by supporting reading the rootDir wrangler.toml and merge it into .data/hub/wrangler.toml instead.

I've just updated the PR to support that.

Then for production, in the modules:done hook, we pull the config from NuxtHub and add it to nuxt.options.nitro.cloudflare.wrangler so Nitro can merge with the local wrangler.toml

Is this for another PR?

@Barbapapazes Barbapapazes changed the title feat: support custom wrangler config feat: support local wrangler.toml May 24, 2024
@atinux
Copy link
Contributor

atinux commented May 31, 2024

Is this for another PR?

I would keep this PR as pending until we can also have the production wrangler generation, otherwise it will break for production once CF will detect a wrangler.toml (it won't take the storage bindings into account)

@RihanArfan RihanArfan mentioned this pull request Jun 18, 2024
@atinux atinux mentioned this pull request Jun 28, 2024
@Barbapapazes Barbapapazes marked this pull request as draft September 9, 2024 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants