import { defineConfig, loadEnv } from "vite";
import path from "path";
import plugins from "./.build/plugins";


// https://vite.dev/config/
export default defineConfig((cnf) => {

  const { mode } = cnf;
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV } = env;
  return {
    base: VITE_APP_ENV === "production" ? "/" : "/",
    plugins: plugins(cnf),
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@components": path.resolve(__dirname, "./src/vue-element-plus-y/components"),
      },
    },
    css: {
      // css全局变量使用，@/styles/variable.scss文件
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/styles/var.scss" as *;',
        },
      },
    },

    server: {
      port: 17001,
      open: true,
      proxy: {
        [env.VITE_WEB_BASE_API]: {
          target: env.VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(`${[env.VITE_WEB_BASE_API]}`, ""),

          //查看真实代理url
          bypass(req, res, options) {
            //@ts-ignore
            const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || '';
            req.headers['x-req-proxyUrl'] = proxyUrl
            res.setHeader('x-res-proxyUrl',proxyUrl)
          },
        }
      },
    },

  };
});
