vite.config.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { fileURLToPath, URL } from 'node:url'
  4. import Components from 'unplugin-vue-components/vite'
  5. import { VantResolver } from '@vant/auto-import-resolver'
  6. export default defineConfig(({ mode }) => {
  7. // 加载环境变量
  8. const env = loadEnv(mode, process.cwd())
  9. return {
  10. plugins: [
  11. vue(),
  12. // Vant 组件自动导入
  13. Components({
  14. resolvers: [VantResolver({ importStyle: false })],
  15. dts: 'src/components.d.ts'
  16. })
  17. ],
  18. resolve: {
  19. alias: {
  20. '@': fileURLToPath(new URL('./src', import.meta.url))
  21. },
  22. extensions: ['.js', '.json', '.vue', '.mjs', '.ts', '.tsx']
  23. },
  24. server: {
  25. port: 3000,
  26. host: '0.0.0.0', // 允许局域网访问,方便手机调试
  27. open: false, // 不自动打开浏览器
  28. cors: true, // 启用 CORS
  29. hmr: {
  30. overlay: true // 显示错误覆盖层
  31. }
  32. },
  33. build: {
  34. outDir: 'dist',
  35. assetsDir: 'assets',
  36. sourcemap: false,
  37. // 压缩选项 - 使用 esbuild(更快)
  38. minify: 'esbuild',
  39. // esbuild 压缩配置
  40. esbuild: mode === 'production' ? {
  41. drop: ['console', 'debugger'], // 生产环境移除 console 和 debugger
  42. pure: ['console.log'] // 标记为纯函数,方便 tree-shaking
  43. } : {},
  44. // 设置打包后的文件大小警告限制(kb)
  45. chunkSizeWarningLimit: 1000,
  46. // 资源内联限制
  47. assetsInlineLimit: 4096,
  48. rollupOptions: {
  49. output: {
  50. // 分包策略
  51. manualChunks: {
  52. 'vue-vendor': ['vue', 'vue-router', 'pinia'],
  53. 'vant-vendor': ['vant'],
  54. 'liff-vendor': ['@line/liff'],
  55. 'utils': ['axios', 'dayjs']
  56. },
  57. // 静态资源文件命名
  58. chunkFileNames: 'js/[name]-[hash].js',
  59. entryFileNames: 'js/[name]-[hash].js',
  60. assetFileNames: '[ext]/[name]-[hash].[ext]'
  61. }
  62. },
  63. // CSS 代码分割
  64. cssCodeSplit: true
  65. },
  66. // 依赖优化
  67. optimizeDeps: {
  68. include: [
  69. 'vue',
  70. 'vue-router',
  71. 'pinia',
  72. 'axios',
  73. '@line/liff',
  74. 'vant'
  75. ]
  76. },
  77. // CSS 配置
  78. css: {
  79. preprocessorOptions: {
  80. scss: {
  81. additionalData: '', // 可以在这里添加全局 scss 变量
  82. api: 'modern-compiler' // 使用现代编译器 API
  83. }
  84. }
  85. }
  86. }
  87. })