vite.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'node:path'
  3. import uni from '@dcloudio/vite-plugin-uni'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. import UniRouter from 'unplugin-uni-router/vite'
  6. import UnoCSS from 'unocss/vite'
  7. // https://github.com/uni-helper
  8. import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
  9. import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
  10. import UniComponents from '@uni-helper/vite-plugin-uni-components'
  11. // https://vitejs.dev/config/
  12. export default ({ command, mode }) => {
  13. const isDevelopment = mode === 'development'
  14. const env = loadEnv(mode, path.resolve(process.cwd()))
  15. const {
  16. VITE_APP_API_BASEURL,
  17. VITE_APP_PROXY,
  18. VITE_APP_PROXY_PREFIX,
  19. VITE_DELETE_CONSOLE,
  20. VITE_SHOW_SOURCEMAP
  21. } = env
  22. return defineConfig({
  23. build: {
  24. /** 解决 Windows 下开发模式控制台提示崩溃的问题 */
  25. ...(isDevelopment
  26. ? {
  27. watch: {
  28. exclude: ['node_modules/**', '/__uno.css']
  29. }
  30. }
  31. : {}),
  32. // minify: false,
  33. // 方便非h5端调试
  34. sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
  35. target: 'es6',
  36. // 开发环境不用压缩
  37. minify: isDevelopment ? false : 'terser',
  38. terserOptions: {
  39. compress: {
  40. drop_console: VITE_DELETE_CONSOLE === 'true',
  41. drop_debugger: true
  42. }
  43. }
  44. },
  45. resolve: {
  46. alias: {
  47. '@': path.resolve('./src'),
  48. '@root': path.resolve('./'),
  49. '@img': path.resolve('./src/static/images')
  50. }
  51. },
  52. plugins: [
  53. UniComponents({
  54. dts: false,
  55. resolvers: []
  56. }),
  57. UniLayouts(),
  58. UniManifest(),
  59. UniRouter(),
  60. uni(),
  61. AutoImport({
  62. dts: false,
  63. imports: [
  64. 'vue',
  65. 'uni-app',
  66. 'pinia',
  67. {
  68. '@/api': ['useRequest']
  69. }
  70. ],
  71. dirs: ['./src/store/modules/**', './src/hooks/**', './src/router/helper.js'],
  72. eslintrc: {
  73. enabled: true,
  74. globalsPropValue: true
  75. }
  76. }),
  77. UnoCSS()
  78. ],
  79. server: {
  80. host: '0.0.0.0',
  81. port: '8888',
  82. hmr: true,
  83. // 仅 H5 端生效,其他端不生效(其他端走build,不走devServer)
  84. proxy: JSON.parse(VITE_APP_PROXY)
  85. ? {
  86. [VITE_APP_PROXY_PREFIX]: {
  87. target: VITE_APP_API_BASEURL,
  88. changeOrigin: true
  89. // rewrite: (path) => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), '')
  90. },
  91. '/loadFile': {
  92. target: VITE_APP_API_BASEURL,
  93. changeOrigin: true,
  94. ws: true,
  95. rewrite: (path) => path.replace(new RegExp('^' + '/loadFile'), '')
  96. }
  97. }
  98. : undefined
  99. },
  100. css: {
  101. preprocessorOptions: {
  102. scss: {
  103. additionalData: `@import "nutui-uniapp/styles/variables.scss";`,
  104. api: 'modern-compiler',
  105. // 忽略警告提示
  106. silenceDeprecations: ['legacy-js-api', 'import']
  107. }
  108. }
  109. }
  110. })
  111. }