vite.config.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const env = loadEnv(mode, path.join(process.cwd(), 'env'));
  7. return {
  8. base: '/',// 基础路径
  9. envDir: './env',// 环境目录
  10. css: {
  11. preprocessorOptions: {
  12. scss: {// scss配置全局导入
  13. additionalData: '@import "./src/styles/variables";'
  14. },
  15. }
  16. },
  17. resolve: {
  18. // 别名
  19. alias: [
  20. {
  21. find: '/@',
  22. replacement: path.resolve(__dirname, './src'),
  23. }
  24. ],
  25. },
  26. server: {
  27. // 监听所有地址
  28. host: '0.0.0.0',
  29. // 自定义端口号
  30. port: 3010,
  31. // 自动打开浏览器
  32. open: true,
  33. // 开启热更新
  34. hmr: true,
  35. // 若端口被占用,自动尝试下一个可用端口
  36. strictPort: false,
  37. // 代理规则
  38. proxy: {
  39. '/api': {
  40. // 开启跨域
  41. changeOrigin: true,
  42. // 转发地址
  43. target: env.VITE_API_URL,
  44. // 路径重写
  45. rewrite: (path) => path.replace(/^\/api/, ''),
  46. }
  47. }
  48. },
  49. build: {
  50. outDir: 'dist',// 指定打包文件根目录
  51. sourcemap: false,// 构建后不生成源代码
  52. write: true,// 构建的文件写入磁盘
  53. chunkSizeWarningLimit: 10240,// 触发警告的chunk大小10M
  54. },
  55. esbuild: {
  56. drop: command === 'build' ? ['console', 'debugger'] : [],
  57. },
  58. plugins: [
  59. vue(),
  60. viteVConsole({
  61. entry: path.resolve(__dirname, './src/main.ts'), // 入口文件
  62. localEnabled: command === 'serve', // serve开发环境下
  63. enabled: command === 'serve', // 本地运行开启
  64. config: { // vconsole 配置项
  65. maxLogNumber: 1000,
  66. theme: 'light'
  67. }
  68. }),
  69. ],
  70. }
  71. })