vite.config.ts 1.8 KB

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