vite.config.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { defineConfig } from 'vite'
  2. import uni from '@dcloudio/vite-plugin-uni'
  3. import path from 'path'
  4. // https://vitejs.dev/config/
  5. export default defineConfig(({ command, mode }) => {
  6. const isApp = process.env.UNI_PLATFORM === 'app' || process.env.UNI_PLATFORM === 'app-harmony'
  7. return {
  8. plugins: [uni()],
  9. resolve: {
  10. alias: {
  11. '@': path.resolve(__dirname, './'),
  12. // 添加 uview-plus 别名
  13. 'uview-plus': path.resolve(__dirname, './node_modules/uview-plus')
  14. }
  15. },
  16. optimizeDeps: {
  17. include: ['uview-plus']
  18. },
  19. css: {
  20. preprocessorOptions: {
  21. scss: {
  22. // 可以在这里添加全局 scss 变量
  23. // 取消sass废弃API的报警
  24. silenceDeprecations: ['legacy-js-api', 'color-functions', 'import'],
  25. }
  26. }
  27. },
  28. server: {
  29. proxy: {
  30. // H5 开发环境代理配置
  31. '/base': {
  32. target: 'http://localhost:8080',
  33. changeOrigin: true,
  34. secure: false,
  35. rewrite: (path) => path.replace(/^\/base/, ''),
  36. },
  37. '/uniapp': {
  38. target: 'http://localhost:8080',
  39. changeOrigin: true,
  40. secure: false,
  41. rewrite: (path) => path.replace(/^\/base/, ''),
  42. },
  43. },
  44. },
  45. build: {
  46. // 生产环境移除 console
  47. minify: 'terser',
  48. terserOptions: {
  49. compress: {
  50. drop_console: true,
  51. drop_debugger: true
  52. }
  53. },
  54. // App 平台(包括鸿蒙)禁用代码分割,避免 iife 格式错误
  55. rollupOptions: isApp ? {
  56. output: {
  57. format: 'es',
  58. inlineDynamicImports: true, // 关键:禁用代码分割
  59. manualChunks: undefined
  60. }
  61. } : {
  62. output: {
  63. format: 'es',
  64. manualChunks: undefined
  65. }
  66. },
  67. target: 'esnext'
  68. }
  69. }})