vite.config.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import vue from '@vitejs/plugin-vue'
  2. import { resolve } from 'path'
  3. import { defineConfig, loadEnv, ConfigEnv } from 'vite'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { ElementPlusResolver, VantResolver } from 'unplugin-vue-components/resolvers'
  7. import pxtovw from 'postcss-px-to-viewport'
  8. import vueJsx from '@vitejs/plugin-vue-jsx'
  9. const viteConfig = defineConfig((mode: ConfigEnv) => {
  10. const env = loadEnv(mode.mode, process.cwd())
  11. console.log(mode.command, env)
  12. return {
  13. plugins: [
  14. vue(),
  15. vueJsx(),
  16. AutoImport({
  17. imports: ['vue', 'vue-router'],
  18. resolvers: [ElementPlusResolver(), VantResolver()],
  19. }),
  20. Components({
  21. resolvers: [ElementPlusResolver(), VantResolver()],
  22. })
  23. ],
  24. resolve: {
  25. alias: {
  26. '@': resolve(__dirname, './src'),
  27. },
  28. },
  29. base: '/', // 打包路径
  30. server: {
  31. open: false, // 服务启动时是否自动打开浏览器
  32. hmr: true, // 开启热更新
  33. proxy: {
  34. '/api': {
  35. target: env.VITE_BASE_URL,
  36. changeOrigin: true,
  37. ws: true,
  38. secure: false,
  39. rewrite: (path) => path.replace(/^\/api/, ''),
  40. },
  41. },
  42. },
  43. css: {
  44. postcss: {
  45. plugins: [
  46. pxtovw({
  47. viewportWidth: 375,
  48. viewportUnit: 'vw',
  49. }),
  50. ],
  51. },
  52. preprocessorOptions: {
  53. // sass config
  54. scss: {
  55. additionalData: `@import "./src/styles/variables.scss";`,
  56. },
  57. },
  58. },
  59. define: {
  60. 'process.env': env,
  61. },
  62. }
  63. })
  64. export default viteConfig