vite.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import VueDevTools from 'vite-plugin-vue-devtools'
  5. import AutoImport from 'unplugin-auto-import/vite';
  6. import Components from 'unplugin-vue-components/vite';
  7. import { VantResolver } from '@vant/auto-import-resolver';
  8. import postCssPxToRem from "postcss-pxtorem";
  9. import UnoCSS from 'unocss/vite'
  10. import vueSetupExtend from "vite-plugin-vue-setup-extend";
  11. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  12. import path from "path";
  13. import { viteMockServe } from 'vite-plugin-mock'
  14. import viteCompression from "vite-plugin-compression";
  15. // 当前工作目录路径
  16. const root = process.cwd();
  17. // https://vitejs.dev/config/
  18. export default defineConfig(({ mode }) => {
  19. // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
  20. // const env = loadEnv(mode, process.cwd(), '')
  21. const env = loadEnv(mode, root)
  22. const { VITE_BASE_URL, VITE_BASE_API } = env
  23. return {
  24. base: VITE_BASE_URL,
  25. build: {
  26. minify: 'terser',
  27. terserOptions: {
  28. compress: {
  29. drop_console: true, // 去除console
  30. drop_debugger: true // 去除debugger
  31. }
  32. }
  33. },
  34. server: {
  35. port: 3000,
  36. open: 'http://localhost:3000' + VITE_BASE_URL,
  37. host: true,
  38. proxy: {
  39. [VITE_BASE_API]: {
  40. target: "",
  41. changeOrigin: true, // 是否允许不同源
  42. secure: false, // 支持https
  43. rewrite: (path) => path.replace(new RegExp('^' + VITE_BASE_API), '')
  44. }
  45. }
  46. },
  47. plugins: [
  48. vue(),
  49. // 原子化css
  50. UnoCSS({
  51. configFile: './uno.config.js',
  52. }),
  53. // VueDevTools(),// 官方调试工具
  54. viteMockServe({
  55. mockPath: './mock/', // 设置模拟数据的存储文件夹
  56. supportTs: false, // 是否读取ts文件模块
  57. logger: true, //  是否在控制台显示请求日志
  58. localEnabled: true, //设置是否启用本地mock文件
  59. prodEnabled: false //设置打包是否启用 mock 功能
  60. }),
  61. // svg 图标
  62. createSvgIconsPlugin({
  63. // 指定图标文件夹
  64. iconDirs: [path.resolve(root, "src/assets/icons/svg")],
  65. // 指定 symbolId 格式
  66. symbolId: "icon-[dir]-[name]"
  67. }),
  68. // 允许 setup 语法糖上添加组件名属性
  69. vueSetupExtend(),
  70. // 生产环境 gzip 压缩资源
  71. viteCompression(),
  72. // 自动导入
  73. AutoImport({
  74. resolvers: [VantResolver()],
  75. // global imports to register
  76. imports: [
  77. // presets
  78. 'vue',
  79. 'vue-i18n',
  80. 'vue-router',
  81. // custom
  82. {
  83. '@vueuse/core': [
  84. // named imports
  85. 'useMouse', // import { useMouse } from '@vueuse/core',
  86. // alias
  87. ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
  88. ],
  89. 'axios': [
  90. // default imports
  91. ['default', 'axios'], // import { default as axios } from 'axios',
  92. ],
  93. '[package-name]': [
  94. '[import-names]',
  95. // alias
  96. ['[from]', '[alias]'],
  97. ],
  98. }
  99. ],
  100. dirs: [
  101. 'src/composables',
  102. 'src/store/modules',
  103. ],
  104. vueTemplate: true,
  105. }),
  106. Components({
  107. resolvers: [VantResolver()],
  108. }),
  109. ],
  110. css: {
  111. postcss: {
  112. preprocessorOptions: {
  113. // 这里可以配置sass的选项,例如全局变量、函数等
  114. scss: {
  115. javascriptEnabled: true,
  116. additionalData: '@import "./assets/styles/variable.scss";',
  117. },
  118. },
  119. plugins: [
  120. postCssPxToRem({
  121. rootValue: 37.5, // 1rem,根据 设计稿宽度/10 进行设置
  122. propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
  123. selectorBlackList: ["norem"], // 过滤掉norem-开头的class,不进行rem转换
  124. })
  125. ]
  126. }
  127. },
  128. resolve: {
  129. alias: {
  130. '@': fileURLToPath(new URL('./src', import.meta.url))
  131. }
  132. }
  133. }
  134. }
  135. )