vite.config.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { rmSync } from "node:fs";
  2. import path from "node:path";
  3. import { defineConfig } from "vite";
  4. import react from "@vitejs/plugin-react";
  5. import electron from "vite-electron-plugin";
  6. import { customStart, loadViteEnv } from "vite-electron-plugin/plugin";
  7. import pkg from "./package.json";
  8. import legacy from "@vitejs/plugin-legacy";
  9. import { createRequire } from "node:module";
  10. const require = createRequire(import.meta.url);
  11. // import visualizer from "rollup-plugin-visualizer";
  12. // https://vitejs.dev/config/
  13. export default defineConfig(({ command }) => {
  14. rmSync("dist-electron", { recursive: true, force: true });
  15. const sourcemap = command === "serve" || !!process.env.VSCODE_DEBUG;
  16. return {
  17. resolve: {
  18. alias: {
  19. "@": path.join(__dirname, "src"),
  20. },
  21. },
  22. plugins: [
  23. react(),
  24. electron({
  25. include: ["electron"],
  26. transformOptions: {
  27. sourcemap,
  28. },
  29. plugins: [
  30. ...(!!process.env.VSCODE_DEBUG
  31. ? [
  32. // Will start Electron via VSCode Debug
  33. customStart(() =>
  34. console.log(
  35. /* For `.vscode/.debug.script.mjs` */ "[startup] Electron App",
  36. ),
  37. ),
  38. ]
  39. : []),
  40. // Allow use `import.meta.env.VITE_SOME_KEY` in Electron-Main
  41. loadViteEnv(),
  42. ],
  43. }),
  44. // legacy({
  45. // targets: ["defaults", "not IE 11"],
  46. // }),
  47. // visualizer({ open: true }),
  48. ],
  49. server: !!process.env.VSCODE_DEBUG
  50. ? (() => {
  51. const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL);
  52. return {
  53. host: url.hostname,
  54. port: +url.port,
  55. };
  56. })()
  57. : undefined,
  58. clearScreen: false,
  59. build: {
  60. sourcemap: false,
  61. cssCodeSplit: true,
  62. chunkSizeWarningLimit: 500,
  63. terserOptions: {
  64. compress: {
  65. drop_console: true,
  66. drop_debugger: true,
  67. },
  68. },
  69. rollupOptions: {
  70. output: {
  71. // manualChunks(id) {
  72. // if (
  73. // id.includes("node_modules") &&
  74. // !id.includes("rc") &&
  75. // !id.includes("ant")
  76. // ) {
  77. // return id.toString().split("node_modules/")[1].split("/")[0].toString();
  78. // }
  79. // },
  80. },
  81. },
  82. },
  83. };
  84. });