electron.vite.config.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { sentryVitePlugin } from "@sentry/vite-plugin"
  2. import { defineConfig } from "electron-vite"
  3. import appPlugin from "@opencode-ai/app/vite"
  4. import * as fs from "node:fs/promises"
  5. const channel = (() => {
  6. const raw = process.env.OPENCODE_CHANNEL
  7. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  8. return "dev"
  9. })()
  10. const OPENCODE_SERVER_DIST = "../opencode/dist/node"
  11. const nodePtyPkg = `@lydell/node-pty-${process.platform}-${process.arch}`
  12. const sentry =
  13. process.env.SENTRY_AUTH_TOKEN && process.env.SENTRY_ORG && process.env.SENTRY_PROJECT
  14. ? sentryVitePlugin({
  15. authToken: process.env.SENTRY_AUTH_TOKEN,
  16. org: process.env.SENTRY_ORG,
  17. project: process.env.SENTRY_PROJECT,
  18. telemetry: false,
  19. release: {
  20. name: process.env.SENTRY_RELEASE ?? process.env.VITE_SENTRY_RELEASE,
  21. },
  22. sourcemaps: {
  23. assets: "./out/renderer/**",
  24. filesToDeleteAfterUpload: "./out/renderer/**/*.map",
  25. },
  26. })
  27. : false
  28. export default defineConfig({
  29. main: {
  30. define: {
  31. "import.meta.env.OPENCODE_CHANNEL": JSON.stringify(channel),
  32. },
  33. build: {
  34. rollupOptions: {
  35. input: { index: "src/main/index.ts" },
  36. },
  37. externalizeDeps: { include: [nodePtyPkg] },
  38. },
  39. plugins: [
  40. {
  41. name: "opencode:node-pty-narrower",
  42. enforce: "pre",
  43. resolveId(s) {
  44. if (s === "@lydell/node-pty") return nodePtyPkg
  45. },
  46. },
  47. {
  48. name: "opencode:virtual-server-module",
  49. enforce: "pre",
  50. resolveId(id) {
  51. if (id === "virtual:opencode-server") return this.resolve(`${OPENCODE_SERVER_DIST}/node.js`)
  52. },
  53. },
  54. {
  55. name: "opencode:copy-server-assets",
  56. async writeBundle() {
  57. for (const l of await fs.readdir(OPENCODE_SERVER_DIST)) {
  58. if (!l.endsWith(".wasm")) continue
  59. await fs.writeFile(`./out/main/chunks/${l}`, await fs.readFile(`${OPENCODE_SERVER_DIST}/${l}`))
  60. }
  61. },
  62. },
  63. ],
  64. },
  65. preload: {
  66. build: {
  67. rollupOptions: {
  68. input: { index: "src/preload/index.ts" },
  69. output: {
  70. format: "cjs",
  71. entryFileNames: "[name].js",
  72. },
  73. },
  74. },
  75. },
  76. renderer: {
  77. plugins: [appPlugin, sentry],
  78. publicDir: "../../../app/public",
  79. root: "src/renderer",
  80. define: {
  81. "import.meta.env.VITE_OPENCODE_CHANNEL": JSON.stringify(channel),
  82. },
  83. build: {
  84. sourcemap: true,
  85. rollupOptions: {
  86. input: {
  87. main: "src/renderer/index.html",
  88. loading: "src/renderer/loading.html",
  89. },
  90. },
  91. },
  92. },
  93. })