$npx -y skills add vercel-labs/json-render --skill codegenCode generation utilities for json-render. Use when generating code from UI specs, building custom code exporters, traversing specs, or serializing props for @json-render/codegen.
| 1 | # @json-render/codegen |
| 2 | |
| 3 | Framework-agnostic utilities for generating code from json-render UI trees. Use these to build custom code exporters for Next.js, Remix, or other frameworks. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install @json-render/codegen |
| 9 | ``` |
| 10 | |
| 11 | ## Tree Traversal |
| 12 | |
| 13 | ```typescript |
| 14 | import { |
| 15 | traverseSpec, |
| 16 | collectUsedComponents, |
| 17 | collectStatePaths, |
| 18 | collectActions, |
| 19 | } from "@json-render/codegen"; |
| 20 | |
| 21 | // Walk the spec depth-first |
| 22 | traverseSpec(spec, (element, key, depth, parent) => { |
| 23 | console.log(`${" ".repeat(depth * 2)}${key}: ${element.type}`); |
| 24 | }); |
| 25 | |
| 26 | // Get all component types used |
| 27 | const components = collectUsedComponents(spec); |
| 28 | // Set { "Card", "Metric", "Button" } |
| 29 | |
| 30 | // Get all state paths referenced |
| 31 | const statePaths = collectStatePaths(spec); |
| 32 | // Set { "analytics/revenue", "user/name" } |
| 33 | |
| 34 | // Get all action names |
| 35 | const actions = collectActions(spec); |
| 36 | // Set { "submit_form", "refresh_data" } |
| 37 | ``` |
| 38 | |
| 39 | ## Serialization |
| 40 | |
| 41 | ```typescript |
| 42 | import { |
| 43 | serializePropValue, |
| 44 | serializeProps, |
| 45 | escapeString, |
| 46 | type SerializeOptions, |
| 47 | } from "@json-render/codegen"; |
| 48 | |
| 49 | // Serialize a single value |
| 50 | serializePropValue("hello"); |
| 51 | // { value: '"hello"', needsBraces: false } |
| 52 | |
| 53 | serializePropValue({ $state: "/user/name" }); |
| 54 | // { value: '{ $state: "/user/name" }', needsBraces: true } |
| 55 | |
| 56 | // Serialize props for JSX |
| 57 | serializeProps({ title: "Dashboard", columns: 3, disabled: true }); |
| 58 | // 'title="Dashboard" columns={3} disabled' |
| 59 | |
| 60 | // Escape strings for code |
| 61 | escapeString('hello "world"'); |
| 62 | // 'hello \"world\"' |
| 63 | ``` |
| 64 | |
| 65 | ### SerializeOptions |
| 66 | |
| 67 | ```typescript |
| 68 | interface SerializeOptions { |
| 69 | quotes?: "single" | "double"; |
| 70 | indent?: number; |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | ## Types |
| 75 | |
| 76 | ```typescript |
| 77 | import type { GeneratedFile, CodeGenerator } from "@json-render/codegen"; |
| 78 | |
| 79 | const myGenerator: CodeGenerator = { |
| 80 | generate(spec) { |
| 81 | return [ |
| 82 | { path: "package.json", content: "..." }, |
| 83 | { path: "app/page.tsx", content: "..." }, |
| 84 | ]; |
| 85 | }, |
| 86 | }; |
| 87 | ``` |
| 88 | |
| 89 | ## Building a Custom Generator |
| 90 | |
| 91 | ```typescript |
| 92 | import { |
| 93 | collectUsedComponents, |
| 94 | collectStatePaths, |
| 95 | traverseSpec, |
| 96 | serializeProps, |
| 97 | type GeneratedFile, |
| 98 | } from "@json-render/codegen"; |
| 99 | import type { Spec } from "@json-render/core"; |
| 100 | |
| 101 | export function generateNextJSProject(spec: Spec): GeneratedFile[] { |
| 102 | const files: GeneratedFile[] = []; |
| 103 | const components = collectUsedComponents(spec); |
| 104 | // Generate package.json, component files, main page... |
| 105 | return files; |
| 106 | } |
| 107 | ``` |