vite.config.ts 2.0 KB

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