[wrangler] Add programmatic TypeScript config file support#12533
[wrangler] Add programmatic TypeScript config file support#12533
Conversation
|
| case "programmatic": | ||
| // Return the actual filename for programmatic configs | ||
| return configPath | ||
| ? configPath.split("/").pop() ?? "config file" |
There was a problem hiding this comment.
There's a path API for this that'd be better
| // For programmatic configs, return empty config - they are handled | ||
| // separately via loadProgrammaticConfig() in the ConfigController | ||
| if (isProgrammaticConfigPath(configPath)) { | ||
| return { | ||
| rawConfig: {}, | ||
| configPath, | ||
| userConfigPath, | ||
| deployConfigPath, | ||
| redirected, | ||
| }; | ||
| } |
There was a problem hiding this comment.
I think we should probably throw in this case instead?
| /** | ||
| * Helper function to define a worker configuration with type safety. | ||
| */ | ||
| export function worker(fn: WorkerConfigFn): WorkerConfigFn { |
There was a problem hiding this comment.
I think we actually want this user facing function to be called defineConfig()
| const wranglerDir = path.join(configDir, ".wrangler", "tmp"); | ||
| await fs.promises.mkdir(wranglerDir, { recursive: true }); |
There was a problem hiding this comment.
Don't we have helpers for finding Wrangler's temp dir?
| const tsconfigPath = findTsConfig(configDir); | ||
|
|
||
| // Create a unique temp file path in .wrangler directory | ||
| const wranglerDir = path.join(configDir, ".wrangler", "tmp"); |
There was a problem hiding this comment.
Don't we have helpers for this?
packages/workers-utils/package.json
Outdated
| "dependencies": { | ||
| "esbuild": "catalog:default" | ||
| }, |
There was a problem hiding this comment.
Move this to devDeps along with everything else
| this.#programmaticWatcher = await watchProgrammaticConfig({ | ||
| configPath, | ||
| env: this.latestInput?.env, | ||
| onChange: (_result: LoadProgrammaticConfigResult) => { |
There was a problem hiding this comment.
Why isn't this result being used?
d77f7d7 to
b39637e
Compare
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
e5c5df5 to
ba60297
Compare
- Add missing curly braces for single-line if statements (config-helpers, programmatic) - Replace non-null assertions with proper narrowing guards (ConfigController, register-yargs-command, worker-config-to-config) - Remove unused type imports (startDevWorker/types) - Use const for single-assignment variable (programmatic.ts ctx) - Remove unused BuildContext import
There was a problem hiding this comment.
This file is so much messier than I wanted it to be. Turns out that even after the CfWorkerInit["bindings"] refactor we had a lot of places accessing bindings directly from the Config type (which had a similar shape). More refactoring to come to make this simpler!
| /** | ||
| * Pre-flattened bindings from programmatic config. | ||
| * When set, deploy can use these directly instead of calling getBindings(config) | ||
| * which would unflatten→reflatten. TOML configs leave this undefined. | ||
| * | ||
| * Typed as `Record<string, unknown>` to avoid cross-module type dependency in | ||
| * the DTS bundle. Consumers should cast to `Record<string, Binding>`. | ||
| */ | ||
| flatBindings?: Record<string, unknown>; |
There was a problem hiding this comment.
This is messy, and there's more refactoring to do to make this not necessary
WIP — introduces programmatic TypeScript config files (
cf.config.ts) as an alternative to staticwrangler.toml/wrangler.json.Changes
createWorkerUploadFormandprovisionBindingsuse the new flat bindings format@cloudflare/workers-utils—StartDevWorkerInput,Hook,CfAccount, etc. so they can be imported by user config files without pulling in all of wranglerWorkerConfigtype — user-facing subset ofStartDevWorkerInputwithenvinstead ofbindingsworker()helper — type-safe identity function for config files:export default worker(() => ({ ... })).ts/.jsconfig files, with watch mode supportcf.config.ts→cloudflare.config.ts→wrangler.config.ts→wrangler.json→wrangler.tomlwrangler/configand@cloudflare/workers-utils/programmatic