devflare 1.0.0-next.11 → 1.0.0-next.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LLM.md +67 -73
  2. package/README.md +11 -9
  3. package/dist/{build-ezksv2dd.js → build-e6kgjwr8.js} +39 -10
  4. package/dist/bundler/do-bundler.d.ts.map +1 -1
  5. package/dist/bundler/index.d.ts +1 -0
  6. package/dist/bundler/index.d.ts.map +1 -1
  7. package/dist/bundler/worker-bundler.d.ts +14 -0
  8. package/dist/bundler/worker-bundler.d.ts.map +1 -0
  9. package/dist/cli/commands/build.d.ts.map +1 -1
  10. package/dist/cli/commands/deploy.d.ts.map +1 -1
  11. package/dist/cli/commands/dev.d.ts.map +1 -1
  12. package/dist/config/compiler.d.ts.map +1 -1
  13. package/dist/config/index.d.ts +1 -0
  14. package/dist/config/index.d.ts.map +1 -1
  15. package/dist/config/resolve.d.ts +3 -0
  16. package/dist/config/resolve.d.ts.map +1 -0
  17. package/dist/{deploy-jdpy21t6.js → deploy-eeaqwxaa.js} +39 -10
  18. package/dist/{dev-9mq7zhww.js → dev-mqsffeeb.js} +64 -461
  19. package/dist/dev-server/server.d.ts.map +1 -1
  20. package/dist/{index-xqfbd9fx.js → index-8x16kn47.js} +4 -4
  21. package/dist/index-nb0bqtx7.js +625 -0
  22. package/dist/{index-51s1hkw4.js → index-rfhx0yd5.js} +10 -6
  23. package/dist/index-zbvmtcn2.js +795 -0
  24. package/dist/src/browser.js +1 -1
  25. package/dist/src/cli/index.js +1 -1
  26. package/dist/src/index.js +10 -11
  27. package/dist/src/sveltekit/index.js +2 -3
  28. package/dist/src/test/index.js +4 -5
  29. package/dist/src/vite/index.js +19 -399
  30. package/dist/{types-nq5acrwh.js → types-sffr9681.js} +5 -6
  31. package/dist/vite/config-file.d.ts +25 -0
  32. package/dist/vite/config-file.d.ts.map +1 -0
  33. package/dist/vite/index.d.ts +1 -0
  34. package/dist/vite/index.d.ts.map +1 -1
  35. package/dist/worker-entry/composed-worker.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/dist/index-k7r18na8.js +0 -0
  38. package/dist/index-ws68xvq2.js +0 -311
  39. package/dist/{index-53xcakh8.js → index-0kzg8wed.js} +3 -3
