vite.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. },
  41. base: "./",
  42. resolve: {
  43. alias: {
  44. "@/": `${path.resolve(__dirname, "src")}/`,
  45. },
  46. },
  47. css: {
  48. preprocessorOptions: {
  49. scss: {
  50. quietDeps: true,
  51. additionalData: `
  52. @forward 'element-plus/theme-chalk/src/common/var' with (
  53. $colors: (
  54. 'primary': (
  55. 'base': ${env.VITE_PRIMARY},
  56. ),
  57. ),
  58. );
  59. `,
  60. },
  61. },
  62. },
  63. server: {
  64. port: 9010,
  65. open: true,
  66. host: "0.0.0.0",
  67. proxy: proxy,
  68. },
  69. plugins: [
  70. {
  71. name: "remove-static-models",
  72. closeBundle() {
  73. const dir = path.resolve(__dirname, 'dist', outDir || '', "static/models");
  74. console.log('remove dist', dir)
  75. if (fs.existsSync(dir)) {
  76. fs.rmSync(dir, { recursive: true, force: true });
  77. console.log("Removed dist/static/models directory after build.");
  78. }
  79. },
  80. },
  81. createSvgIconsPlugin({
  82. iconDirs: [path.resolve(process.cwd(), "public/icons")],
  83. symbolId: "icon-[dir]-[name]",
  84. }),
  85. createHtmlPlugin({
  86. template: "index.html",
  87. entry: `/src${env.VITE_ENTRY}`,
  88. inject: {
  89. data: {
  90. title: env.VITE_TITLE,
  91. },
  92. },
  93. }),
  94. vue(),
  95. ],
  96. define: {
  97. __VERSION__: JSON.stringify(version),
  98. },
  99. });
  100. };