Skip to content

AI-native design editor. Open-source Figma alternative.

Fix render tool in automation: use ssrLoadModule for JSX transform

Danila Poyarkov70c25cddf51e
diff --git a/packages/core/package.json b/packages/core/package.json
index bac1fafa2..ab7628612 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -7,7 +7,6 @@
".": {
"types": "./src/index.ts",
"bun": "./src/index.ts",
- "node": "./dist/index.js",
"default": "./src/index.ts"
}
},
@@ -25,7 +24,16 @@
},
"publishConfig": {
"access": "public",
- "provenance": true
+ "provenance": true,
+ "exports": {
+ ".": {
+ "types": "./dist/index.d.ts",
+ "node": "./dist/index.js",
+ "default": "./dist/index.js"
+ }
+ },
+ "main": "./dist/index.js",
+ "types": "./dist/index.d.ts"
},
"dependencies": {
"canvaskit-wasm": "^0.40.0",
diff --git a/src/automation/bridge.ts b/src/automation/bridge.ts
index de0527bba..84d7e14ba 100644
--- a/src/automation/bridge.ts
+++ b/src/automation/bridge.ts
@@ -76,7 +76,8 @@ function handleBrowserMessage(data: string) {
}
}
-export function startAutomationBridge() {
+export function startAutomationBridge(server: ViteServer) {
+ viteServer = server
const wss = new WebSocketServer({ port: AUTOMATION_WS_PORT, host: '127.0.0.1' })
wss.on('connection', (ws) => {
@@ -142,12 +143,15 @@ export function startAutomationBridge() {
console.log(`[automation] WS ws://127.0.0.1:${AUTOMATION_WS_PORT}`)
}
+type ViteServer = { ssrLoadModule: (id: string) => Promise<Record<string, unknown>> }
+let viteServer: ViteServer | null = null
+
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)
+ if (!viteServer) throw new Error('Vite server not initialized')
+ const core = await viteServer.ssrLoadModule('@open-pencil/core')
+ const buildComponent = core.buildComponent as (jsx: string) => Promise<() => unknown>
+ const resolveToTree = core.resolveToTree as (el: unknown) => unknown
+ const createElement = core.createElement as (type: unknown, props: unknown) => unknown
const Component = await buildComponent(jsx)
const element = createElement(Component, null)
return resolveToTree(element)
diff --git a/src/automation/vite-plugin.ts b/src/automation/vite-plugin.ts
index 86638564c..13ea7c3e1 100644
--- a/src/automation/vite-plugin.ts
+++ b/src/automation/vite-plugin.ts
@@ -3,9 +3,9 @@ import type { Plugin } from 'vite'
export function automationPlugin(): Plugin {
return {
name: 'open-pencil-automation',
- configureServer() {
+ configureServer(server) {
import('./bridge').then(({ startAutomationBridge }) => {
- startAutomationBridge()
+ startAutomationBridge(server)
})
}
}