vite.config.ts 1.7 KB

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