vite.config.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { defineConfig, loadEnv } from "vite";
  2. import vue from "@vitejs/plugin-vue";
  3. import path from "node:path";
  4. import { createHtmlPlugin } from "vite-plugin-html";
  5. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  6. import { version } from "./package.json";
  7. import fs from "fs";
  8. const envDir = process.cwd() + "/profile";
  9. function getChinaTimeISO() {
  10. const d = new Date();
  11. // 将时间转换为 UTC 毫秒数,然后加上 +8 小时
  12. const utcMs = d.getTime() + d.getTimezoneOffset() * 60000;
  13. const chinaMs = utcMs + 8 * 3600 * 1000;
  14. const chinaDate = new Date(chinaMs);
  15. // 返回类似 "2025-11-24T15:30:00+08:00"
  16. const pad = (n: number) => String(n).padStart(2, "0");
  17. const Y = chinaDate.getFullYear();
  18. const M = pad(chinaDate.getMonth() + 1);
  19. const D = pad(chinaDate.getDate());
  20. const h = pad(chinaDate.getHours());
  21. const m = pad(chinaDate.getMinutes());
  22. const s = pad(chinaDate.getSeconds());
  23. // 手动拼 +08:00 时区偏移
  24. return `${Y}-${M}-${D}T${h}:${m}:${s}+08:00`;
  25. }
  26. // https://vite.dev/config/
  27. export default ({ mode }: any) => {
  28. const env = loadEnv(mode, envDir);
  29. const buildTime = getChinaTimeISO();
  30. console.log('current', buildTime)
  31. let proxy: any = {};
  32. if (env.VITE_MOCK_ENV) {
  33. const mockEnv = loadEnv(env.VITE_MOCK_ENV, envDir);
  34. console.log("mockEnv", mockEnv);
  35. const getProxy = (prev: string, api: string) => ({
  36. target: api,
  37. changeOrigin: true,
  38. rewrite: (path: any) => path.replace(prev, ""),
  39. });
  40. if (env.VITE_MOCK_PROXY) {
  41. proxy[env.VITE_MOCK_PROXY] = getProxy(
  42. env.VITE_MOCK_PROXY,
  43. mockEnv.VITE_MOCK_PROXY
  44. );
  45. } else {
  46. for (const key in env) {
  47. if (env[key].includes("/") && env[key] !== mockEnv[key]) {
  48. proxy[env[key]] = getProxy(env[key], mockEnv[key]);
  49. }
  50. }
  51. }
  52. console.log(proxy);
  53. }
  54. const outDir = env.VITE_BUILD_DIR || "";
  55. return defineConfig({
  56. envDir: envDir,
  57. build: {
  58. outDir: `dist/${outDir}`,
  59. },
  60. base: "./",
  61. resolve: {
  62. alias: {
  63. "@/": `${path.resolve(__dirname, "src")}/`,
  64. },
  65. },
  66. css: {
  67. preprocessorOptions: {
  68. scss: {
  69. quietDeps: true,
  70. additionalData: `
  71. @forward 'element-plus/theme-chalk/src/common/var' with (
  72. $colors: (
  73. 'primary': (
  74. 'base': ${env.VITE_PRIMARY},
  75. ),
  76. ),
  77. );
  78. `,
  79. },
  80. },
  81. },
  82. server: {
  83. port: 9010,
  84. open: true,
  85. host: "0.0.0.0",
  86. proxy: proxy,
  87. },
  88. plugins: [
  89. {
  90. name: "remove-static-models",
  91. closeBundle() {
  92. const dir = path.resolve(
  93. __dirname,
  94. "dist",
  95. outDir || "",
  96. "static/models"
  97. );
  98. console.log("remove dist", dir);
  99. if (fs.existsSync(dir)) {
  100. fs.rmSync(dir, { recursive: true, force: true });
  101. console.log("Removed dist/static/models directory after build.");
  102. }
  103. },
  104. },
  105. createSvgIconsPlugin({
  106. iconDirs: [path.resolve(process.cwd(), "public/icons")],
  107. symbolId: "icon-[dir]-[name]",
  108. }),
  109. createHtmlPlugin({
  110. template: "index.html",
  111. entry: `/src${env.VITE_ENTRY}`,
  112. inject: {
  113. data: {
  114. title: env.VITE_TITLE,
  115. },
  116. },
  117. }),
  118. vue(),
  119. ],
  120. define: {
  121. __VERSION__: JSON.stringify(version),
  122. __BUILD_TIME__: JSON.stringify(buildTime),
  123. },
  124. });
  125. };