page.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. export const metadata = { title: "Code Export" }
  2. # Code Export
  3. Export generated UI as standalone code for your framework.
  4. ## Overview
  5. While json-render is designed for dynamic rendering, you can export generated UI as static code. The code generation is intentionally project-specific so you have full control over:
  6. - Component templates (standalone, no json-render dependencies)
  7. - Package.json and project structure
  8. - Framework-specific patterns (Next.js, Remix, etc.)
  9. - How data is passed to components
  10. ## Architecture
  11. Code export is split into two parts:
  12. ### 1. @json-render/codegen (utilities)
  13. Framework-agnostic utilities for building code generators:
  14. ```typescript
  15. import {
  16. traverseSpec, // Walk the UI spec
  17. collectUsedComponents, // Get all component types used
  18. collectStatePaths, // Get all data binding paths
  19. collectActions, // Get all action names
  20. serializeProps, // Convert props to JSX string
  21. } from '@json-render/codegen';
  22. ```
  23. ### 2. Your Project (generator)
  24. Custom code generator specific to your project and framework:
  25. ```typescript
  26. // lib/codegen/generator.ts
  27. import { collectUsedComponents, serializeProps } from '@json-render/codegen';
  28. export function generateNextJSProject(spec: Spec): GeneratedFile[] {
  29. const components = collectUsedComponents(spec);
  30. return [
  31. { path: 'package.json', content: '...' },
  32. { path: 'app/page.tsx', content: '...' },
  33. // ... component files
  34. ];
  35. }
  36. ```
  37. ## Example: Next.js Export
  38. See the dashboard example for a complete implementation that exports:
  39. - `package.json` - Dependencies and scripts
  40. - `tsconfig.json` - TypeScript config
  41. - `next.config.js` - Next.js config
  42. - `app/layout.tsx` - Root layout
  43. - `app/globals.css` - Global styles
  44. - `app/page.tsx` - Generated page with data
  45. - `components/ui/*.tsx` - Standalone components
  46. ## Standalone Components
  47. The exported components are standalone with no json-render dependencies. They receive data as props instead of using hooks:
  48. ```tsx
  49. // Generated component (standalone)
  50. interface MetricProps {
  51. label: string;
  52. statePath: string;
  53. data?: Record<string, unknown>;
  54. }
  55. export function Metric({ label, statePath, data }: MetricProps) {
  56. const value = data ? getByPath(data, statePath) : undefined;
  57. return (
  58. <div>
  59. <span>{label}</span>
  60. <span>{formatValue(value)}</span>
  61. </div>
  62. );
  63. }
  64. ```
  65. ## Using the Utilities
  66. ### traverseSpec
  67. ```typescript
  68. import { traverseSpec } from '@json-render/codegen';
  69. traverseSpec(spec, (element, key, depth, parent) => {
  70. console.log(' '.repeat(depth * 2) + `${key}: ${element.type}`);
  71. });
  72. ```
  73. ### collectUsedComponents
  74. ```typescript
  75. import { collectUsedComponents } from '@json-render/codegen';
  76. const components = collectUsedComponents(spec);
  77. // Set { 'Card', 'Metric', 'Chart', 'Table' }
  78. // Generate only the needed component files
  79. for (const component of components) {
  80. files.push({
  81. path: `components/ui/${component.toLowerCase()}.tsx`,
  82. content: componentTemplates[component],
  83. });
  84. }
  85. ```
  86. ### serializeProps
  87. ```typescript
  88. import { serializeProps } from '@json-render/codegen';
  89. const propsStr = serializeProps({
  90. title: 'Dashboard',
  91. columns: 3,
  92. disabled: true,
  93. });
  94. // 'title="Dashboard" columns={3} disabled'
  95. ```
  96. ## Try It
  97. Run the dashboard example and click "Export Project" to see code generation in action:
  98. ```bash
  99. cd examples/dashboard
  100. pnpm dev
  101. # Open http://localhost:3001
  102. # Generate a widget, then click "Export Project"
  103. ```