vite.config.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import path from 'node:path'
  2. import process from 'node:process'
  3. import { loadEnv } from 'vite'
  4. import type { ConfigEnv, UserConfig } from 'vite'
  5. import viewport from 'postcss-mobile-forever'
  6. import autoprefixer from 'autoprefixer'
  7. import { createVitePlugins } from './build/vite'
  8. import { exclude, include } from './build/vite/optimize'
  9. export default ({ mode }: ConfigEnv): UserConfig => {
  10. const root = process.cwd()
  11. const env = loadEnv(mode, root)
  12. return {
  13. base: env.VITE_APP_PUBLIC_PATH,
  14. plugins: createVitePlugins(),
  15. server: {
  16. host: true,
  17. port: 3000,
  18. proxy: {
  19. '/api': {
  20. target: env.VITE_APP_API_HTTP_URL,
  21. rewrite: path => path.replace(new RegExp(`^${env.VITE_APP_API_BASE_URL}`), ''),
  22. bypass(req, res, options: any) {
  23. const realUrl = options.target + (options.rewrite ? options.rewrite(req.url) : '')
  24. res.setHeader('A-Real-Url', realUrl) // 添加响应标头(A-Real-Url为自定义命名),在浏览器中显示
  25. },
  26. ws: false,
  27. changeOrigin: true,
  28. },
  29. },
  30. },
  31. resolve: {
  32. alias: {
  33. '~@': path.join(__dirname, './src'),
  34. '@': path.join(__dirname, './src'),
  35. '~': path.join(__dirname, './src/assets'),
  36. },
  37. },
  38. css: {
  39. postcss: {
  40. plugins: [
  41. autoprefixer(),
  42. // https://github.com/wswmsword/postcss-mobile-forever
  43. viewport({
  44. appSelector: '#app',
  45. viewportWidth: 375,
  46. maxDisplayWidth: 600,
  47. rootContainingBlockSelectorList: [
  48. 'van-tabbar',
  49. 'van-popup',
  50. ],
  51. }),
  52. ],
  53. },
  54. },
  55. build: {
  56. cssCodeSplit: false,
  57. chunkSizeWarningLimit: 2048,
  58. },
  59. optimizeDeps: { include, exclude },
  60. }
  61. }