vite.config.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { VantResolver } from "unplugin-vue-components/resolvers";
  7. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  8. import path from "path";
  9. import mockDevServerPlugin from "vite-plugin-mock-dev-server";
  10. import vueSetupExtend from "vite-plugin-vue-setup-extend";
  11. import viteCompression from "vite-plugin-compression";
  12. import { createHtmlPlugin } from "vite-plugin-html";
  13. import { enableCDN } from "./build/cdn";
  14. // 当前工作目录路径
  15. const root: string = process.cwd();
  16. // https://vitejs.dev/config/
  17. export default defineConfig(({ mode }) => {
  18. // 环境变量
  19. const env = loadEnv(mode, root, "");
  20. return {
  21. base: env.VITE_PUBLIC_PATH || "/",
  22. plugins: [
  23. vue(),
  24. vueJsx(),
  25. mockDevServerPlugin(),
  26. // vant 组件自动按需引入
  27. Components({
  28. dts: "src/typings/components.d.ts",
  29. resolvers: [VantResolver()]
  30. }),
  31. // svg icon
  32. createSvgIconsPlugin({
  33. // 指定图标文件夹
  34. iconDirs: [path.resolve(root, "src/icons/svg")],
  35. // 指定 symbolId 格式
  36. symbolId: "icon-[dir]-[name]"
  37. }),
  38. // 允许 setup 语法糖上添加组件名属性
  39. vueSetupExtend(),
  40. // 生产环境 gzip 压缩资源
  41. viteCompression(),
  42. // 注入模板数据
  43. createHtmlPlugin({
  44. inject: {
  45. data: {
  46. ENABLE_ERUDA: env.VITE_ENABLE_ERUDA || "false"
  47. }
  48. }
  49. }),
  50. // 生产环境默认不启用 CDN 加速
  51. enableCDN(env.VITE_CDN_DEPS)
  52. ],
  53. resolve: {
  54. alias: {
  55. "@": fileURLToPath(new URL("./src", import.meta.url))
  56. }
  57. },
  58. server: {
  59. host: true,
  60. open: true
  61. // 仅在 proxy 中配置的代理前缀, mock-dev-server 才会拦截并 mock
  62. // doc: https://github.com/pengzhanbo/vite-plugin-mock-dev-server
  63. // proxy: {
  64. // "^/dev-api": {
  65. // target: ""
  66. // }
  67. // }
  68. },
  69. build: {
  70. rollupOptions: {
  71. output: {
  72. chunkFileNames: "static/js/[name]-[hash].js",
  73. entryFileNames: "static/js/[name]-[hash].js",
  74. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  75. }
  76. }
  77. }
  78. };
  79. });