Skip to content

Commit

Permalink
feat(cloudflare-pages): generate wrangler.toml (#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 10, 2024
1 parent 2de822b commit f75f042
Show file tree
Hide file tree
Showing 7 changed files with 1,160 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"chalk": "^5.3.0",
"chokidar": "^3.6.0",
"citty": "^0.1.6",
"confbox": "^0.1.6",
"consola": "^3.2.3",
"cookie-es": "^1.1.0",
"croner": "^8.0.2",
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/presets/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
withTrailingSlash,
withoutLeadingSlash,
} from "ufo";
import { parseTOML, stringifyTOML } from "confbox";
import { globby } from "globby";
import { defineNitroPreset } from "../preset";
import type { Nitro } from "../types";
import { CloudflarePagesRoutes } from "../types/presets/cloudflare";
import defu from "defu";

export const cloudflarePages = defineNitroPreset({
extends: "cloudflare",
Expand Down Expand Up @@ -45,6 +47,7 @@ export const cloudflarePages = defineNitroPreset({
await writeCFRoutes(nitro);
await writeCFPagesHeaders(nitro);
await writeCFPagesRedirects(nitro);
await writeCFWrangler(nitro);
},
},
});
Expand Down Expand Up @@ -214,3 +217,26 @@ async function writeCFPagesRedirects(nitro: Nitro) {

await fsp.writeFile(redirectsPath, contents.join("\n"));
}

async function writeCFWrangler(nitro: Nitro) {
type WranglerConfig = typeof nitro.options.cloudflare.wrangler;

const inlineConfig: WranglerConfig =
nitro.options.cloudflare?.wrangler || ({} as WranglerConfig);

let configFromFile: WranglerConfig = {} as WranglerConfig;
const configPath = resolve(
nitro.options.rootDir,
inlineConfig.configPath || "wrangler.toml"
);
if (existsSync(configPath)) {
configFromFile = parseTOML<WranglerConfig>(
await fsp.readFile(configPath, "utf8")
);
}

const wranglerConfig: WranglerConfig = defu(configFromFile, inlineConfig);

const wranglerPath = join(nitro.options.output.publicDir, "wrangler.toml");
await fsp.writeFile(wranglerPath, stringifyTOML(wranglerConfig));
}
7 changes: 7 additions & 0 deletions src/types/presets/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Config as WranglerConfig } from "./cloudflare.wrangler";

/**
* https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes
*/
Expand All @@ -13,6 +15,11 @@ export interface CloudflarePagesRoutes {
}

export interface CloudflareOptions {
/**
* Configuration for the Cloudflare Deployments
*/
wrangler?: WranglerConfig;

pages: {
/**
* Nitro will automatically generate a `_routes.json` that controls which files get served statically and
Expand Down
Loading

0 comments on commit f75f042

Please sign in to comment.