|
| 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() + "/", "")}`) |
0 commit comments