vite.config.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // base: '/h5/',
  11. plugins: [uni()],
  12. // envDir: resolve(__dirname, 'env'),
  13. define: {
  14. __VITE_BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
  15. },
  16. server: {
  17. port: 5174
  18. }
  19. }
  20. } else if (mode === 'production') {
  21. // 生产环境配置
  22. return {
  23. base: '/h5/',
  24. plugins: [uni()],
  25. // envDir: resolve(__dirname, 'env'),
  26. define: {
  27. __VITE_BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
  28. },
  29. // 删除console
  30. build: {
  31. minify: 'terser',
  32. terserOptions: {
  33. compress: {
  34. drop_console: true
  35. }
  36. }
  37. }
  38. }
  39. } else {
  40. // 本地开发环境配置
  41. return {
  42. plugins: [uni()],
  43. // envDir: resolve(__dirname, 'env'),
  44. define: {
  45. __VITE_BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
  46. },
  47. server: {
  48. port: 5174
  49. }
  50. }
  51. }
  52. })