vite.config.ts 2.0 KB

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