AI-native design editor. Open-source Figma alternative.
Pre-process render JSX in bridge where esbuild is available
Danila Poyarkov39af21ff06fd
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex abd4a15c8..1c7db5823 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -142,6 +142,7 @@ export {} from './kiwi-serialize'export {+ createElement,renderTree,renderJSX,renderTreeNode,@@ -163,6 +164,7 @@ export {Page as PageNode,INTRINSIC_ELEMENTS,isTreeNode,+ resolveToTree,node,type TreeNode,type BaseProps,diff --git a/packages/core/src/render/index.ts b/packages/core/src/render/index.tsindex a85b38a10..d3b397da4 100644--- a/packages/core/src/render/index.ts+++ b/packages/core/src/render/index.ts@@ -29,6 +29,8 @@ export {export { renderTree, type RenderResult } from './renderer'+export { createElement } from './mini-react'+export { renderJSX, renderTreeNode, buildComponent } from './render-jsx'export { sceneNodeToJSX, selectionToJSX, type JSXFormat } from './export-jsx'diff --git a/src/automation/bridge.ts b/src/automation/bridge.tsindex abef11691..de0527bba 100644--- a/src/automation/bridge.ts+++ b/src/automation/bridge.ts@@ -122,11 +122,12 @@ export function startAutomationBridge() {})app.post('/rpc', async (c) => {- const body = await c.req.json().catch(() => null)+ let body = await c.req.json().catch(() => null)if (!body || typeof body !== 'object') {return c.json({ error: 'Invalid request body' }, 400)}try {+ body = await preprocessRpc(body as Record<string, unknown>)const result = await sendToBrowser(body as Record<string, unknown>)return c.json({ ok: true, result })} catch (e) {@@ -141,6 +142,31 @@ export function startAutomationBridge() {console.log(`[automation] WS ws://127.0.0.1:${AUTOMATION_WS_PORT}`)}+async function jsxToTree(jsx: string): Promise<unknown> {+ // Dynamic import hidden from esbuild's static analysis — this file is bundled+ // as part of the Vite config, and esbuild can't resolve workspace packages.+ // At runtime (inside configureServer), the import resolves normally via Vite.+ const pkg = '@open-pencil' + '/core'+ const { buildComponent, resolveToTree, createElement } = await import(/* @vite-ignore */ pkg)+ const Component = await buildComponent(jsx)+ const element = createElement(Component, null)+ return resolveToTree(element)+}++async function preprocessRpc(body: Record<string, unknown>): Promise<Record<string, unknown>> {+ if (body.command !== 'tool') return body+ const args = body.args as { name?: string; args?: Record<string, unknown> } | undefined+ if (args?.name !== 'render' || !args.args?.jsx) return body+ const tree = await jsxToTree(args.args.jsx as string)+ return {+ ...body,+ args: {+ ...args,+ args: { ...args.args, jsx: undefined, tree }+ }+ }+}+function isBunRuntime(): boolean {return 'Bun' in globalThis}diff --git a/src/automation/server.ts b/src/automation/server.tsindex 28920e9f8..bdf17e6d8 100644--- a/src/automation/server.ts+++ b/src/automation/server.ts@@ -4,7 +4,7 @@* Connects to the bridge via WebSocket, receives RPC requests,* executes them against the live EditorStore, and sends results back.*/-import { ALL_TOOLS, AUTOMATION_WS_PORT, FigmaAPI, executeRpcCommand } from '@open-pencil/core'+import { ALL_TOOLS, AUTOMATION_WS_PORT, FigmaAPI, executeRpcCommand, renderTreeNode } from '@open-pencil/core'import type { EditorStore } from '@/stores/editor'import type { ExportFormat } from '@open-pencil/core'@@ -62,6 +62,19 @@ export function connectAutomation(getStore: () => EditorStore) {const toolName = (args as { name?: string })?.nameconst toolArgs = (args as { args?: Record<string, unknown> })?.args ?? {}if (!toolName) throw new Error('Missing "name" in args')++ if (toolName === 'render' && toolArgs.tree) {+ const tree = toolArgs.tree as Parameters<typeof renderTreeNode>[1]+ const result = renderTreeNode(store.graph, tree, {+ parentId: (toolArgs.parent_id as string) ?? store.state.currentPageId,+ x: toolArgs.x as number | undefined,+ y: toolArgs.y as number | undefined+ })+ store.requestRender()+ store.flashNodes([result.id])+ return { ok: true, result: { id: result.id, name: result.name, type: result.type, children: result.childIds } }+ }+const def = ALL_TOOLS.find((t) => t.name === toolName)if (!def) throw new Error(`Unknown tool: ${toolName}`)const figma = makeFigma()