vite.config.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { defineConfig, loadEnv } from 'vite';
  2. import path from 'path';
  3. import vue from '@vitejs/plugin-vue';
  4. import { viteVConsole } from 'vite-plugin-vconsole';
  5. export default defineConfig(({ mode, command }) => {
  6. // 加载环境配置
  7. const env = loadEnv(mode, path.join(process.cwd(), 'env'));
  8. return {
  9. base: '/',// 基础路径
  10. envDir: './env',// 环境目录
  11. css: {
  12. preprocessorOptions: {
  13. scss: {// scss配置全局导入
  14. additionalData: '@import "./src/styles/variables";'
  15. },
  16. }
  17. },
  18. resolve: {
  19. // 别名
  20. alias: [
  21. {
  22. find: '/@',
  23. replacement: path.resolve(__dirname, './src'),
  24. }
  25. ],
  26. },
  27. server: {
  28. // 监听所有地址
  29. host: '0.0.0.0',
  30. // 自定义端口号
  31. port: 3010,
  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: 'dist',// 指定打包文件根目录
  52. sourcemap: false,// 构建后不生成源代码
  53. write: true,// 构建的文件写入磁盘
  54. chunkSizeWarningLimit: 10240,// 触发警告的chunk大小10M
  55. },
  56. esbuild: {
  57. drop: command === 'build' ? ['console', 'debugger'] : [],
  58. },
  59. plugins: [
  60. vue(),
  61. viteVConsole({
  62. entry: path.resolve(__dirname, './src/main.ts'),// 入口文件
  63. localEnabled: command === 'serve',// serve开发环境下
  64. enabled: command === 'serve',// 本地运行时开启
  65. config: {// 配置
  66. maxLogNumber: 1000,
  67. theme: 'light'
  68. }
  69. }),
  70. ],
  71. }
  72. })