vite.config.mts 1.8 KB
import { defineConfig } from '@vben/vite-config';
import { loadEnv } from "vite";

import { thDevApiBufferPlugin } from './vite-plugins/th-dev-api-buffer';
// 自行取消注释来启用按需导入功能
// import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
// import Components from 'unplugin-vue-components/vite';

export default defineConfig(async ({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  const devApiPrefix = (env.VITE_GLOB_API_URL || "/dev-api").replace(/^"|"$/g, "");
  return {
    application: {},
    vite: {
      plugins: [
        /**
         * 替代 /dev-api 的 http-proxy(实测同 body 经 proxy 需 ~63s,直连上游 ~3.5s)
         * 重启 dev 后 login 响应应带 Content-Length 与 X-Dev-Api-Buffer: 1
         */
        thDevApiBufferPlugin(env.VITE_APP_URL, devApiPrefix),
        // Components({
        //   dirs: [], // 默认会导入src/components目录下所有组件 不需要
        //   dts: './types/components.d.ts', // 输出类型文件
        //   resolvers: [
        //     AntDesignVueResolver({
        //       // 需要排除Button组件 全局已经默认导入了
        //       exclude: ['Button'],
        //       importStyle: false, // css in js
        //     }),
        //   ],
        // }),
      ],
      server: {
        proxy: {
          // /dev-api 已由 thDevApiBufferPlugin 处理,勿再配置 http-proxy
          ...(env.VITE_APP_BASE_WS
            ? {
                [env.VITE_APP_BASE_WS]: {
                  target: env.VITE_APP_BASE_URL_WS,
                  changeOrigin: true,
                  rewrite: (p: string) =>
                    p.replace(`${env.VITE_APP_BASE_WS}`, ""),
                  ws: true,
                },
              }
            : {}),
        },
      },
    },
  };
});