vite.config.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { defineConfig, loadEnv } from 'vite'
  2. import uni from '@dcloudio/vite-plugin-uni'
  3. // import { resolve } from 'path'
  4. export default defineConfig(({ command, mode }) => {
  5. // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
  6. const env = loadEnv(mode, process.cwd(), '')
  7. if (mode === 'development' || mode === 'sandbox') {
  8. // 测试环境配置
  9. return {
  10. plugins: [uni()],
  11. // envDir: resolve(__dirname, 'env'),
  12. define: {
  13. __VITE_BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
  14. },
  15. server: {
  16. port: 5174
  17. }
  18. }
  19. } else if (mode === 'production') {
  20. // 生产环境配置
  21. return {
  22. plugins: [uni()],
  23. // envDir: resolve(__dirname, 'env'),
  24. define: {
  25. __VITE_BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
  26. },
  27. // 删除console
  28. build: {
  29. minify: 'terser',
  30. terserOptions: {
  31. compress: {
  32. drop_console: true
  33. }
  34. }
  35. }
  36. }
  37. } else {
  38. // 本地开发环境配置
  39. return {
  40. plugins: [uni()],
  41. // envDir: resolve(__dirname, 'env'),
  42. define: {
  43. __VITE_BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
  44. },
  45. server: {
  46. port: 5174
  47. }
  48. }
  49. }
  50. })