vite.config.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { fileURLToPath, URL } from "node:url";
  2. import { defineConfig, loadEnv } from "vite";
  3. import vue from "@vitejs/plugin-vue";
  4. import vueJsx from "@vitejs/plugin-vue-jsx";
  5. import Components from "unplugin-vue-components/vite";
  6. import {ElementPlusResolver, VantResolver} from "unplugin-vue-components/resolvers";
  7. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  8. import AutoImport from 'unplugin-auto-import/vite';
  9. import path from "path";
  10. import mockDevServerPlugin from "vite-plugin-mock-dev-server";
  11. import vueSetupExtend from "vite-plugin-vue-setup-extend";
  12. import viteCompression from "vite-plugin-compression";
  13. import { createHtmlPlugin } from "vite-plugin-html";
  14. import { enableCDN } from "./build/cdn";
  15. import { ViteImageOptimizer } from 'vite-plugin-image-optimizer';
  16. import legacy from '@vitejs/plugin-legacy';
  17. // 当前工作目录路径
  18. const root: string = process.cwd();
  19. // https://vitejs.dev/config/
  20. export default defineConfig(({ mode }) => {
  21. // 环境变量
  22. const env = loadEnv(mode, root, "");
  23. return {
  24. base: env.VITE_PUBLIC_PATH || "/",
  25. plugins: [
  26. vue(),
  27. vueJsx(),
  28. mockDevServerPlugin(),
  29. // vant 组件自动按需引入
  30. Components({
  31. dts: "src/typings/components.d.ts",
  32. resolvers: [VantResolver()]
  33. }),
  34. // svg icon
  35. createSvgIconsPlugin({
  36. // 指定图标文件夹
  37. iconDirs: [path.resolve(root, "src/icons/svg")],
  38. // 指定 symbolId 格式
  39. symbolId: "icon-[dir]-[name]"
  40. }),
  41. // 允许 setup 语法糖上添加组件名属性
  42. vueSetupExtend(),
  43. // 生产环境 gzip 压缩资源
  44. viteCompression(),
  45. // 注入模板数据
  46. createHtmlPlugin({
  47. inject: {
  48. data: {
  49. ENABLE_ERUDA: env.VITE_ENABLE_ERUDA || "false"
  50. }
  51. }
  52. }),
  53. // 自动引入组件
  54. AutoImport({
  55. // 自动导入 Vue 相关函数
  56. imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
  57. dts: './auto-imports.d.ts',
  58. eslintrc: {
  59. enabled: false,
  60. filepath: './.eslintrc-auto-import.json',
  61. globalsPropValue: true
  62. },
  63. resolvers: [
  64. ElementPlusResolver()
  65. ],
  66. vueTemplate: true, // 是否在 vue 模板中自动导入
  67. }),
  68. legacy({
  69. targets: ['defaults', 'Chrome >= 51'],
  70. additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
  71. modernPolyfills: ['es.string.replace-all'],
  72. renderLegacyChunks: true,
  73. polyfills: [
  74. 'es.array.flat-map',
  75. 'es.array.filter',
  76. 'es.promise',
  77. 'es.promise.finally',
  78. 'es.object.assign',
  79. 'es.array.for-each',
  80. 'es.object.define-properties',
  81. 'es.object.define-property',
  82. 'es.object.get-own-property-descriptor',
  83. 'es.object.get-own-property-descriptors',
  84. 'es.object.keys',
  85. 'es.object.to-string',
  86. 'web.dom-collections.for-each',
  87. 'esnext.global-this',
  88. 'esnext.string.match-all',
  89. 'es.array.iterator',
  90. 'es.string.includes',
  91. 'es.string.starts-with',
  92. 'es.object.values'
  93. ]
  94. }),
  95. // 生产环境默认不启用 CDN 加速
  96. enableCDN(env.VITE_CDN_DEPS),
  97. ViteImageOptimizer()
  98. ],
  99. resolve: {
  100. alias: {
  101. "@": fileURLToPath(new URL("./src", import.meta.url))
  102. }
  103. },
  104. server: {
  105. host: true,
  106. port: Number(env.VITE_APP_PORT),
  107. open: true
  108. // 仅在 proxy 中配置的代理前缀, mock-dev-server 才会拦截并 mock
  109. // doc: https://github.com/pengzhanbo/vite-plugin-mock-dev-server
  110. // proxy: {
  111. // "^/dev-api": {
  112. // target: ""
  113. // }
  114. // }
  115. },
  116. css: {
  117. preprocessorOptions: {
  118. scss: { api: 'modern-compiler' },
  119. }
  120. },
  121. build: {
  122. rollupOptions: {
  123. output: {
  124. chunkFileNames: "static/js/[name]-[hash].js",
  125. entryFileNames: "static/js/[name]-[hash].js",
  126. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  127. }
  128. }
  129. }
  130. };
  131. });