React / foldkit-react-codegen

Programmatic API

ts
import { formatDiagnostic, generate, transformSourceFile, watch } from 'foldkit-react-codegen'

const sourceText = `
import type { Html, HtmlBuilder } from 'foldkit/html'
export const view = (model: { title: string }, h: HtmlBuilder<never>): Html =>
  h.h1([], [model.title])
`
const result = transformSourceFile('src/Title.ts', sourceText, { sourceMap: true })
if (result.ok) {
  console.log(result.code, result.map)
} else {
  result.diagnostics.map(formatDiagnostic).forEach(line => console.error(line))
}

await generate({ inputs: ['src'], outDir: 'generated' })

const watcher = watch({ inputs: ['src'], outDir: 'generated' }, event => {
  if (event._tag === 'Generated') console.log(event.result.written)
})
watcher.close()

transformSourceFile is pure. Note that --source-map relies on TypeScript compiler printer internals and throws an error on unsupported TypeScript versions rather than writing broken maps.

Limits

OnMount and OnUnmount lifecycle attributes are unsupported in compiled JSX views; host those components via foldkit-react.

Next

  • Embed full Foldkit programs or React islands at runtime: foldkit-react
  • See both codegen and runtime embedding working together: examples/react