-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
284 lines (264 loc) · 9.48 KB
/
Copy pathvite.config.ts
File metadata and controls
284 lines (264 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { cloudflare, type WorkerConfig } from "@cloudflare/vite-plugin";
import path from "path";
import fs from "node:fs";
function extractEnvRoutes(
content: string,
env: "preview" | "production",
): NonNullable<WorkerConfig["routes"]> | undefined {
const envTag = env === "preview" ? "[env.preview]" : "[env.production]";
const varsTag = env === "preview" ? "[env.preview.vars]" : "[env.production.vars]";
const start = content.indexOf(envTag);
if (start === -1) return undefined;
const afterHeader = content.slice(start + envTag.length);
const varsIdx = afterHeader.indexOf(varsTag);
const block = varsIdx === -1 ? afterHeader : afterHeader.slice(0, varsIdx);
const routesMatch = block.match(/routes\s*=\s*\[([\s\S]*?)\]/);
if (!routesMatch) return undefined;
const inner = routesMatch[1];
const routes: NonNullable<WorkerConfig["routes"]> = [];
const entryRe = /\{\s*pattern\s*=\s*"([^"]+)"\s*,\s*custom_domain\s*=\s*(true|false)\s*\}/g;
let m: RegExpExecArray | null;
while ((m = entryRe.exec(inner)) !== null) {
routes.push({ pattern: m[1], custom_domain: m[2] === "true" });
}
return routes.length ? routes : undefined;
}
function extractKvRateId(content: string, env: "preview" | "production"): string | undefined {
const envTag = `[[env.${env}.kv_namespaces]]`;
if (!content.includes(envTag)) return undefined;
const re = new RegExp(
`\\[\\[env\\.${env}\\.kv_namespaces\\]\\][\\s\\S]*?binding\\s*=\\s*"KV_RATE"[\\s\\S]*?id\\s*=\\s*"([^"]+)"`,
);
return content.match(re)?.[1];
}
function extractEnvVars(
content: string,
env: "preview" | "production",
): Record<string, string> | undefined {
const header = `[env.${env}.vars]`;
const start = content.indexOf(header);
if (start === -1) return undefined;
const after = content.slice(start + header.length);
const nextHeader = after.search(/\n\[/);
const block = nextHeader === -1 ? after : after.slice(0, nextHeader);
const vars: Record<string, string> = {};
const re = /^[ \t]*([A-Z_][A-Z0-9_]*)\s*=\s*"([^"]*)"\s*$/gm;
let m: RegExpExecArray | null;
while ((m = re.exec(block)) !== null) {
vars[m[1]] = m[2];
}
return Object.keys(vars).length ? vars : undefined;
}
function extractCrons(
content: string,
env: "preview" | "production",
): string[] | undefined {
const header = `[env.${env}.triggers]`;
const start = content.indexOf(header);
if (start === -1) return undefined;
const after = content.slice(start + header.length);
const nextHeader = after.search(/\n\[/);
const block = nextHeader === -1 ? after : after.slice(0, nextHeader);
const cronsMatch = block.match(/crons\s*=\s*\[([\s\S]*?)\]/);
if (!cronsMatch) return undefined;
const items: string[] = [];
const itemRe = /"([^"]+)"/g;
let m: RegExpExecArray | null;
while ((m = itemRe.exec(cronsMatch[1])) !== null) {
items.push(m[1]);
}
return items.length ? items : undefined;
}
function extractR2Buckets(
content: string,
env: "preview" | "production",
): NonNullable<WorkerConfig["r2_buckets"]> | undefined {
const tag = `[[env.${env}.r2_buckets]]`;
const buckets: NonNullable<WorkerConfig["r2_buckets"]> = [];
let cursor = 0;
while (true) {
const start = content.indexOf(tag, cursor);
if (start === -1) break;
const after = content.slice(start + tag.length);
const nextHeader = after.search(/\n\[/);
const block = nextHeader === -1 ? after : after.slice(0, nextHeader);
const binding = block.match(/binding\s*=\s*"([^"]+)"/)?.[1];
const bucketName = block.match(/bucket_name\s*=\s*"([^"]+)"/)?.[1];
if (binding && bucketName) {
buckets.push({ binding, bucket_name: bucketName });
}
cursor = start + tag.length + (nextHeader === -1 ? after.length : nextHeader);
}
return buckets.length ? buckets : undefined;
}
// Parse fields needed for environment-specific deployment from wrangler.toml.
//
// Background: @cloudflare/vite-plugin generates dist/<name>/wrangler.json as a
// flat file (no [env.*] sections). Passing --env to `wrangler deploy` has no
// effect on that generated file, so deployments always use the base config
// (database_id "local"). The fix is to bake the correct per-env values into
// the generated wrangler.json at build time via the plugin's config customizer.
function parseWranglerToml(filePath: string) {
const content = fs.readFileSync(filePath, "utf-8");
const baseName = content.match(/^name\s*=\s*"([^"]+)"/m)?.[1] ?? "app";
const previewDb = {
name:
content.match(/\[\[env\.preview\.d1_databases\]\][^[]*database_name\s*=\s*"([^"]+)"/s)?.[1] ??
`${baseName}-preview`,
id: content.match(/\[\[env\.preview\.d1_databases\]\][^[]*database_id\s*=\s*"([^"]+)"/s)?.[1] ?? "",
};
const productionDb = {
name:
content.match(/\[\[env\.production\.d1_databases\]\][^[]*database_name\s*=\s*"([^"]+)"/s)?.[1] ??
baseName,
id: content.match(/\[\[env\.production\.d1_databases\]\][^[]*database_id\s*=\s*"([^"]+)"/s)?.[1] ?? "",
};
const previewEmailSender =
content.match(/\[\[env\.preview\.send_email\]\][^[]*allowed_sender_addresses\s*=\s*\[\s*"([^"]+)"/s)?.[1] ??
content.match(/\[\[send_email\]\][^[]*allowed_sender_addresses\s*=\s*\[\s*"([^"]+)"/s)?.[1] ??
"noreply@example.com";
const productionEmailSender =
content.match(/\[\[env\.production\.send_email\]\][^[]*allowed_sender_addresses\s*=\s*\[\s*"([^"]+)"/s)?.[1] ??
content.match(/\[\[send_email\]\][^[]*allowed_sender_addresses\s*=\s*\[\s*"([^"]+)"/s)?.[1] ??
"noreply@example.com";
const previewRoutes = extractEnvRoutes(content, "preview");
const productionRoutes = extractEnvRoutes(content, "production");
const previewKvRateId = extractKvRateId(content, "preview");
const productionKvRateId = extractKvRateId(content, "production");
const previewVars = extractEnvVars(content, "preview");
const productionVars = extractEnvVars(content, "production");
const previewCrons = extractCrons(content, "preview");
const productionCrons = extractCrons(content, "production");
const previewR2Buckets = extractR2Buckets(content, "preview");
const productionR2Buckets = extractR2Buckets(content, "production");
return {
baseName,
previewDb,
productionDb,
previewEmailSender,
productionEmailSender,
previewRoutes,
productionRoutes,
previewKvRateId,
productionKvRateId,
previewVars,
productionVars,
previewCrons,
productionCrons,
previewR2Buckets,
productionR2Buckets,
};
}
export default defineConfig(({ mode }) => {
const {
baseName,
previewDb,
productionDb,
previewEmailSender,
productionEmailSender,
previewRoutes,
productionRoutes,
previewKvRateId,
productionKvRateId,
previewVars,
productionVars,
previewCrons,
productionCrons,
previewR2Buckets,
productionR2Buckets,
} = parseWranglerToml(path.resolve(__dirname, "wrangler.toml"));
// Use the mutating function form (returning void) so that array fields like
// d1_databases are replaced rather than appended, avoiding duplicate entries.
//
// IMPORTANT: every per-env field that exists under `[env.<env>.*]` in
// `wrangler.toml` must be re-injected here. The plugin generates a flat
// `dist/<name>/wrangler.json` (no `[env.*]` sections), so anything the
// customizer omits is silently dropped from the deploy. Common gotchas:
// - `triggers.crons` → cron schedules disappear (recovery + GC stop firing).
// - `[env.*.vars]` extras → env-only Worker vars are missing.
// - `[[env.*.r2_buckets]]` → wrong bucket binding ends up on remote envs.
const configCustomizer =
mode === "preview"
? (cfg: WorkerConfig): void => {
cfg.name = `${baseName}-preview`;
cfg.vars = previewVars ?? { ENVIRONMENT: "preview", EMAIL_PROVIDER: "cloudflare" };
cfg.d1_databases = [
{
binding: "DB",
database_name: previewDb.name,
database_id: previewDb.id,
},
];
cfg.send_email = [
{
name: "SEND_EMAIL",
allowed_sender_addresses: [previewEmailSender],
},
];
if (previewRoutes?.length) {
cfg.routes = previewRoutes;
}
if (previewKvRateId) {
cfg.kv_namespaces = [{ binding: "KV_RATE", id: previewKvRateId }];
}
if (previewR2Buckets?.length) {
cfg.r2_buckets = previewR2Buckets;
}
if (previewCrons?.length) {
cfg.triggers = { crons: previewCrons };
}
}
: mode === "production"
? (cfg: WorkerConfig): void => {
cfg.name = `${baseName}-production`;
cfg.vars = productionVars ?? { ENVIRONMENT: "production", EMAIL_PROVIDER: "cloudflare" };
cfg.d1_databases = [
{
binding: "DB",
database_name: productionDb.name,
database_id: productionDb.id,
},
];
cfg.send_email = [
{
name: "SEND_EMAIL",
allowed_sender_addresses: [productionEmailSender],
},
];
if (productionRoutes?.length) {
cfg.routes = productionRoutes;
}
if (productionKvRateId) {
cfg.kv_namespaces = [{ binding: "KV_RATE", id: productionKvRateId }];
}
if (productionR2Buckets?.length) {
cfg.r2_buckets = productionR2Buckets;
}
if (productionCrons?.length) {
cfg.triggers = { crons: productionCrons };
}
}
: undefined;
return {
plugins: [
react(),
cloudflare({
remoteBindings: false,
...(configCustomizer ? { config: configCustomizer } : {}),
}),
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src/react-app"),
},
},
// Avoid ELOOP when `.claude/AGENTS.md` is a broken self-symlink (Cursor/Claude hook).
server: {
watch: {
ignored: [path.resolve(__dirname, ".claude")],
},
},
};
});