vite.config.ts 1.3 KB

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