vite.config.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { defineConfig } from "vite";
  2. import vue from "@vitejs/plugin-vue";
  3. import path from "path";
  4. import { loadEnv } from "vite";
  5. import AutoImport from "unplugin-auto-import/vite";
  6. import Components from "unplugin-vue-components/vite";
  7. import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
  8. export default defineConfig(({ mode }) => {
  9. const env = loadEnv(mode, process.cwd());
  10. const { VITE_APP_ENV, VITE_APP_BASE_API, VITE_APP_URL } = env;
  11. let buildConfig = {};
  12. if (mode === "test") {
  13. buildConfig = {
  14. outDir: "dist",
  15. // 测试环境的其他特定构建配置
  16. };
  17. } else if (mode === "prod") {
  18. buildConfig = {
  19. outDir: "dist",
  20. minify: "terser",
  21. // 生产环境的其他特定构建配置
  22. };
  23. }
  24. return {
  25. base: VITE_APP_ENV === "production" ? "/" : "/",
  26. plugins: [
  27. vue(),
  28. AutoImport({
  29. resolvers: [ElementPlusResolver()],
  30. }),
  31. Components({
  32. resolvers: [ElementPlusResolver()],
  33. }),
  34. ],
  35. css: {
  36. preprocessorOptions: {
  37. less: {
  38. javascriptEnabled: true,
  39. },
  40. },
  41. },
  42. resolve: {
  43. alias: {
  44. "@": path.resolve(__dirname, "./src"),
  45. },
  46. extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  47. },
  48. server: {
  49. port: 80,
  50. host: true,
  51. hmr: true,
  52. open: true,
  53. proxy: {
  54. [VITE_APP_BASE_API]: {
  55. target: VITE_APP_URL,
  56. changeOrigin: true,
  57. rewrite: (p) => p.replace(/^\/dev-api/, ""),
  58. },
  59. },
  60. },
  61. build: buildConfig,
  62. };
  63. });