|
| 1 | +import { plugin, type BunPlugin } from "bun" |
| 2 | +import { transformAsync } from "@babel/core" |
| 3 | +import solid from "babel-preset-solid" |
| 4 | +import react from "@babel/preset-react" |
| 5 | +import ts from "@babel/preset-typescript" |
| 6 | + |
| 7 | +const inkExcludedPlugin: BunPlugin = { |
| 8 | + name: "solid-with-ink-exclusion", |
| 9 | + setup(build) { |
| 10 | + build.onLoad({ filter: /[\\/]node_modules[\\/]solid-js[\\/]dist[\\/]server\.js$/ }, async (args) => { |
| 11 | + try { |
| 12 | + const path = args.path.replace(/(\.server\.js)$/, ".solid.js") |
| 13 | + const file = Bun.file(path) |
| 14 | + if (!(await file.exists())) return undefined |
| 15 | + const code = await file.text() |
| 16 | + return { contents: code, loader: "js" } |
| 17 | + } catch { |
| 18 | + return undefined |
| 19 | + } |
| 20 | + }) |
| 21 | + |
| 22 | + build.onLoad({ filter: /[\\/]node_modules[\\/]solid-js[\\/]store[\\/]dist[\\/]server\.js$/ }, async (args) => { |
| 23 | + try { |
| 24 | + const path = args.path.replace(/(\.server\.js)$/, ".store.js") |
| 25 | + const file = Bun.file(path) |
| 26 | + if (!(await file.exists())) return undefined |
| 27 | + const code = await file.text() |
| 28 | + return { contents: code, loader: "js" } |
| 29 | + } catch { |
| 30 | + return undefined |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + build.onLoad({ filter: /\.(tsx|jsx)$/ }, async (args) => { |
| 35 | + if (args.path.includes("node_modules")) { |
| 36 | + return undefined |
| 37 | + } |
| 38 | + |
| 39 | + const code = await Bun.file(args.path).text() |
| 40 | + |
| 41 | + // Detect if file is Ink-related (React-based) |
| 42 | + const isInkFile = |
| 43 | + /from\s*["']ink["']/.test(code) || |
| 44 | + /from\s*["']ink-/.test(code) || |
| 45 | + /require\(["']ink["']\)/.test(code) || |
| 46 | + /@jsxImportSource\s+react/.test(code) || |
| 47 | + args.path.includes("/cli/ink/") || |
| 48 | + args.path.includes("/test/cli/ink/") |
| 49 | + |
| 50 | + if (isInkFile) { |
| 51 | + // Transform with Babel React preset |
| 52 | + const result = await transformAsync(code, { |
| 53 | + filename: args.path, |
| 54 | + presets: [ |
| 55 | + ["@babel/preset-react", { runtime: "automatic", importSource: "react" }], |
| 56 | + [ts, { isTSX: true, allExtensions: true }], |
| 57 | + ], |
| 58 | + }) |
| 59 | + return { contents: result?.code ?? "", loader: "js" } |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + // Transform with SolidJS preset |
| 64 | + const transforms = await transformAsync(code, { |
| 65 | + filename: args.path, |
| 66 | + presets: [ |
| 67 | + [ |
| 68 | + solid, |
| 69 | + { |
| 70 | + moduleName: "@opentui/solid", |
| 71 | + generate: "universal", |
| 72 | + }, |
| 73 | + ], |
| 74 | + [ts], |
| 75 | + ], |
| 76 | + }) |
| 77 | + |
| 78 | + if (!transforms || !transforms.code) { |
| 79 | + throw new Error(`Transform produced no output: ${args.path}`) |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + contents: transforms.code, |
| 84 | + loader: "js", |
| 85 | + } |
| 86 | + } catch (error) { |
| 87 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 88 | + const errorStack = error instanceof Error ? error.stack : undefined |
| 89 | + const fullMessage = errorStack |
| 90 | + ? `SolidJS transform failed for ${args.path}: ${errorMessage}\n${errorStack}` |
| 91 | + : `SolidJS transform failed for ${args.path}: ${errorMessage}` |
| 92 | + throw new Error(fullMessage) |
| 93 | + } |
| 94 | + }) |
| 95 | + }, |
| 96 | +} |
| 97 | + |
| 98 | +plugin(inkExcludedPlugin) |
0 commit comments