vite.config.ts 1.3 KB

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