electron.vite.config.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { defineConfig } from "electron-vite"
  2. import appPlugin from "@opencode-ai/app/vite"
  3. import * as fs from "node:fs/promises"
  4. const channel = (() => {
  5. const raw = process.env.OPENCODE_CHANNEL
  6. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  7. return "dev"
  8. })()
  9. const OPENCODE_SERVER_DIST = "../opencode/dist/node"
  10. const nodePtyPkg = `@lydell/node-pty-${process.platform}-${process.arch}`
  11. export default defineConfig({
  12. main: {
  13. define: {
  14. "import.meta.env.OPENCODE_CHANNEL": JSON.stringify(channel),
  15. },
  16. build: {
  17. rollupOptions: {
  18. input: { index: "src/main/index.ts" },
  19. },
  20. externalizeDeps: { include: [nodePtyPkg] },
  21. },
  22. plugins: [
  23. {
  24. name: "opencode:node-pty-narrower",
  25. enforce: "pre",
  26. resolveId(s) {
  27. if (s === "@lydell/node-pty") return nodePtyPkg
  28. },
  29. },
  30. {
  31. name: "opencode:virtual-server-module",
  32. enforce: "pre",
  33. resolveId(id) {
  34. if (id === "virtual:opencode-server") return this.resolve(`${OPENCODE_SERVER_DIST}/node.js`)
  35. },
  36. },
  37. {
  38. name: "opencode:copy-server-assets",
  39. async writeBundle() {
  40. for (const l of await fs.readdir(OPENCODE_SERVER_DIST)) {
  41. if (!l.endsWith(".wasm")) continue
  42. await fs.writeFile(`./out/main/chunks/${l}`, await fs.readFile(`${OPENCODE_SERVER_DIST}/${l}`))
  43. }
  44. },
  45. },
  46. ],
  47. },
  48. preload: {
  49. build: {
  50. rollupOptions: {
  51. input: { index: "src/preload/index.ts" },
  52. },
  53. },
  54. },
  55. renderer: {
  56. plugins: [appPlugin],
  57. publicDir: "../../../app/public",
  58. root: "src/renderer",
  59. define: {
  60. "import.meta.env.VITE_OPENCODE_CHANNEL": JSON.stringify(channel),
  61. },
  62. build: {
  63. rollupOptions: {
  64. input: {
  65. main: "src/renderer/index.html",
  66. loading: "src/renderer/loading.html",
  67. },
  68. },
  69. },
  70. },
  71. })