vite.config.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/-captchaImage': {
  41. // 开启跨域
  42. changeOrigin: true,
  43. // 转发地址
  44. target: env.VITE_API_URL_img,
  45. // 路径重写
  46. rewrite: (path) => path.replace(/^\/api\/-captchaImage/i, '')
  47. },
  48. '/api': {
  49. // 开启跨域
  50. changeOrigin: true,
  51. // 转发地址
  52. target: env.VITE_API_URL,
  53. // 路径重写
  54. rewrite: (path) => path.replace(/^\/api/, ''),
  55. }
  56. }
  57. },
  58. build: {
  59. outDir: 'build',// 打包文件目录
  60. chunkSizeWarningLimit: 10240,// chunk大于10M触发警告
  61. // 底层配置
  62. rollupOptions: {
  63. output: {
  64. entryFileNames: 'js/[name]-[hash].js',
  65. chunkFileNames: 'js/[name]-[hash].js',
  66. }
  67. },
  68. },
  69. esbuild: {
  70. drop: command === 'build' ? ['console', 'debugger'] : [],
  71. },
  72. plugins: [
  73. [react()],// React编译
  74. ],
  75. }
  76. })