vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. build: {
  35. rollupOptions: {
  36. // 防止打包时处理指定目录
  37. external: [
  38. /^\/static\/models\/.*/, // 正则匹配 /static/models/ 下的所有文件
  39. ],
  40. },
  41. },
  42. base: "./",
  43. resolve: {
  44. alias: {
  45. "@/": `${path.resolve(__dirname, "src")}/`,
  46. },
  47. },
  48. css: {
  49. preprocessorOptions: {
  50. scss: {
  51. quietDeps: true,
  52. additionalData: `
  53. @forward 'element-plus/theme-chalk/src/common/var' with (
  54. $colors: (
  55. 'primary': (
  56. 'base': ${env.VITE_PRIMARY},
  57. ),
  58. ),
  59. );
  60. `,
  61. },
  62. },
  63. },
  64. server: {
  65. port: 9010,
  66. open: true,
  67. host: "0.0.0.0",
  68. proxy: proxy,
  69. },
  70. plugins: [
  71. createSvgIconsPlugin({
  72. iconDirs: [path.resolve(process.cwd(), "public/icons")],
  73. symbolId: "icon-[dir]-[name]",
  74. }),
  75. createHtmlPlugin({
  76. template: "index.html",
  77. entry: `/src${env.VITE_ENTRY}`,
  78. inject: {
  79. data: {
  80. title: env.VITE_TITLE,
  81. },
  82. },
  83. }),
  84. vue(),
  85. ],
  86. define: {
  87. __VERSION__: JSON.stringify(version),
  88. },
  89. });
  90. };