vite.config.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // https://vite.dev/config/
  8. export default ({ mode }: any) => {
  9. const env = loadEnv(mode, process.cwd());
  10. let proxy: any = {};
  11. if (env.VITE_MOCK_ENV) {
  12. const mockEnv = loadEnv(env.VITE_MOCK_ENV, process.cwd());
  13. const getProxy = (prev: string, api: string) => ({
  14. target: api,
  15. changeOrigin: true,
  16. rewrite: (path: any) => path.replace(prev, ""),
  17. });
  18. if (env.VITE_MOCK_PROXY) {
  19. proxy[env.VITE_MOCK_PROXY] = getProxy(
  20. env.VITE_MOCK_PROXY,
  21. mockEnv.VITE_MOCK_PROXY
  22. );
  23. } else {
  24. for (const key in env) {
  25. if (env[key].includes("/") && env[key] !== mockEnv[key]) {
  26. proxy[env[key]] = getProxy(env[key], mockEnv[key]);
  27. }
  28. }
  29. }
  30. }
  31. console.log(proxy)
  32. return defineConfig({
  33. base: './',
  34. resolve: {
  35. alias: {
  36. "@/": `${path.resolve(__dirname, "src")}/`,
  37. },
  38. },
  39. css: {
  40. preprocessorOptions: {
  41. scss: {
  42. quietDeps: true,
  43. additionalData: `
  44. @forward 'element-plus/theme-chalk/src/common/var' with (
  45. $colors: (
  46. 'primary': (
  47. 'base': ${env.VITE_PRIMARY},
  48. ),
  49. ),
  50. );
  51. `,
  52. },
  53. },
  54. },
  55. server: {
  56. port: 9010,
  57. open: true,
  58. host: "0.0.0.0",
  59. proxy: proxy,
  60. },
  61. plugins: [
  62. createSvgIconsPlugin({
  63. iconDirs: [path.resolve(process.cwd(), "public/icons")],
  64. symbolId: "icon-[dir]-[name]",
  65. }),
  66. createHtmlPlugin({
  67. template: "index.html",
  68. entry: `/src${env.VITE_ENTRY}`,
  69. inject: {
  70. data: {
  71. title: env.VITE_TITLE,
  72. },
  73. },
  74. }),
  75. vue(),
  76. ],
  77. define: {
  78. __VERSION__: JSON.stringify(version),
  79. }
  80. });
  81. };