vite.config.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { defineConfig } from "vite";
  2. import react from "@vitejs/plugin-react";
  3. import path, { resolve } from "path";
  4. import electron from "vite-plugin-electron";
  5. import electronRenderer from "vite-plugin-electron-renderer";
  6. import pkg from "./package.json";
  7. import viteCompression from "vite-plugin-compression";
  8. // https://vitejs.dev/config/
  9. export default defineConfig(({ command }) => {
  10. const isServe = command === "serve";
  11. const isBuild = command === "build";
  12. const sourcemap = isServe || !!process.env.VSCODE_DEBUG;
  13. return {
  14. base: "./",
  15. publicDir: path.resolve(__dirname, "public"),
  16. plugins: [
  17. react(),
  18. electron([
  19. {
  20. // Main-Process entry file of the Electron App.
  21. entry: "electron/main",
  22. onstart(options) {
  23. if (process.env.VSCODE_DEBUG) {
  24. console.log(
  25. /* For `.vscode/.debug.script.mjs` */ "[startup] Electron App"
  26. );
  27. } else {
  28. options.startup();
  29. }
  30. },
  31. vite: {
  32. build: {
  33. sourcemap,
  34. minify: isBuild,
  35. outDir: "dist-electron/main",
  36. rollupOptions: {
  37. external: Object.keys(
  38. "dependencies" in pkg ? pkg.dependencies : {}
  39. ),
  40. },
  41. },
  42. },
  43. },
  44. {
  45. entry: "electron/preload",
  46. onstart(options) {
  47. // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
  48. // instead of restarting the entire Electron App.
  49. options.reload();
  50. },
  51. vite: {
  52. build: {
  53. sourcemap: sourcemap ? "inline" : undefined, // #332
  54. minify: isBuild,
  55. outDir: "dist-electron/preload",
  56. rollupOptions: {
  57. external: Object.keys(
  58. "dependencies" in pkg ? pkg.dependencies : {}
  59. ),
  60. },
  61. },
  62. },
  63. },
  64. // {
  65. // entry: "electron/sqlite",
  66. // onstart(options) {
  67. // options.reload();
  68. // },
  69. // vite: {
  70. // build: {
  71. // sourcemap: sourcemap ? "inline" : undefined, // #332
  72. // minify: isBuild,
  73. // outDir: "dist-electron/sqlite",
  74. // rollupOptions: {
  75. // external: Object.keys(
  76. // "dependencies" in pkg ? pkg.dependencies : {}
  77. // ),
  78. // },
  79. // },
  80. // },
  81. // },
  82. // {
  83. // entry: "electron/db/database.db",
  84. // onstart(options) {
  85. // options.reload();
  86. // },
  87. // vite: {
  88. // build: {
  89. // outDir: "dist-electron/db/database",
  90. // },
  91. // },
  92. // },
  93. ]),
  94. electronRenderer(),
  95. // 不知道后端是否支持,暂时注释掉
  96. viteCompression({
  97. // gzip静态资源压缩配置
  98. verbose: true,
  99. disable: false,
  100. threshold: 10240,
  101. algorithm: "gzip",
  102. ext: ".gz",
  103. }),
  104. ],
  105. build: {
  106. // emptyOutDir: false, // 默认情况下,若 outDir 在 root 目录下,则 Vite 会在构建时清空该目录
  107. outDir: "dist-electron",
  108. cssCodeSplit: true,
  109. chunkSizeWarningLimit: 1500,
  110. rollupOptions: {
  111. input: "index.html",
  112. output: {
  113. // 静态资源打包做处理
  114. chunkFileNames: "assets/js/[name]-[hash].js",
  115. entryFileNames: "assets/js/[name]-[hash].js",
  116. assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
  117. manualChunks(id) {
  118. if (id.includes("node_modules")) {
  119. return id
  120. .toString()
  121. .split("node_modules/")[1]
  122. .split("/")[0]
  123. .toString();
  124. }
  125. },
  126. },
  127. },
  128. terserOptions: {
  129. // 清除console和debugger
  130. compress: {
  131. drop_console: true,
  132. drop_debugger: true,
  133. },
  134. },
  135. },
  136. server: {
  137. host: "localhost",
  138. port: 3010,
  139. strictPort: true,
  140. proxy: {
  141. "/api": {
  142. target: "http://localhost:3003",
  143. changeOrigin: true,
  144. // rewrite: (path) => path.replace(new RegExp(`/api`), "/api"),
  145. },
  146. "/notification": {
  147. target: "http://114.215.252.134:8989",
  148. changeOrigin: true,
  149. rewrite: (path) => path.replace(/^\/notification/, ""),
  150. },
  151. "/userLogin": {
  152. target: "http://182.44.10.206:8000",
  153. changeOrigin: true,
  154. rewrite: (path) => path.replace(/^\/userLogin/, ""),
  155. },
  156. },
  157. },
  158. resolve: {
  159. alias: {
  160. "@": resolve(".", "./src"),
  161. },
  162. },
  163. };
  164. });