vite.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // 导入 Vite 的配置函数
  2. import { defineConfig, loadEnv } from 'vite'
  3. // 导入 Vue 插件
  4. import vue from '@vitejs/plugin-vue'
  5. // 导入自动导入插件,用于自动导入 API
  6. import AutoImport from 'unplugin-auto-import/vite'
  7. // 导入组件自动注册插件
  8. import Components from 'unplugin-vue-components/vite'
  9. // 导入 Vant 组件库的解析器
  10. import { VantResolver } from '@vant/auto-import-resolver'
  11. import path from 'node:path'
  12. import postcssPxToViewport from 'postcss-px-to-viewport-8-plugin'
  13. // 导出 Vite 配置
  14. export default defineConfig(({ mode }) => {
  15. const env = loadEnv(mode, process.cwd())
  16. return {
  17. // 配置环境变量前缀
  18. envPrefix: 'APP_',
  19. // 配置插件
  20. plugins: [
  21. // 启用 Vue 插件
  22. vue(),
  23. // 配置自动导入插件
  24. AutoImport({
  25. // 使用 Vant 解析器
  26. resolvers: [VantResolver()]
  27. }),
  28. // 配置组件自动注册插件
  29. Components({
  30. // 使用 Vant 解析器
  31. resolvers: [VantResolver()]
  32. })
  33. ],
  34. css: {
  35. postcss: {
  36. plugins: [
  37. postcssPxToViewport({
  38. // 要转化的单位
  39. unitToConvert: 'px',
  40. // UI设计稿的大小
  41. viewportWidth: 375,
  42. // 转换后的精度
  43. unitPrecision: 6,
  44. // 转换后的单位
  45. viewportUnit: 'vw',
  46. // 字体转换后的单位
  47. fontViewportUnit: 'vw',
  48. // 能转换的属性,*表示所有属性,!border表示border不转
  49. propList: ['*'],
  50. // 指定不转换为视窗单位的类名,
  51. selectorBlackList: ['van-'],
  52. // 最小转换的值,小于等于1不转
  53. minPixelValue: 1,
  54. // 是否在媒体查询的css代码中也进行转换,默认false
  55. mediaQuery: false,
  56. // 是否转换后直接更换属性值
  57. replace: true,
  58. // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
  59. exclude: [/node_modules/],
  60. // 包含那些文件或者特定文件
  61. include: [],
  62. // 是否处理横屏情况
  63. landscape: false
  64. })
  65. ]
  66. },
  67. preprocessorOptions: {
  68. scss: {
  69. silenceDeprecations: ['legacy-js-api'],
  70. additionalData: `@use "@/assets/style/variables.scss";`
  71. }
  72. }
  73. },
  74. resolve: {
  75. alias: {
  76. '@': path.resolve(__dirname, './src')
  77. }
  78. },
  79. server: {
  80. host: '0.0.0.0',
  81. proxy: {
  82. [env.APP_BASE_API]: {
  83. target: env.APP_BASE_SERVER_URL,
  84. changeOrigin: true
  85. }
  86. }
  87. }
  88. }
  89. })