vite.config.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. resolve: {
  34. alias: {
  35. "@/": `${path.resolve(__dirname, "src")}/`,
  36. },
  37. },
  38. css: {
  39. preprocessorOptions: {
  40. scss: {
  41. quietDeps: true,
  42. additionalData: `
  43. @forward 'element-plus/theme-chalk/src/common/var' with (
  44. $colors: (
  45. 'primary': (
  46. 'base': ${env.VITE_PRIMARY},
  47. ),
  48. ),
  49. );
  50. `,
  51. },
  52. },
  53. },
  54. server: {
  55. port: 9010,
  56. open: true,
  57. host: "0.0.0.0",
  58. proxy: proxy,
  59. },
  60. plugins: [
  61. createSvgIconsPlugin({
  62. iconDirs: [path.resolve(process.cwd(), "public/icons")],
  63. symbolId: "icon-[dir]-[name]",
  64. }),
  65. createHtmlPlugin({
  66. template: "index.html",
  67. entry: `/src${env.VITE_ENTRY}`,
  68. inject: {
  69. data: {
  70. title: env.VITE_TITLE,
  71. },
  72. },
  73. }),
  74. vue(),
  75. ],
  76. define: {
  77. __VERSION__: JSON.stringify(version),
  78. }
  79. });
  80. };