@@ -0,0 +1,795 @@
1
+ import {
2
+ discoverRoutes
3
+ } from "./index-1p814k7s.js";
4
+ import {
5
+ DEFAULT_DO_PATTERN,
6
+ findFiles
7
+ } from "./index-rbht7m9r.js";
8
+ import {
9
+ findDurableObjectClasses
10
+ } from "./index-9wt9x09k.js";
11
+ import {
12
+ compileConfig,
13
+ compileToProgrammaticConfig,
14
+ resolveConfigForEnvironment,
15
+ writeWranglerConfig
16
+ } from "./index-rfhx0yd5.js";
17
+ import {
18
+ loadConfig,
19
+ resolveConfigPath
20
+ } from "./index-wyf3s77s.js";
21
+ import {
22
+ __require
23
+ } from "./index-37x76zdn.js";
24
+
25
+ // src/vite/plugin.ts
26
+ import { isAbsolute, relative as relative2, resolve as resolve2 } from "pathe";
27
+
28
+ // src/worker-entry/composed-worker.ts
29
+ import { dirname, relative, resolve } from "pathe";
30
+ var DEFAULT_FETCH_ENTRY_FILES = [
31
+ "src/fetch.ts",
32
+ "src/fetch.js",
33
+ "src/fetch.mts",
34
+ "src/fetch.mjs"
35
+ ];
36
+ var DEFAULT_QUEUE_ENTRY_FILES = [
37
+ "src/queue.ts",
38
+ "src/queue.js",
39
+ "src/queue.mts",
40
+ "src/queue.mjs"
41
+ ];
42
+ var DEFAULT_SCHEDULED_ENTRY_FILES = [
43
+ "src/scheduled.ts",
44
+ "src/scheduled.js",
45
+ "src/scheduled.mts",
46
+ "src/scheduled.mjs"
47
+ ];
48
+ var DEFAULT_EMAIL_ENTRY_FILES = [
49
+ "src/email.ts",
50
+ "src/email.js",
51
+ "src/email.mts",
52
+ "src/email.mjs"
53
+ ];
54
+ async function resolveWorkerHandlerPath(cwd, configuredPath, defaultEntries) {
55
+ if (configuredPath === false) {
56
+ return null;
57
+ }
58
+ const fs = await import("node:fs/promises");
59
+ const candidates = new Set;
60
+ if (typeof configuredPath === "string" && configuredPath) {
61
+ candidates.add(configuredPath);
62
+ }
63
+ for (const defaultEntry of defaultEntries) {
64
+ candidates.add(defaultEntry);
65
+ }
66
+ for (const candidate of candidates) {
67
+ const absolutePath = resolve(cwd, candidate);
68
+ try {
69
+ await fs.access(absolutePath);
70
+ return absolutePath;
71
+ } catch {
72
+ continue;
73
+ }
74
+ }
75
+ return null;
76
+ }
77
+ async function resolveWorkerSurfacePaths(cwd, config) {
78
+ return {
79
+ fetch: await resolveWorkerHandlerPath(cwd, config.files?.fetch, DEFAULT_FETCH_ENTRY_FILES),
80
+ queue: await resolveWorkerHandlerPath(cwd, config.files?.queue, DEFAULT_QUEUE_ENTRY_FILES),
81
+ scheduled: await resolveWorkerHandlerPath(cwd, config.files?.scheduled, DEFAULT_SCHEDULED_ENTRY_FILES),
82
+ email: await resolveWorkerHandlerPath(cwd, config.files?.email, DEFAULT_EMAIL_ENTRY_FILES)
83
+ };
84
+ }
85
+ function toImportSpecifier(fromFilePath, toFilePath) {
86
+ const specifier = relative(dirname(fromFilePath), toFilePath).replace(/\\/g, "/");
87
+ return specifier.startsWith(".") ? specifier : `./${specifier}`;
88
+ }
89
+ function createGeneratedRouteModuleImports(entryPath, routeDiscovery) {
90
+ if (!routeDiscovery) {
91
+ return [];
92
+ }
93
+ return routeDiscovery.routes.map((route, index) => ({
94
+ identifier: `__devflareRouteModule${index}`,
95
+ importPath: toImportSpecifier(entryPath, route.absolutePath),
96
+ filePath: route.filePath,
97
+ routePath: route.routePath,
98
+ segmentsJson: JSON.stringify(route.segments)
99
+ }));
100
+ }
101
+ function needsComposedWorkerEntrypoint(surfacePaths, routeDiscovery) {
102
+ return Boolean(surfacePaths.fetch || surfacePaths.queue || surfacePaths.scheduled || surfacePaths.email || routeDiscovery?.routes.length);
103
+ }
104
+ function getComposedWorkerEntrypointSource(surfaceImportPaths, configuredLocalSendEmailBindings = {}, routeImports = [], options = {}) {
105
+ const importLines = [`import { createEmailEvent, createFetchEvent, createQueueEvent, createRouteResolve, createScheduledEvent, invokeFetchModule, matchFetchRoute, runWithEventContext, setLocalSendEmailBindings } from 'devflare/runtime'`];
106
+ const moduleFallbackLines = [];
107
+ const localSendEmailBindings = JSON.stringify(configuredLocalSendEmailBindings);
108
+ const routeManifestEntries = routeImports.map(({ identifier, filePath, routePath, segmentsJson }) => {
109
+ return ` { filePath: ${JSON.stringify(filePath)}, routePath: ${JSON.stringify(routePath)}, segments: ${segmentsJson}, module: ${identifier} }`;
110
+ });
111
+ const registerSurfaceModule = (identifier, importPath) => {
112
+ if (importPath) {
113
+ importLines.push(`import * as ${identifier} from '${importPath}'`);
114
+ return;
115
+ }
116
+ moduleFallbackLines.push(`const ${identifier} = {}`);
117
+ };
118
+ registerSurfaceModule("__devflareFetchModule", surfaceImportPaths.fetch);
119
+ registerSurfaceModule("__devflareQueueModule", surfaceImportPaths.queue);
120
+ registerSurfaceModule("__devflareScheduledModule", surfaceImportPaths.scheduled);
121
+ registerSurfaceModule("__devflareEmailModule", surfaceImportPaths.email);
122
+ for (const routeImport of routeImports) {
123
+ importLines.push(`import * as ${routeImport.identifier} from '${routeImport.importPath}'`);
124
+ }
125
+ const includeDevInternalEmail = options.devInternalEmail === true;
126
+ const devInternalEmailHelpers = includeDevInternalEmail ? `
127
+ function __devflareCreateEmailHeaders(rawBody) {
128
+ const headers = new Headers()
129
+ const lines = rawBody.split(/\\r?\\n/)
130
+
131
+ for (const line of lines) {
132
+ if (line.trim() === '') {
133
+ break
134
+ }
135
+
136
+ const colonIndex = line.indexOf(':')
137
+ if (colonIndex <= 0) {
138
+ continue
139
+ }
140
+
141
+ headers.append(line.slice(0, colonIndex).trim(), line.slice(colonIndex + 1).trim())
142
+ }
143
+
144
+ return headers
145
+ }
146
+
147
+ function __devflareCreateEmailRawStream(rawBody) {
148
+ return new ReadableStream({
149
+ start(controller) {
150
+ controller.enqueue(new TextEncoder().encode(rawBody))
151
+ controller.close()
152
+ }
153
+ })
154
+ }
155
+
156
+ async function __devflareHandleInternalEmail(request, env, ctx) {
157
+ if (!__devflareEmailHandler) {
158
+ return new Response('Email handler not configured', { status: 501 })
159
+ }
160
+
161
+ const from = request.headers.get('x-devflare-email-from') || 'unknown@example.com'
162
+ const to = request.headers.get('x-devflare-email-to') || 'worker@example.com'
163
+ const rawBody = await request.text()
164
+ const emailMessage = {
165
+ from,
166
+ to,
167
+ headers: __devflareCreateEmailHeaders(rawBody),
168
+ raw: __devflareCreateEmailRawStream(rawBody),
169
+ rawSize: rawBody.length,
170
+ setReject(reason) {
171
+ console.warn('[Devflare email rejected]', reason)
172
+ },
173
+ async forward(rcptTo) {
174
+ console.log('[Devflare email forwarded]', rcptTo)
175
+ return Promise.resolve()
176
+ },
177
+ async reply(message) {
178
+ console.log('[Devflare email reply sent]', message?.from)
179
+ return Promise.resolve()
180
+ }
181
+ }
182
+
183
+ const __devflareEvent = createEmailEvent(emailMessage, env, ctx)
184
+
185
+ await runWithEventContext(
186
+ __devflareEvent,
187
+ () => __devflareEmailHandler(__devflareEvent, env, ctx)
188
+ )
189
+
190
+ return new Response(JSON.stringify({ ok: true, from, to }), {
191
+ headers: { 'Content-Type': 'application/json' }
192
+ })
193
+ }
194
+ ` : "";
195
+ return `
196
+ ${importLines.join(`
197
+ `)}
198
+ ${moduleFallbackLines.join(`
199
+ `)}
200
+
201
+ setLocalSendEmailBindings(${localSendEmailBindings})
202
+
203
+ const __devflareHasFetchModule = ${surfaceImportPaths.fetch ? "true" : "false"}
204
+ const __devflareRoutes = [
205
+ ${routeManifestEntries.join(`,
206
+ `)}
207
+ ]
208
+ const __devflareHasRoutes = __devflareRoutes.length > 0
209
+
210
+ const __devflareResolveHandler = (module, namedExport) => {
211
+ const defaultExport = module.default
212
+
213
+ if (typeof defaultExport === 'function') {
214
+ return defaultExport
215
+ }
216
+
217
+ if (defaultExport && typeof defaultExport[namedExport] === 'function') {
218
+ return defaultExport[namedExport].bind(defaultExport)
219
+ }
220
+
221
+ if (typeof module[namedExport] === 'function') {
222
+ return module[namedExport]
223
+ }
224
+
225
+ return null
226
+ }
227
+
228
+ const __devflareQueueHandler = __devflareResolveHandler(__devflareQueueModule, 'queue')
229
+ const __devflareScheduledHandler = __devflareResolveHandler(__devflareScheduledModule, 'scheduled')
230
+ const __devflareEmailHandler = __devflareResolveHandler(__devflareEmailModule, 'email')
231
+ ${devInternalEmailHelpers}
232
+
233
+ export default {
234
+ ...(${surfaceImportPaths.fetch || routeImports.length > 0 || includeDevInternalEmail ? "true" : "false"}
235
+ ? {
236
+ async fetch(request, env, ctx) {
237
+ ${includeDevInternalEmail ? `const url = new URL(request.url)
238
+
239
+ if (
240
+ request.headers.get('x-devflare-event') === 'email'
241
+ && url.pathname === '/_devflare/internal/email'
242
+ ) {
243
+ return __devflareHandleInternalEmail(request, env, ctx)
244
+ }
245
+
246
+ ` : ""}const __devflareInitialRouteMatch = __devflareHasRoutes ? matchFetchRoute(__devflareRoutes, request) : null
247
+ const __devflareEvent = createFetchEvent(request, env, ctx, {
248
+ params: __devflareInitialRouteMatch?.params ?? {}
249
+ })
250
+ return runWithEventContext(
251
+ __devflareEvent,
252
+ () => invokeFetchModule(
253
+ __devflareFetchModule,
254
+ __devflareEvent,
255
+ __devflareHasRoutes
256
+ ? createRouteResolve(__devflareRoutes, __devflareEvent)
257
+ : undefined
258
+ )
259
+ )
260
+ }
261
+ }
262
+ : {}),
263
+ ...(__devflareQueueHandler
264
+ ? {
265
+ async queue(batch, env, ctx) {
266
+ const __devflareEvent = createQueueEvent(batch, env, ctx)
267
+ return runWithEventContext(
268
+ __devflareEvent,
269
+ () => __devflareQueueHandler(__devflareEvent, env, ctx)
270
+ )
271
+ }
272
+ }
273
+ : {}),
274
+ ...(__devflareScheduledHandler
275
+ ? {
276
+ async scheduled(controller, env, ctx) {
277
+ const __devflareEvent = createScheduledEvent(controller, env, ctx)
278
+ return runWithEventContext(
279
+ __devflareEvent,
280
+ () => __devflareScheduledHandler(__devflareEvent, env, ctx)
281
+ )
282
+ }
283
+ }
284
+ : {}),
285
+ ...(__devflareEmailHandler
286
+ ? {
287
+ async email(message, env, ctx) {
288
+ const __devflareEvent = createEmailEvent(message, env, ctx)
289
+ return runWithEventContext(
290
+ __devflareEvent,
291
+ () => __devflareEmailHandler(__devflareEvent, env, ctx)
292
+ )
293
+ }
294
+ }
295
+ : {})
296
+ }
297
+ `.trimStart();
298
+ }
299
+ async function prepareComposedWorkerEntrypoint(cwd, config, environment, options = {}) {
300
+ const resolvedConfig = resolveConfigForEnvironment(config, environment);
301
+ if (resolvedConfig.wrangler?.passthrough && Object.prototype.hasOwnProperty.call(resolvedConfig.wrangler.passthrough, "main")) {
302
+ return null;
303
+ }
304
+ const surfacePaths = await resolveWorkerSurfacePaths(cwd, resolvedConfig);
305
+ const routeDiscovery = await discoverRoutes(cwd, resolvedConfig);
306
+ if (!needsComposedWorkerEntrypoint(surfacePaths, routeDiscovery)) {
307
+ return null;
308
+ }
309
+ const fs = await import("node:fs/promises");
310
+ const entryDir = resolve(cwd, ".devflare", "worker-entrypoints");
311
+ const entryPath = resolve(entryDir, "main.ts");
312
+ await fs.mkdir(entryDir, { recursive: true });
313
+ const surfaceImportPaths = {
314
+ fetch: surfacePaths.fetch ? toImportSpecifier(entryPath, surfacePaths.fetch) : null,
315
+ queue: surfacePaths.queue ? toImportSpecifier(entryPath, surfacePaths.queue) : null,
316
+ scheduled: surfacePaths.scheduled ? toImportSpecifier(entryPath, surfacePaths.scheduled) : null,
317
+ email: surfacePaths.email ? toImportSpecifier(entryPath, surfacePaths.email) : null
318
+ };
319
+ const routeImports = createGeneratedRouteModuleImports(entryPath, routeDiscovery);
320
+ await fs.writeFile(entryPath, getComposedWorkerEntrypointSource(surfaceImportPaths, resolvedConfig.bindings?.sendEmail ?? {}, routeImports, options));
321
+ return ".devflare/worker-entrypoints/main.ts";
322
+ }
323
+
324
+ // src/vite/plugin.ts
325
+ var CONFIG_DIR = ".devflare";
326
+ var VIRTUAL_DO_ENTRY = "virtual:devflare-do-entry";
327
+ var RESOLVED_VIRTUAL_DO_ENTRY = "\x00" + VIRTUAL_DO_ENTRY;
328
+ var pluginContext = {
329
+ wranglerConfig: null,
330
+ cloudflareConfig: null,
331
+ projectRoot: process.cwd(),
332
+ auxiliaryWorkerConfig: null,
333
+ durableObjects: null
334
+ };
335
+ function getPluginContext() {
336
+ return pluginContext;
337
+ }
338
+ async function discoverDurableObjects(projectRoot, pattern, workerName) {
339
+ const files = new Map;
340
+ const matchedFiles = await findFiles(pattern, { cwd: projectRoot });
341
+ const fs = await import("node:fs/promises");
342
+ for (const filePath of matchedFiles) {
343
+ try {
344
+ const code = await fs.readFile(filePath, "utf-8");
345
+ const classNames = findDurableObjectClasses(code);
346
+ if (classNames.length > 0) {
347
+ files.set(filePath, classNames);
348
+ }
349
+ } catch (error) {
350
+ console.warn(`[devflare] Failed to read DO file: ${filePath}`, error);
351
+ }
352
+ }
353
+ return { files, workerName };
354
+ }
355
+ function generateVirtualDOEntry(discovery) {
356
+ const lines = [
357
+ "// Auto-generated by devflare — DO entry module",
358
+ "// Re-exports all Durable Object classes discovered from files.durableObjects pattern",
359
+ ""
360
+ ];
361
+ for (const [filePath, classNames] of discovery.files) {
362
+ const normalizedPath = filePath.replace(/\\/g, "/");
363
+ lines.push(`export { ${classNames.join(", ")} } from '${normalizedPath}'`);
364
+ }
365
+ lines.push("");
366
+ lines.push("// Default fetch handler for DO worker");
367
+ lines.push("export default {");
368
+ lines.push("\tasync fetch(request: Request): Promise<Response> {");
369
+ lines.push('\t\treturn new Response("Devflare DO Worker", { status: 200 })');
370
+ lines.push("\t}");
371
+ lines.push("}");
372
+ return lines.join(`
373
+ `);
374
+ }
375
+ function createAuxiliaryWorkerConfig(wranglerConfig, discovery) {
376
+ const doBindings = wranglerConfig.durable_objects?.bindings?.map((binding) => ({
377
+ name: binding.name,
378
+ class_name: binding.class_name
379
+ })) ?? [];
380
+ return {
381
+ config: {
382
+ name: discovery.workerName,
383
+ main: VIRTUAL_DO_ENTRY,
384
+ compatibility_date: wranglerConfig.compatibility_date,
385
+ compatibility_flags: wranglerConfig.compatibility_flags,
386
+ durable_objects: { bindings: doBindings },
387
+ migrations: wranglerConfig.migrations,
388
+ kv_namespaces: wranglerConfig.kv_namespaces,
389
+ d1_databases: wranglerConfig.d1_databases,
390
+ r2_buckets: wranglerConfig.r2_buckets,
391
+ browser: wranglerConfig.browser
392
+ }
393
+ };
394
+ }
395
+ function rebaseMainPathForConfigDir(projectRoot, configDir, mainEntry) {
396
+ if (!mainEntry) {
397
+ return mainEntry;
398
+ }
399
+ const absoluteMainPath = isAbsolute(mainEntry) ? mainEntry : resolve2(projectRoot, mainEntry);
400
+ return relative2(configDir, absoluteMainPath).replace(/\\/g, "/");
401
+ }
402
+ function logDiscoveredDurableObjects(projectRoot, discovery) {
403
+ if (!discovery || discovery.files.size === 0) {
404
+ return;
405
+ }
406
+ console.log(`[devflare] Discovered ${discovery.files.size} DO file(s):`);
407
+ for (const [filePath, classes] of discovery.files) {
408
+ console.log(` • ${filePath.replace(projectRoot, ".")} → ${classes.join(", ")}`);
409
+ }
410
+ }
411
+ async function buildPluginContextState(projectRoot, devflareConfig, environment) {
412
+ const wranglerConfig = compileConfig(devflareConfig, environment);
413
+ const cloudflareConfig = compileToProgrammaticConfig(devflareConfig, environment);
414
+ const composedMainEntry = await prepareComposedWorkerEntrypoint(projectRoot, devflareConfig, environment);
415
+ if (composedMainEntry) {
416
+ wranglerConfig.main = composedMainEntry;
417
+ cloudflareConfig.main = composedMainEntry;
418
+ }
419
+ let durableObjects = null;
420
+ let auxiliaryWorkerConfig = null;
421
+ const doPatternConfig = devflareConfig.files?.durableObjects;
422
+ const doPattern = typeof doPatternConfig === "string" ? doPatternConfig : DEFAULT_DO_PATTERN;
423
+ if (doPatternConfig !== false) {
424
+ const doWorkerName = `${wranglerConfig.name}-do`;
425
+ const discovery = await discoverDurableObjects(projectRoot, doPattern, doWorkerName);
426
+ if (discovery.files.size > 0) {
427
+ durableObjects = discovery;
428
+ if (wranglerConfig.durable_objects?.bindings) {
429
+ for (const binding of wranglerConfig.durable_objects.bindings) {
430
+ binding.script_name = doWorkerName;
431
+ }
432
+ }
433
+ if (cloudflareConfig.durable_objects) {
434
+ const doConfig = cloudflareConfig.durable_objects;
435
+ for (const binding of doConfig.bindings) {
436
+ binding.script_name = doWorkerName;
437
+ }
438
+ }
439
+ auxiliaryWorkerConfig = createAuxiliaryWorkerConfig(wranglerConfig, discovery);
440
+ }
441
+ }
442
+ return {
443
+ wranglerConfig,
444
+ cloudflareConfig,
445
+ durableObjects,
446
+ auxiliaryWorkerConfig
447
+ };
448
+ }
449
+ async function ensureGeneratedConfigDir(projectRoot) {
450
+ const configDir = resolve2(projectRoot, CONFIG_DIR);
451
+ const fs = await import("node:fs/promises");
452
+ await fs.mkdir(configDir, { recursive: true });
453
+ const gitignorePath = resolve2(configDir, ".gitignore");
454
+ try {
455
+ await fs.access(gitignorePath);
456
+ } catch {
457
+ await fs.writeFile(gitignorePath, `*
458
+ `, "utf-8");
459
+ }
460
+ return configDir;
461
+ }
462
+ async function writeGeneratedWranglerConfig(projectRoot, wranglerConfig) {
463
+ const configDir = await ensureGeneratedConfigDir(projectRoot);
464
+ const wranglerFileConfig = {
465
+ ...wranglerConfig,
466
+ ...wranglerConfig.main && {
467
+ main: rebaseMainPathForConfigDir(projectRoot, configDir, wranglerConfig.main)
468
+ }
469
+ };
470
+ await writeWranglerConfig(configDir, wranglerFileConfig, "wrangler.jsonc");
471
+ }
472
+ async function resolvePluginConfigPath(projectRoot, configPath) {
473
+ if (configPath) {
474
+ return isAbsolute(configPath) ? configPath : resolve2(projectRoot, configPath);
475
+ }
476
+ return await resolveConfigPath(projectRoot) ?? null;
477
+ }
478
+ function devflarePlugin(options = {}) {
479
+ const {
480
+ configPath,
481
+ environment,
482
+ doTransforms = true,
483
+ watchConfig = true,
484
+ bridgePort = process.env.DEVFLARE_BRIDGE_PORT ? parseInt(process.env.DEVFLARE_BRIDGE_PORT, 10) : undefined,
485
+ wsProxyPatterns = []
486
+ } = options;
487
+ let projectRoot;
488
+ let devflareConfig;
489
+ let resolvedPluginConfigPath = null;
490
+ return {
491
+ name: "devflare",
492
+ enforce: "pre",
493
+ async config(config, { command }) {
494
+ const cwd = config.root ?? process.cwd();
495
+ const returnConfig = {};
496
+ let lfConfig = null;
497
+ try {
498
+ lfConfig = await loadConfig({
499
+ cwd,
500
+ configFile: configPath
501
+ });
502
+ } catch (error) {
503
+ if (command === "build") {
504
+ console.warn("[devflare] Could not load config:", error);
505
+ }
506
+ }
507
+ if (lfConfig) {
508
+ const workerNameValue = lfConfig.name ?? "unknown";
509
+ returnConfig.define = {
510
+ ...config.define ?? {},
511
+ __DEVFLARE_WORKER_NAME__: JSON.stringify(workerNameValue)
512
+ };
513
+ }
514
+ if (command === "serve" && process.env.DEVFLARE_DEV && lfConfig) {
515
+ const port = bridgePort ?? 8787;
516
+ const patterns = [...wsProxyPatterns];
517
+ if (lfConfig.wsRoutes && lfConfig.wsRoutes.length > 0) {
518
+ for (const route of lfConfig.wsRoutes) {
519
+ if (!patterns.includes(route.pattern)) {
520
+ patterns.push(route.pattern);
521
+ }
522
+ }
523
+ }
524
+ const proxyConfig = {};
525
+ for (const pattern of patterns) {
526
+ proxyConfig[pattern] = {
527
+ target: `http://127.0.0.1:${port}`,
528
+ changeOrigin: true,
529
+ ws: true,
530
+ configure: (proxy) => {
531
+ proxy.on("error", (err) => {
532
+ console.error(`[devflare] Proxy error: ${err.message}`);
533
+ });
534
+ }
535
+ };
536
+ }
537
+ if (Object.keys(proxyConfig).length > 0) {
538
+ console.log(`[devflare] WebSocket proxy configured for: ${patterns.join(", ")}`);
539
+ returnConfig.server = {
540
+ proxy: proxyConfig
541
+ };
542
+ }
543
+ }
544
+ return Object.keys(returnConfig).length > 0 ? returnConfig : undefined;
545
+ },
546
+ resolveId(id) {
547
+ if (id === VIRTUAL_DO_ENTRY) {
548
+ return RESOLVED_VIRTUAL_DO_ENTRY;
549
+ }
550
+ return null;
551
+ },
552
+ async load(id) {
553
+ if (id === RESOLVED_VIRTUAL_DO_ENTRY) {
554
+ if (!pluginContext.durableObjects) {
555
+ return `// No Durable Objects configured
556
+ export default { fetch: () => new Response("No DOs") }`;
557
+ }
558
+ return generateVirtualDOEntry(pluginContext.durableObjects);
559
+ }
560
+ return null;
561
+ },
562
+ async configResolved(config) {
563
+ projectRoot = config.root;
564
+ pluginContext.projectRoot = projectRoot;
565
+ resolvedPluginConfigPath = await resolvePluginConfigPath(projectRoot, configPath);
566
+ try {
567
+ devflareConfig = await loadConfig({
568
+ cwd: projectRoot,
569
+ configFile: configPath
570
+ });
571
+ const pluginState = await buildPluginContextState(projectRoot, devflareConfig, environment);
572
+ Object.assign(pluginContext, {
573
+ projectRoot,
574
+ ...pluginState
575
+ });
576
+ logDiscoveredDurableObjects(projectRoot, pluginState.durableObjects);
577
+ await writeGeneratedWranglerConfig(projectRoot, pluginState.wranglerConfig);
578
+ if (config.command === "serve") {
579
+ console.log("[devflare] Config generated to .devflare/wrangler.jsonc");
580
+ if (pluginState.auxiliaryWorkerConfig) {
581
+ console.log("[devflare] ✓ Auxiliary DO worker configured");
582
+ }
583
+ }
584
+ if (config.command === "build") {
585
+ console.log(`[devflare] Generated ${CONFIG_DIR}/wrangler.jsonc`);
586
+ }
587
+ } catch (error) {
588
+ if (error instanceof Error) {
589
+ console.error("[devflare] Config error:", error.message);
590
+ }
591
+ throw error;
592
+ }
593
+ },
594
+ configureServer(server) {
595
+ if (!watchConfig)
596
+ return;
597
+ const fullConfigPath = resolvedPluginConfigPath ?? resolve2(projectRoot, configPath || "devflare.config.ts");
598
+ server.watcher.add(fullConfigPath);
599
+ server.watcher.on("change", async (changedPath) => {
600
+ if (changedPath === fullConfigPath) {
601
+ console.log("[devflare] Config changed, reloading...");
602
+ try {
603
+ devflareConfig = await loadConfig({
604
+ cwd: projectRoot,
605
+ configFile: configPath
606
+ });
607
+ const pluginState = await buildPluginContextState(projectRoot, devflareConfig, environment);
608
+ Object.assign(pluginContext, {
609
+ projectRoot,
610
+ ...pluginState
611
+ });
612
+ logDiscoveredDurableObjects(projectRoot, pluginState.durableObjects);
613
+ await writeGeneratedWranglerConfig(projectRoot, pluginState.wranglerConfig);
614
+ console.log("[devflare] Config reloaded");
615
+ server.ws.send({
616
+ type: "full-reload",
617
+ path: "*"
618
+ });
619
+ } catch (error) {
620
+ console.error("[devflare] Failed to reload config:", error);
621
+ }
622
+ }
623
+ });
624
+ },
625
+ async transform(code, id) {
626
+ if (id.includes("node_modules"))
627
+ return null;
628
+ if (!id.endsWith(".ts") && !id.endsWith(".tsx") && !id.endsWith(".js")) {
629
+ return null;
630
+ }
631
+ if (id.endsWith("worker.ts") || id.endsWith("worker.js")) {
632
+ const {
633
+ shouldTransformWorker,
634
+ transformWorkerEntrypoint
635
+ } = await import("./worker-entrypoint-c259fmfs.js");
636
+ if (shouldTransformWorker(code, id)) {
637
+ const result = transformWorkerEntrypoint(code, id);
638
+ if (result) {
639
+ return {
640
+ code: result.code,
641
+ map: result.map
642
+ };
643
+ }
644
+ }
645
+ }
646
+ if (doTransforms) {
647
+ if (code.includes("DurableObject") || code.includes("@durableObject")) {
648
+ const { transformDurableObject } = await import("./durable-object-yt8v1dyn.js");
649
+ return transformDurableObject(code, id);
650
+ }
651
+ }
652
+ return null;
653
+ }
654
+ };
655
+ }
656
+ async function getCloudflareConfig(options = {}) {
657
+ const cwd = options.cwd ?? process.cwd();
658
+ const devflareConfig = await loadConfig({
659
+ cwd,
660
+ configFile: options.configPath
661
+ });
662
+ const composedMainEntry = await prepareComposedWorkerEntrypoint(cwd, devflareConfig, options.environment);
663
+ const cloudflareConfig = compileToProgrammaticConfig(devflareConfig, options.environment);
664
+ if (composedMainEntry) {
665
+ cloudflareConfig.main = composedMainEntry;
666
+ }
667
+ return cloudflareConfig;
668
+ }
669
+ async function getDevflareConfigs(options = {}) {
670
+ const cwd = options.cwd ?? process.cwd();
671
+ const devflareConfig = await loadConfig({
672
+ cwd,
673
+ configFile: options.configPath
674
+ });
675
+ const composedMainEntry = await prepareComposedWorkerEntrypoint(cwd, devflareConfig, options.environment);
676
+ const wranglerConfig = compileConfig(devflareConfig, options.environment);
677
+ const cloudflareConfig = { ...wranglerConfig };
678
+ if (composedMainEntry) {
679
+ wranglerConfig.main = composedMainEntry;
680
+ cloudflareConfig.main = composedMainEntry;
681
+ }
682
+ const auxiliaryWorkers = [];
683
+ const doPatternConfig = devflareConfig.files?.durableObjects;
684
+ const doPattern = typeof doPatternConfig === "string" ? doPatternConfig : DEFAULT_DO_PATTERN;
685
+ if (doPatternConfig !== false) {
686
+ const doWorkerName = `${wranglerConfig.name}-do`;
687
+ const discovery = await discoverDurableObjects(cwd, doPattern, doWorkerName);
688
+ if (discovery.files.size > 0) {
689
+ if (cloudflareConfig.durable_objects) {
690
+ const doConfig = cloudflareConfig.durable_objects;
691
+ for (const binding of doConfig.bindings) {
692
+ binding.script_name = doWorkerName;
693
+ }
694
+ }
695
+ auxiliaryWorkers.push(createAuxiliaryWorkerConfig(wranglerConfig, discovery));
696
+ }
697
+ }
698
+ return { cloudflareConfig, auxiliaryWorkers };
699
+ }
700
+ // src/vite/config-file.ts
701
+ import { loadConfigFromFile, mergeConfig } from "vite";
702
+ var CONFIG_DIR2 = ".devflare";
703
+ var GENERATED_VITE_CONFIG_FILENAME = "vite.config.mjs";
704
+ function hasInlineViteConfig(viteConfig) {
705
+ return Boolean(viteConfig && Object.keys(viteConfig).length > 0);
706
+ }
707
+ function resolveEffectiveViteProject(detection, config, environment) {
708
+ const resolvedConfig = resolveConfigForEnvironment(config, environment);
709
+ const hasDevflareConfig = hasInlineViteConfig(resolvedConfig.vite);
710
+ return {
711
+ ...detection,
712
+ hasDevflareViteConfig: hasDevflareConfig,
713
+ shouldStartVite: detection.shouldStartVite || hasDevflareConfig,
714
+ wantsViteIntegration: detection.wantsViteIntegration || hasDevflareConfig
715
+ };
716
+ }
717
+ function collectPlugins(pluginOption, plugins = []) {
718
+ if (Array.isArray(pluginOption)) {
719
+ for (const nestedPlugin of pluginOption) {
720
+ collectPlugins(nestedPlugin, plugins);
721
+ }
722
+ return plugins;
723
+ }
724
+ if (!pluginOption || typeof pluginOption === "boolean") {
725
+ return plugins;
726
+ }
727
+ if (typeof pluginOption.then === "function") {
728
+ return plugins;
729
+ }
730
+ plugins.push(pluginOption);
731
+ return plugins;
732
+ }
733
+ function withInjectedDevflarePlugin(config, pluginOptions) {
734
+ const existingPlugins = collectPlugins(config.plugins).filter((plugin) => plugin.name !== "devflare");
735
+ return {
736
+ ...config,
737
+ plugins: [devflarePlugin(pluginOptions), ...existingPlugins]
738
+ };
739
+ }
740
+ async function resolveViteUserConfig(configEnv, options = {}) {
741
+ const cwd = options.cwd ?? process.cwd();
742
+ const devflareConfig = await loadConfig({
743
+ cwd,
744
+ configFile: options.configPath
745
+ });
746
+ const resolvedDevflareConfig = resolveConfigForEnvironment(devflareConfig, options.environment);
747
+ const inlineViteConfig = resolvedDevflareConfig.vite ?? {};
748
+ const localConfig = options.localConfigPath ? (await loadConfigFromFile(configEnv, options.localConfigPath, cwd))?.config ?? {} : {};
749
+ const mergedConfig = mergeConfig(localConfig, inlineViteConfig);
750
+ const normalizedConfig = mergedConfig.root ? mergedConfig : {
751
+ ...mergedConfig,
752
+ root: cwd
753
+ };
754
+ return withInjectedDevflarePlugin(normalizedConfig, {
755
+ configPath: options.configPath,
756
+ environment: options.environment,
757
+ bridgePort: options.bridgePort
758
+ });
759
+ }
760
+ async function ensureGeneratedConfigDir2(cwd) {
761
+ const fs = await import("node:fs/promises");
762
+ const { resolve: resolve3 } = await import("pathe");
763
+ const configDir = resolve3(cwd, CONFIG_DIR2);
764
+ await fs.mkdir(configDir, { recursive: true });
765
+ const gitignorePath = resolve3(configDir, ".gitignore");
766
+ try {
767
+ await fs.access(gitignorePath);
768
+ } catch {
769
+ await fs.writeFile(gitignorePath, `*
770
+ `, "utf-8");
771
+ }
772
+ return configDir;
773
+ }
774
+ async function writeGeneratedViteConfig(options) {
775
+ const fs = await import("node:fs/promises");
776
+ const { resolve: resolve3 } = await import("pathe");
777
+ const configDir = await ensureGeneratedConfigDir2(options.cwd);
778
+ const generatedConfigPath = resolve3(configDir, GENERATED_VITE_CONFIG_FILENAME);
779
+ const content = `import { defineConfig } from 'vite'
780
+ import { resolveViteUserConfig } from 'devflare/vite'
781
+
782
+ export default defineConfig(async (env) => {
783
+ return await resolveViteUserConfig(env, {
784
+ cwd: ${JSON.stringify(options.cwd)},
785
+ configPath: ${JSON.stringify(options.configPath)},
786
+ environment: ${JSON.stringify(options.environment)},
787
+ localConfigPath: ${JSON.stringify(options.localConfigPath)},
788
+ bridgePort: ${JSON.stringify(options.bridgePort)}
789
+ })
790
+ })
791
+ `;
792
+ await fs.writeFile(generatedConfigPath, content, "utf-8");
793
+ return generatedConfigPath;
794
+ }
795
+ export { prepareComposedWorkerEntrypoint, getPluginContext, devflarePlugin, getCloudflareConfig, getDevflareConfigs, hasInlineViteConfig, resolveEffectiveViteProject, resolveViteUserConfig, writeGeneratedViteConfig };