vite.config.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { defineConfig, loadEnv, ConfigEnv } from "vite";
  2. import vue from "@vitejs/plugin-vue";
  3. import path from "path";
  4. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  5. import AutoImport from "unplugin-auto-import/vite";
  6. import Components from "unplugin-vue-components/vite";
  7. import IconsResolver from "unplugin-icons/resolver";
  8. import Icons from "unplugin-icons/vite";
  9. import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
  10. import { VueUseComponentsResolver } from "unplugin-vue-components/resolvers";
  11. import postcsspxtoviewport from "postcss-px-to-viewport";
  12. import legacyPlugin from "@vitejs/plugin-legacy";
  13. // https://vitejs.dev/config/
  14. export default ({ mode }: ConfigEnv) => {
  15. const env = loadEnv(mode, process.cwd());
  16. return defineConfig({
  17. plugins: [
  18. vue(),
  19. createSvgIconsPlugin({
  20. iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
  21. symbolId: "icon-[dir]-[name]",
  22. }),
  23. AutoImport({
  24. imports: ["vue", "vue-router", "pinia", "@vueuse/core", { axios: [["default", "axios"]] }],
  25. resolvers: [ElementPlusResolver(), IconsResolver({})],
  26. vueTemplate: true,
  27. dts: path.resolve(__dirname, "src/types", "auto-imports.d.ts"),
  28. }),
  29. Components({
  30. resolvers: [ElementPlusResolver(), IconsResolver({ prefix: "icon", enabledCollections: ["ep"] }), VueUseComponentsResolver()],
  31. dts: path.resolve(__dirname, "src/types", "components.d.ts"),
  32. }),
  33. // 图标库 'https://fontawesome.com.cn/v5' 'https://icones.netlify.app/' 'https://icon-sets.iconify.design/'
  34. Icons({ autoInstall: true }),
  35. legacyPlugin({
  36. targets: ["chrome 52"], // 需要兼容的目标列表,可以设置多个
  37. additionalLegacyPolyfills: ["regenerator-runtime/runtime"], // 面向IE11时需要此插件
  38. }),
  39. ],
  40. resolve: {
  41. alias: {
  42. "@": path.resolve(__dirname, "./src"),
  43. "#": path.resolve(__dirname, "./"),
  44. },
  45. },
  46. css: {
  47. preprocessorOptions: {
  48. scss: {
  49. additionalData: `@import "@/assets/styles/variables.scss";`,
  50. javascriptEnabled: true,
  51. },
  52. },
  53. postcss: {
  54. plugins: [
  55. postcsspxtoviewport({
  56. unitToConvert: "px", // 要转化的单位
  57. viewportWidth: 1920, // 要转化的单位
  58. unitPrecision: 6, // 转换后的精度,即小数点位数
  59. propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
  60. viewportUnit: "vw", // 指定需要转换成的视窗单位,默认vw
  61. fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位,默认vw
  62. selectorBlackList: [], // 指定不转换为视窗单位的类名,
  63. minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
  64. mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
  65. replace: true, // 是否转换后直接更换属性值
  66. landscape: false, // 是否处理横屏情况
  67. }),
  68. ],
  69. },
  70. },
  71. server: {
  72. host: "0.0.0.0",
  73. port: 80,
  74. proxy: {
  75. "/api": {
  76. target: "http://localhost:8086",
  77. ws: true,
  78. changeOrigin: true,
  79. rewrite: (path) => path.replace(/^\/api/, ""),
  80. },
  81. },
  82. },
  83. });
  84. };