vite.config.ts 1.9 KB

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