React / foldkit-react-codegen

Library 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: text in, text (and a map, if asked) or diagnostics out. Source maps rely on TypeScript printer internals; on a TypeScript version without them, asking for a map throws a clear error rather than emitting a wrong one.

Not yet

OnMount and OnUnmount are not supported yet.