Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/presets/cloudflare/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ export async function enableNodeCompat(nitro: Nitro) {
}
}

// Wrangler accepts comments and trailing commas in both JSON formats
const parseWranglerJSON = (text: string) => parseJSONC(text, { allowTrailingComma: true });

const extensionParsers = {
".json": parseJSONC,
".jsonc": parseJSONC,
".json": parseWranglerJSON,
".jsonc": parseWranglerJSON,
".toml": parseTOML,
} as const;

Expand Down
17 changes: 17 additions & 0 deletions test/unit/cloudflare.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ describe("writeWranglerConfig (cloudflare-module)", () => {
expect(config.rules).toBeUndefined();
});

it("reads a wrangler.jsonc with comments and trailing commas", async () => {
const { root, nitro } = createNitroStub();
cleanup.push(root);
writeFileSync(
join(root, "wrangler.jsonc"),
`{
// Worker name
"name": "my-worker",
"vars": { "A": "1", },
}`
);
await writeWranglerConfig(nitro, "module");
const config = JSON.parse(readFileSync(join(root, ".output/server/wrangler.json"), "utf8"));
expect(config.name).toBe("my-worker");
expect(config.vars).toEqual({ A: "1" });
});

it.each([
["2024-09-22", []],
["2024-09-23", ["nodejs_compat"]],
Expand Down
Loading