mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[GH-ISSUE #253] Refactor: Extract template string code generation from index.ts and app-dev-server.ts into separate modules #65
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#65
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @southpolesteve on GitHub (Mar 5, 2026).
Original GitHub issue: https://github.com/cloudflare/vinext/issues/253
Problem
index.ts(3,528 lines) andapp-dev-server.ts(2,734 lines) are the two largest files in the codebase. Both files mix Vite plugin logic with massive template string blocks that generate runtime JavaScript modules. This creates several problems:\\n,\\.),varinstead ofconst/let, and mixes interpolated expressions with static code. AI agents routinely break escaping or confuse plugin code with generated runtime code.What needs to be extracted
From
index.tsgenerateServerEntry()middlewareExportCodevariablegenerateClientEntry()_appwrapping,hydrateRoot()The server entry template (743 lines) is the primary target. It contains the entire Pages Router production request lifecycle as a template string.
From
app-dev-server.tsgenerateRscEntry()generateSsrEntry()generateBrowserEntry()The RSC entry template (1,860 lines) is the largest single template string in the codebase. It contains at least 15 logically distinct subsystems inlined as generated code.
Proposed approach
1. Create an
entries/directoryEach file exports a function that takes the same parameters the current generator functions receive (route table, config, feature flags) and returns the generated code string.
2. Add snapshot tests
For each extracted template function, add a test that:
This is the key safety improvement. Currently there's no way to see what the generated code looks like without running the full pipeline.
3. Reduce
index.tsandapp-dev-server.tsto orchestrationAfter extraction,
index.tsshould be the Vite plugin shell (resolveId, load, configureServer, config hooks) that calls into the entry generators.app-dev-server.tsshould be a thin module that calls the three App Router entry generators.Non-goals
Verification
pnpm test(all Vitest tests pass)pnpm run test:e2e(all Playwright E2E tests pass)pnpm run typecheckpnpm run lintindex.tsis under 1,000 lines andapp-dev-server.tsis under 500 lines after extraction@james-elicx commented on GitHub (Mar 8, 2026):
For contributors: I think it would be best to do this incrementally (starting with snapshots) instead of all-at-once, so that we can better avoid regressions with our fast rate-of-change.
@southpolesteve commented on GitHub (Mar 20, 2026):
Reopening. PR #610 hit multiple codegen issues that reinforce the need for this refactor: TypeScript syntax in generated JS causing parse failures, backticks in comments breaking the formatter, snapshot churn on every change, and dynamic detection flags leaking across pipeline stages because the logic is inlined rather than properly scoped in a real function.
The __handleRouteWithIsrCache extraction in PR #610 is a first step toward this, but the function still lives inside the template string. Moving it into a real TypeScript module would be the next step.
@NathanDrake2406 commented on GitHub (May 1, 2026):
The coupled god files have been crippling our speed. I plan to refactor this to be modular with clear separation so that we can parallel tasks/features without wasting time in merge hell.
Also a pattern I've noticed is that agents tend to follow the codebase's existing convention and design. If we refactor to be elegant, well-tested code then there's a higher chance going forward the agents will follow that.
Refactor roadmap
Single thesis: two monoliths today → pure cores + thin shells + thin generated wiring tomorrow. Every PR moves one decision out of an effect-heavy shell.
Legend: 🟢 already merged · 🟦 open PR (in flight) · ⬜ planned · 🟨 target state