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