electron.vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Keep this identical to electron-vite's Node 20.11+ shim. Its regex insertion can
  38. // corrupt bundled TypeScript, while a Rollup banner places the shim safely.
  39. output: {
  40. banner: `
  41. // -- CommonJS Shims --
  42. import __cjs_mod__ from 'node:module';
  43. const __filename = import.meta.filename;
  44. const __dirname = import.meta.dirname;
  45. const require = __cjs_mod__.createRequire(import.meta.url);
  46. `,
  47. },
  48. },
  49. externalizeDeps: { include: [nodePtyPkg] },
  50. },
  51. plugins: [
  52. {
  53. name: "opencode:node-pty-narrower",
  54. enforce: "pre",
  55. resolveId(s) {
  56. if (s === "@lydell/node-pty") return nodePtyPkg
  57. },
  58. },
  59. {
  60. name: "opencode:virtual-server-module",
  61. enforce: "pre",
  62. resolveId(id) {
  63. if (id === "virtual:opencode-server") return this.resolve(`${OPENCODE_SERVER_DIST}/node.js`)
  64. },
  65. },
  66. {
  67. name: "opencode:copy-server-assets",
  68. async writeBundle() {
  69. for (const l of await fs.readdir(OPENCODE_SERVER_DIST)) {
  70. if (!l.endsWith(".wasm")) continue
  71. await fs.writeFile(`./out/main/chunks/${l}`, await fs.readFile(`${OPENCODE_SERVER_DIST}/${l}`))
  72. }
  73. },
  74. },
  75. ],
  76. },
  77. preload: {
  78. build: {
  79. rollupOptions: {
  80. input: { index: "src/preload/index.ts" },
  81. output: {
  82. format: "cjs",
  83. entryFileNames: "[name].js",
  84. },
  85. },
  86. },
  87. },
  88. renderer: {
  89. plugins: [appPlugin, sentry],
  90. publicDir: "../../../app/public",
  91. root: "src/renderer",
  92. build: {
  93. sourcemap: true,
  94. rollupOptions: {
  95. input: {
  96. main: "src/renderer/index.html",
  97. },
  98. },
  99. },
  100. },
  101. })