feat(docs): run docs adapters on any renderer, with its renderer passed in
Merged
KI
12d ago+429+429+012345678901234567890123456789-78-78-01234567890123456789
Problem
- A docs adapter ran only in React apps. Every adapter gate checked
isReactRenderer(), and the descriptor had only areactslot for the UI module. - So a Svelte, Vue, Solid, Preact, or custom-renderer app could not use
@farming-labs/farmjs, even with a UI written for its renderer. - Dev and production disagreed for that pairing: production served Farm's built-in docs renderer, while dev served the adapter's pages and never hydrated them.
- A docs UI meant to run on any renderer had no way to get the app's renderer from Farm.
Root cause
- The React gate came in with the Solid renderer (#323) as a precaution. No adapter contract needed it.
- The adapter's server already renders its own UI module, loaded through Farm's module loader, so nothing in the protocol is React-specific except the field names.
Change
- The descriptor gains
ui, the UI module, andrenderer, the Farm renderer that UI is written for (default"react").reactstays as a deprecated alias. - Farm passes the adapter
loadUIModule(), and still passesloadReactModule()to a React UI. - New
loadRenderer()for the adapter's handler:renderer.serverthrough the app's module loader in dev, and in production the module the server entry already imports ({ name, createElement, Fragment, renderToString }on React). A renderer'sgenerateHydrationScript(), such as Solid's, comes with it. It loads only when the UI asks. - The client entry imports
uiand calls itshydrateFarmDocs(). For a renderer other than React it also importsrenderer.clientand passes it asrenderer. React UIs load nothing new. - The React-only gates are gone from the dev server, the production build, routing, prerendering, and analytics. All of them now read
hasFarmDocsRuntimeAdapter(). - Config resolution stops when an adapter's
rendereris not the app's, with the fix in the message. - Auto-detection accepts a descriptor that names
ui. It still runs only for React apps.
Safety and compatibility
- Protocol stays 1. Descriptors with only
reactbehave as before in React apps. - Behavior change: an app with another renderer whose
@farming-labs/farmjshas no UI for it now stops at startup, and the error names both fixes. Before, production served the built-in renderer and dev served unhydrated adapter pages. With farming-labs/docs#728,withDocs()alone builds those pages with the app's renderer. - Older adapters ignore
loadUIModuleandloadRenderer, and readloadReactModule, which React UIs still get. - Edge targets: a UI other than React has no edge compiler, so an edge build stops as before.
Verification
pnpm exec vitest run src/__tests__/docs-adapter.test.ts src/__tests__/docs-adapter-client.test.ts src/__tests__/docs-framework-detect.test.ts: 35 passed. The new and changed tests fail on the previousadapter.ts,adapter-client.ts,config.ts,framework-detect.ts, anduniversal-build.ts.tsc --noEmit -p .inpackages/farm: clean.oxlintandoxfmt --checkon the changed files: clean.- End to end with packed tarballs of this branch and docs#728, in farming-labs/leaf (a custom renderer) with
withDocs(defineConfig({ renderer: leaf() })):- leaf's e2e suite: 17/17 against
farm devand 17/17 against the production build (node-server, installed Chrome). That includes 4 new docs tests: leaf renders the docs on the server, hydration keeps the server's DOM, the theme toggle persists, page links work, and the mobile sidebar opens and closes. - The production client JS contains no React. The docs runtime is one chunk, 4.2 KB over the wire.
- leaf's e2e suite: 17/17 against
- The docs conformance suite from docs#728 passes on React, Preact, Vue, Svelte, Solid, and leaf.
- Copies of the Preact, Solid, Vue, and Svelte examples with
withDocs()and leaf's docs pages, same tarballs, installed Chrome: leaf's 4 docs tests pass againstfarm devand the production build for each, 32/32. Preact dev needed a fix in docs#728 (aa84f372): Preact'sreactalias reached the docs Markdown render in dev. pnpm docs:build: passes (the cron sync skips outside a Vercel production build).pnpm docs:check-navigation: 115 pages.- Serverless: the Preact copy built for
deploy: { target: "vercel" }, its function'sfetchhandler served locally withstatic/: leaf's 4 docs tests pass.
Not verified:
- A real Vercel deployment, and the other serverless targets.
Documentation and release
docs-engine: "Docs adapters on any renderer" coversui, the renderer handoff, the default UI from docs#728, its limits, and the conformance suite.- The Svelte, Vue, Solid, Preact, and custom renderer pages say
withDocs()builds docs pages with that renderer. - Companion: farming-labs/docs#728. Both need a release before leaf's docs branch can land.