Skip to content

Commit d5fe8dd

Browse files
committed
fix(tui): load published TUI via precompiled entry
OpenCode's packaged CLI does not run OpenTUI's Solid JSX transform on TSX loaded from node_modules, so the published TUI part failed to load with "Cannot find package 'react'". Precompile src/tui.tsx at build time and ship a JS shim as the ./tui entry that binds the host runtime through opentui:runtime-module ids, falling back to the raw TSX for local/dev hosts. Temporary workaround for anomalyco/opencode#39986.
1 parent 580040d commit d5fe8dd

6 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
with:
3636
node-version: 24
3737
- run: bun install --frozen-lockfile
38+
- run: bun run build:tui
3839
- run: npx semantic-release
3940
env:
4041
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules/
22
dist/
3+
src/tui.compiled.tsx
34
*.log
45
.env
56
.DS_Store

‎package.json‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@
88
"exports": {
99
".": "./src/index.ts",
1010
"./rpc": "./src/rpc.ts",
11-
"./tui": "./src/tui.tsx"
11+
"./tui": "./src/tui.entry.mjs"
1212
},
1313
"files": [
1414
"src/index.ts",
1515
"src/judge.ts",
1616
"src/lib.ts",
1717
"src/rpc.ts",
18-
"src/tui.tsx"
18+
"src/tui.tsx",
19+
"src/tui.entry.mjs",
20+
"src/tui.compiled.tsx"
1921
],
2022
"publishConfig": {
2123
"access": "public"
2224
},
2325
"scripts": {
2426
"typecheck": "tsc --noEmit",
2527
"test": "bun test src/",
26-
"test:integration": "bun test test/integration.test.ts"
28+
"test:integration": "bun test test/integration.test.ts",
29+
"build:tui": "bun scripts/build-tui.ts",
30+
"prepublishOnly": "bun run build:tui"
2731
},
2832
"keywords": [
2933
"opencode",

‎scripts/build-tui.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { mkdir, writeFile } from "node:fs/promises"
2+
import { createRequire } from "node:module"
3+
import { dirname, join } from "node:path"
4+
import { fileURLToPath, pathToFileURL } from "node:url"
5+
6+
// Temporary workaround for anomalyco/opencode#39986: the packaged CLI does not
7+
// run OpenTUI's Solid JSX transform on TSX under node_modules, so the published
8+
// TUI must be precompiled. `@opentui/solid` does not export this subpath, so it
9+
// is resolved from the package root rather than imported bare.
10+
async function loadTransform() {
11+
const resolvePackageJson = async (): Promise<string> => {
12+
try {
13+
return fileURLToPath(import.meta.resolve("@opentui/solid/package.json"))
14+
} catch {
15+
try {
16+
return createRequire(import.meta.url).resolve("@opentui/solid/package.json")
17+
} catch {
18+
throw new Error(
19+
"build-tui: could not resolve @opentui/solid/package.json; is @opentui/solid installed?",
20+
)
21+
}
22+
}
23+
}
24+
const transformPath = join(dirname(await resolvePackageJson()), "scripts/solid-transform.js")
25+
const mod = (await import(pathToFileURL(transformPath).href)) as {
26+
transformSolidSource: (code: string, options: unknown) => Promise<string>
27+
}
28+
return mod.transformSolidSource
29+
}
30+
31+
const sourcePath = fileURLToPath(new URL("../src/tui.tsx", import.meta.url))
32+
const outputPath = fileURLToPath(new URL("../src/tui.compiled.tsx", import.meta.url))
33+
34+
const runtimeSpecifiers = new Set([
35+
"@opentui/core",
36+
"@opentui/core/testing",
37+
"@opentui/solid",
38+
"@opentui/solid/components",
39+
"@opentui/solid/jsx-runtime",
40+
"@opentui/solid/jsx-dev-runtime",
41+
"solid-js",
42+
"solid-js/store",
43+
])
44+
const runtimeModuleId = (s: string) => `opentui:runtime-module:${encodeURIComponent(s)}`
45+
46+
const transformSolidSource = await loadTransform()
47+
const code = await Bun.file(sourcePath).text()
48+
const compiled = await transformSolidSource(code, {
49+
filename: sourcePath,
50+
moduleName: runtimeModuleId("@opentui/solid"),
51+
resolvePath: (s: string) => (runtimeSpecifiers.has(s) ? runtimeModuleId(s) : null),
52+
})
53+
54+
await mkdir(dirname(outputPath), { recursive: true })
55+
await writeFile(outputPath, `// @ts-nocheck\n${compiled}`)
56+
console.log(`build-tui: wrote ${outputPath.replace(process.cwd() + "/", "")}`)

‎src/tui.entry.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Temporary workaround for anomalyco/opencode#39986: the packaged CLI does not
2+
// run the OpenTUI Solid JSX transform on TSX loaded from node_modules, so the
3+
// published TUI entry must be precompiled JS. Probe the host runtime registry,
4+
// then load the compiled bundle; fall back to raw TSX for local/dev hosts.
5+
let mod
6+
try {
7+
await import("opentui:runtime-module:" + encodeURIComponent("@opentui/solid"))
8+
mod = await import("./tui.compiled.tsx")
9+
} catch {
10+
mod = await import("./tui.tsx")
11+
}
12+
13+
export default mod.default

‎src/tui.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// The published `./tui` entry is a precompiled copy of this file (see
2+
// scripts/build-tui.ts), a temporary workaround for anomalyco/opencode#39986.
3+
// This raw TSX stays the source of truth and the dev fallback.
14
import { Plugin } from "@opencode/plugin/tui"
25
import type { RGBA } from "@opentui/core"
36
import { createEffect, createSignal, onCleanup } from "solid-js"

0 commit comments

Comments
 (0)