electron.vite.config.ts 3.1 KB

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