vite.config.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { defineConfig, loadEnv } from 'vite';
  2. import path from 'path';
  3. import Components from '@uni-helper/vite-plugin-uni-components';
  4. import { WotResolver } from '@uni-helper/vite-plugin-uni-components/resolvers';
  5. import uni from '@dcloudio/vite-plugin-uni';
  6. export default defineConfig(({ mode }) => {
  7. const env = loadEnv(mode, path.join(process.cwd(), 'env'));
  8. return {
  9. envDir: './env',// 环境目录
  10. css: {
  11. preprocessorOptions: {
  12. scss: {// 全局导入
  13. charset: false,
  14. additionalData: '@import "@/uni.scss";',
  15. }
  16. }
  17. },
  18. resolve: {
  19. // 别名
  20. alias: [
  21. {
  22. find: '@',
  23. replacement: path.resolve(__dirname, 'src'),
  24. }
  25. ],
  26. // 忽略文件后缀名
  27. extensions: ['.js', '.ts', '.vue', '.scss', '.json']
  28. },
  29. server: {
  30. // 监听所有地址
  31. host: '0.0.0.0',
  32. // 端口号
  33. port: 3000,
  34. // 自动打开浏览器
  35. open: true,
  36. // 热更新
  37. hmr: true,
  38. // 自动更换可用端口
  39. strictPort: false,
  40. // 代理规则
  41. proxy: {
  42. '/api': {
  43. // 开启跨域
  44. changeOrigin: true,
  45. // 转发地址
  46. target: env.VITE_API_URL,
  47. // 路径重写
  48. rewrite: (path) => path.replace(/^\/api/, ''),
  49. },
  50. }
  51. },
  52. build: {
  53. chunkSizeWarningLimit: 2048,// chunk大于2M触发警告
  54. },
  55. esbuild: {
  56. drop: mode === 'production' ? ['console', 'debugger'] : [],
  57. },
  58. plugins: [
  59. Components({// 自动导入组件
  60. resolvers: [WotResolver()],
  61. dts: 'src/typings/components.d.ts'
  62. }),
  63. [uni()],// Uni-App编译
  64. ],
  65. }
  66. })