vite.config.ts 2.0 KB

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