vite.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // https://vite.dev/config/
  10. export default ({ mode }: any) => {
  11. const env = loadEnv(mode, envDir);
  12. let proxy: any = {};
  13. if (env.VITE_MOCK_ENV) {
  14. const mockEnv = loadEnv(env.VITE_MOCK_ENV, envDir);
  15. console.log('mockEnv', mockEnv)
  16. const getProxy = (prev: string, api: string) => ({
  17. target: api,
  18. changeOrigin: true,
  19. rewrite: (path: any) => path.replace(prev, ""),
  20. });
  21. if (env.VITE_MOCK_PROXY) {
  22. proxy[env.VITE_MOCK_PROXY] = getProxy(
  23. env.VITE_MOCK_PROXY,
  24. mockEnv.VITE_MOCK_PROXY
  25. );
  26. } else {
  27. for (const key in env) {
  28. if (env[key].includes("/") && env[key] !== mockEnv[key]) {
  29. proxy[env[key]] = getProxy(env[key], mockEnv[key]);
  30. }
  31. }
  32. }
  33. console.log(proxy)
  34. }
  35. const outDir = env.VITE_BUILD_DIR || ''
  36. return defineConfig({
  37. envDir: envDir,
  38. build: {
  39. outDir: `dist/${outDir}`,
  40. rollupOptions: {
  41. // 防止打包时处理指定目录
  42. external: [
  43. /^\/static\/models\/.*/, // 正则匹配 /static/models/ 下的所有文件
  44. ],
  45. },
  46. },
  47. base: "./",
  48. resolve: {
  49. alias: {
  50. "@/": `${path.resolve(__dirname, "src")}/`,
  51. },
  52. },
  53. css: {
  54. preprocessorOptions: {
  55. scss: {
  56. quietDeps: true,
  57. additionalData: `
  58. @forward 'element-plus/theme-chalk/src/common/var' with (
  59. $colors: (
  60. 'primary': (
  61. 'base': ${env.VITE_PRIMARY},
  62. ),
  63. ),
  64. );
  65. `,
  66. },
  67. },
  68. },
  69. server: {
  70. port: 9010,
  71. open: true,
  72. host: "0.0.0.0",
  73. proxy: proxy,
  74. },
  75. plugins: [
  76. {
  77. name: "remove-static-models",
  78. closeBundle() {
  79. const dir = path.resolve(__dirname, "dist/static/models");
  80. if (fs.existsSync(dir)) {
  81. fs.rmSync(dir, { recursive: true, force: true });
  82. console.log("Removed dist/static/models directory after build.");
  83. }
  84. },
  85. },
  86. createSvgIconsPlugin({
  87. iconDirs: [path.resolve(process.cwd(), "public/icons")],
  88. symbolId: "icon-[dir]-[name]",
  89. }),
  90. createHtmlPlugin({
  91. template: "index.html",
  92. entry: `/src${env.VITE_ENTRY}`,
  93. inject: {
  94. data: {
  95. title: env.VITE_TITLE,
  96. },
  97. },
  98. }),
  99. vue(),
  100. ],
  101. define: {
  102. __VERSION__: JSON.stringify(version),
  103. },
  104. });
  105. };