Skip to content

Commit a65b4cc

Browse files
committed
fix(dev): JSONC-aware parse of wrangler configs in dev.ts
pnpm dev did a raw JSON.parse on the worker wrangler.jsonc files, which choke on the // comments the gateway config gained in 01339c3 (Polar billing). Add a string-safe stripJsonc (line/block comments + trailing commas) so the dev server starts. Pre-existing breakage surfaced when restarting the dev server.
1 parent 9bbde5f commit a65b4cc

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

scripts/dev.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,24 @@ function parseArgs(argv: string[]): DevOptions {
226226
return options;
227227
}
228228

229+
/** Matches a JSON string literal (group 1), a `//` line comment, or a `/* *​/` block comment. */
230+
const JSONC_TOKEN = /("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g;
231+
232+
/**
233+
* Strip comments + trailing commas from a JSONC source so the wrangler `.jsonc` configs
234+
* (which legitimately contain comments) can be `JSON.parse`d. String-aware: the alternation
235+
* matches whole string literals first and the replacer keeps them, so comment-like
236+
* sequences inside strings are never touched.
237+
*/
238+
function stripJsonc(input: string): string {
239+
return input
240+
.replace(JSONC_TOKEN, (_match, stringLiteral?: string) => stringLiteral ?? "")
241+
.replace(/,(\s*[}\]])/g, "$1");
242+
}
243+
229244
function createLocalWorkerConfig(configPath: string): string {
230245
const absolutePath = resolve(GATEWAY_WORKER_DIR, configPath);
231-
const parsed = JSON.parse(readFileSync(absolutePath, "utf8")) as unknown;
246+
const parsed = JSON.parse(stripJsonc(readFileSync(absolutePath, "utf8"))) as unknown;
232247
if (!isRecord(parsed)) {
233248
throw new Error(`${configPath} must parse to a JSON object.`);
234249
}

0 commit comments

Comments
 (0)