|
| 1 | +import { existsSync } from 'node:fs'; |
| 2 | +import { dirname, resolve } from 'node:path'; |
| 3 | +import { type Unstable_Config, unstable_readConfig } from 'wrangler'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The slice of the wrangler configuration the auto-instrument plugin cares |
| 7 | + * about. `main` is an absolute path (wrangler resolves it against the config |
| 8 | + * file's directory). |
| 9 | + */ |
| 10 | +export interface WranglerConfig { |
| 11 | + main?: string; |
| 12 | + durableObjects: Array<{ name: string; className: string }>; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Locate and resolve the wrangler configuration via wrangler's own |
| 17 | + * `unstable_readConfig` — the API `@cloudflare/vite-plugin` uses. |
| 18 | + * |
| 19 | + * We only locate the file (probing `wrangler.json`, `.jsonc`, `.toml` inside |
| 20 | + * `root` with wrangler's own precedence, since it discovers from `cwd` rather |
| 21 | + * than an arbitrary root); wrangler then parses it, flattens the active |
| 22 | + * environment (honoring `CLOUDFLARE_ENV`), and resolves `main` to an absolute |
| 23 | + * path. Durable Object bindings are the active environment's, matching what the |
| 24 | + * deployed Worker actually binds. |
| 25 | + * |
| 26 | + * Returns `undefined` when no config file is found or it can't be read/parsed |
| 27 | + * (the caller warns and disables auto-instrumentation rather than failing the |
| 28 | + * whole build). |
| 29 | + */ |
| 30 | +export function resolveWranglerConfig( |
| 31 | + root: string, |
| 32 | + explicitPath?: string, |
| 33 | +): { config: WranglerConfig; configDir: string } | undefined { |
| 34 | + const configPath = explicitPath |
| 35 | + ? resolve(root, explicitPath) |
| 36 | + : ['wrangler.json', 'wrangler.jsonc', 'wrangler.toml'].map(name => resolve(root, name)).find(existsSync); |
| 37 | + |
| 38 | + if (!configPath || !existsSync(configPath)) { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + |
| 42 | + let raw: Unstable_Config; |
| 43 | + try { |
| 44 | + // `hideWarnings` keeps wrangler's config diagnostics (e.g. missing DO |
| 45 | + // migrations) out of the Vite build output. |
| 46 | + raw = unstable_readConfig({ config: configPath }, { hideWarnings: true }); |
| 47 | + } catch { |
| 48 | + return undefined; |
| 49 | + } |
| 50 | + |
| 51 | + const durableObjects: WranglerConfig['durableObjects'] = []; |
| 52 | + const seenClassNames = new Set<string>(); |
| 53 | + for (const binding of raw.durable_objects?.bindings ?? []) { |
| 54 | + // `script_name` bindings reference a class exported by a *different* worker |
| 55 | + // — there is nothing to wrap in this worker's entry file. |
| 56 | + if (typeof binding?.class_name !== 'string' || binding.script_name || seenClassNames.has(binding.class_name)) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + seenClassNames.add(binding.class_name); |
| 60 | + durableObjects.push({ name: binding.name, className: binding.class_name }); |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + config: { main: raw.main, durableObjects }, |
| 65 | + configDir: dirname(raw.configPath ?? configPath), |
| 66 | + }; |
| 67 | +} |
0 commit comments