solid-plugin.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { transformAsync } from "@babel/core"
  2. // @ts-expect-error - Types not important.
  3. import ts from "@babel/preset-typescript"
  4. // @ts-expect-error - Types not important.
  5. import solid from "babel-preset-solid"
  6. import { type BunPlugin } from "bun"
  7. const solidTransformPlugin: BunPlugin = {
  8. name: "bun-plugin-solid",
  9. setup: (build) => {
  10. build.onLoad({ filter: /\/node_modules\/solid-js\/dist\/server\.js$/ }, async (args) => {
  11. const path = args.path.replace("server.js", "solid.js")
  12. const file = Bun.file(path)
  13. const code = await file.text()
  14. return { contents: code, loader: "js" }
  15. })
  16. build.onLoad({ filter: /\/node_modules\/solid-js\/store\/dist\/server\.js$/ }, async (args) => {
  17. const path = args.path.replace("server.js", "store.js")
  18. const file = Bun.file(path)
  19. const code = await file.text()
  20. return { contents: code, loader: "js" }
  21. })
  22. build.onLoad({ filter: /\.(js|ts)x$/ }, async (args) => {
  23. const file = Bun.file(args.path)
  24. const code = await file.text()
  25. const transforms = await transformAsync(code, {
  26. filename: args.path,
  27. presets: [
  28. [
  29. solid,
  30. {
  31. moduleName: "@opentui/solid",
  32. generate: "universal",
  33. },
  34. ],
  35. [ts],
  36. ],
  37. })
  38. return {
  39. contents: transforms?.code ?? "",
  40. loader: "js",
  41. }
  42. })
  43. },
  44. }
  45. export default solidTransformPlugin