vite.config.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import path from 'node:path'
  2. import { isH5 } from '@uni-helper/uni-env'
  3. import { defineConfig } from 'vite'
  4. import { loadMapEnv } from './helpers/load-map-env/index.js'
  5. import postcss from './postcss.config.js'
  6. import plugins from './vite.config.plugins.js'
  7. import Components from '@uni-helper/vite-plugin-uni-components' // 引入组件自动导入插件
  8. import { uViewProResolver } from '@uni-helper/vite-plugin-uni-components/resolvers'
  9. import UniKuRoot from '@uni-ku/root'
  10. export default defineConfig(({ mode }) => {
  11. const env = loadMapEnv(mode)
  12. const isUseProxy = env.VITE_PROXY_USE === '1'
  13. const appBase = env.VITE_APP_BASE
  14. const proxy = {}
  15. if (isUseProxy && isH5) {
  16. const proxyPath = env.VITE_PROXY_PATH
  17. const apiOrigin = env.VITE_API_ORIGIN
  18. const apiPath = env.VITE_API_PATH
  19. proxy[proxyPath] = {
  20. target: apiOrigin,
  21. changeOrigin: true,
  22. rewrite: (path) => {
  23. return path.replace(new RegExp(`^${proxyPath}`), apiPath)
  24. },
  25. }
  26. }
  27. return {
  28. base: appBase,
  29. server: {
  30. port: 5174,
  31. cors: true,
  32. proxy: {
  33. ...proxy,
  34. },
  35. },
  36. resolve: {
  37. alias: {
  38. '@': path.resolve('./src'),
  39. '@root': path.resolve('./'),
  40. '$uni-router': path.resolve('./helpers/uni-router'),
  41. '$unocss-preset-shades': path.resolve('./helpers/unocss-preset-shades'),
  42. },
  43. },
  44. css: {
  45. /** 解决外部 postcss.config.js 不被解析的问题 */
  46. postcss,
  47. },
  48. build: {
  49. // minify: false,
  50. sourcemap: false
  51. },
  52. define: {
  53. 'process.env': env,
  54. },
  55. plugins: [
  56. // 加载原有插件
  57. ...plugins({ env }),
  58. UniKuRoot(),
  59. // 添加组件自动导入插件,并配置 uViewPro 解析器
  60. Components({
  61. resolvers: [
  62. uViewProResolver() // 启用 uViewPro 组件自动解析
  63. ]
  64. }),
  65. ],
  66. }
  67. })