vite.config.ts 1.3 KB

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