electron.vite.config.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { sentryVitePlugin } from "@sentry/vite-plugin"
  2. import { defineConfig } from "electron-vite"
  3. import appPlugin from "@kirincode-ai/app/vite"
  4. import * as fs from "node:fs/promises"
  5. const KIRINCODE_SERVER_DIST = "../kirincode/dist/node"
  6. const channel = (() => {
  7. const raw = process.env.KIRINCODE_CHANNEL
  8. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  9. if (process.env.KIRINCODE_CHANNEL === "latest") return "prod"
  10. return "dev"
  11. })()
  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.KIRINCODE_CHANNEL": JSON.stringify(channel),
  32. },
  33. build: {
  34. rollupOptions: {
  35. input: { index: "src/main/index.ts", sidecar: "src/main/sidecar.ts", "admin-server": "src/main/admin-server.ts" },
  36. // Keep this identical to electron-vite's Node 20.11+ shim. Its regex insertion can
  37. // corrupt bundled TypeScript, while a Rollup banner places the shim safely.
  38. output: {
  39. banner: `
  40. // -- CommonJS Shims --
  41. import __cjs_mod__ from 'node:module';
  42. const __filename = import.meta.filename;
  43. const __dirname = import.meta.dirname;
  44. const require = __cjs_mod__.createRequire(import.meta.url);
  45. `,
  46. },
  47. },
  48. externalizeDeps: { include: ["@lydell/node-pty"] },
  49. },
  50. plugins: [
  51. {
  52. name: "kirincode:virtual-server-module",
  53. enforce: "pre",
  54. resolveId(id) {
  55. if (id === "virtual:kirincode-server") return this.resolve(`${KIRINCODE_SERVER_DIST}/node.js`)
  56. },
  57. },
  58. {
  59. name: "kirincode:copy-server-assets",
  60. async writeBundle() {
  61. for (const l of await fs.readdir(KIRINCODE_SERVER_DIST)) {
  62. if (!l.endsWith(".wasm")) continue
  63. await fs.writeFile(`./out/main/chunks/${l}`, await fs.readFile(`${KIRINCODE_SERVER_DIST}/${l}`))
  64. }
  65. },
  66. },
  67. ],
  68. },
  69. preload: {
  70. build: {
  71. rollupOptions: {
  72. input: { index: "src/preload/index.ts" },
  73. output: {
  74. format: "cjs",
  75. entryFileNames: "[name].js",
  76. },
  77. },
  78. },
  79. },
  80. renderer: {
  81. plugins: [appPlugin, sentry],
  82. publicDir: "../../../app/public",
  83. root: "src/renderer",
  84. build: {
  85. sourcemap: true,
  86. rollupOptions: {
  87. input: {
  88. main: "src/renderer/index.html",
  89. },
  90. },
  91. },
  92. },
  93. })