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

Support wrangler.json configuration file in adapter-cloudflare-workers #13121

Closed
linnil1 opened this issue Dec 8, 2024 · 1 comment · Fixed by #13148
Closed

Support wrangler.json configuration file in adapter-cloudflare-workers #13121

linnil1 opened this issue Dec 8, 2024 · 1 comment · Fixed by #13148

Comments

@linnil1
Copy link
Contributor

linnil1 commented Dec 8, 2024

Describe the problem

Currently, adapter-cloudflare-workers(2.6.0) only supports wrangler.toml as the configuration file for Cloudflare Workers. If there is only a wrangler.json file in the project, running npm run build will fail because wrangler.toml does not exist. Even if the adapter is configured as:

adapter: adapter({
    config: 'wrangler.json'
})

It does not work because the file is always read using the TOML format.

However, starting from version v3.91.0, Cloudflare Workers also supports wrangler.json as a configuration format. When using wrangler, it automatically searches for wrangler.json if wrangler.toml is not present in the base directory.

For projects with only a wrangler.json file, users are still forced to convert their JSON configuration to TOML, which is tedious and unnecessary.

Describe the proposed solution

The simplest solution is to check the file extension to support both wrangler.toml and wrangler.json. For example:

diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js
index 8a4f9252d..8bce9848b 100644
--- a/packages/adapter-cloudflare-workers/index.js
+++ b/packages/adapter-cloudflare-workers/index.js
function validate_config(builder, config_file) {
    let wrangler_config;

    try {
+       if (config_file.endsWith(".json")) {
+           wrangler_config = JSON.parse(readFileSync(config_file, 'utf-8'));
+       } else {
            wrangler_config = /** @type {WranglerConfig} */ (
                toml.parse(readFileSync(config_file, 'utf-8'))
            );
+       }
    } catch (err) {
        err.message = `Error parsing ${config_file}: ${err.message}`;
        throw err;
    }
    // rest of the function
}

Additionally, the adapter could automatically detect wrangler.json if wrangler.toml is not specified in the adapter's options or does not exist. This can be achieved by modifying the logic to include:

 function validate_config(builder, config_file) {
+       if (!existsSync(config_file)) {
+            config_file = "wrangler.json"
+       }
        if (existsSync(config_file)) {

Alternatives considered

No response

Importance

nice to have

Additional Information

@benmccann
Copy link
Member

Please feel free to send a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants