electron.vite.config.ts 2.6 KB

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