/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: htang
 * @Date: 2024-07-15 16:33:33
 * @LastEditors: htang
 * @LastEditTime: 2024-08-24 14:38:23
 */
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { OUTPUT_DIR } from './build/constant';
import path from "path";

const TimeStamp = new Date().getTime();

function resolve(url) {
  return path.resolve(__dirname, url);
}
// https://vitejs.dev/config/
export default ({ mode }) => {
  // 获取环境变量
  const env = loadEnv(mode, process.cwd());
  const accountApiTarget = env.VITE_ACCOUNT_API_PROXY_TARGET || 'http://127.0.0.1:8888';
  const openApiTarget = env.VITE_OPEN_API_PROXY_TARGET || 'http://127.0.0.1:9999';
  const jsyApiTarget = env.VITE_JSY_API_PROXY_TARGET || openApiTarget;
  const geoserverTarget = env.VITE_GEOSERVER_PROXY_TARGET || 'http://127.0.0.1:8080';
  const ezvizTarget = env.VITE_EZVIZ_PROXY_TARGET || 'https://open.ys7.com';
  const hyResultTileTarget = env.VITE_HY_RESULT_TILE_PROXY_TARGET || 'https://screen3.qklny.cn';
  const hyResultTileCookie = env.VITE_HY_RESULT_TILE_COOKIE || '';
  return defineConfig({
    base: './', //env.VITE_APP_BASE_URL, // 动态改变base值
    assetsInclude: [
      "**/*.glb",
      "**/*.gltf",
      "**/*.fbx",
      "**/*.hdr",
      "**/*.json",
      "**/*.mp4",
      "**/*.mov",
    ],
    resolve: {
      alias: {
        "@": resolve("./src"),
        "~@": resolve("./src"),
      },
      // 省略文件后缀
      extensions: [".mjs", ".js", ".jsx", ".json"],
    },
    build: {
      minify: 'esbuild',
      target: 'es2015',
      cssTarget: 'chrome80',
      outDir: OUTPUT_DIR,
      terserOptions: {
        compress: {
          // keep_infinity: true,
          // // Used to delete console in production environment
          // drop_console: VITE_DROP_CONSOLE,
          // drop_debugger: true,
        },
      },
      // Turning off brotliSize display can slightly reduce packaging time
      reportCompressedSize: false,
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        // 参考：https://blog.cinob.cn/archives/393
        output: {
          // 入口文件名
          entryFileNames: `assets/[name]-${TimeStamp}.js`,
          // 块文件名
          chunkFileNames: `assets/[name]-[hash]-${TimeStamp}.js`,
          // 资源文件名 css 图片等等
          assetFileNames: `assets/[name]-[hash]-balabala-${TimeStamp}.[ext]`,
        }
      }
    },
    server: {
      proxy: {
        '/login': {
          target: accountApiTarget,
          changeOrigin: true,
        },
        '/logout': {
          target: accountApiTarget,
          changeOrigin: true,
        },
        '/getInfo': {
          target: accountApiTarget,
          changeOrigin: true,
        },
        '/getRouters': {
          target: accountApiTarget,
          changeOrigin: true,
        },
        '/captchaImage': {
          target: accountApiTarget,
          changeOrigin: true,
        },
        '/user': {
          target: accountApiTarget,
          changeOrigin: true,
          rewrite: (proxyPath) => proxyPath
            .replace(/^\/user\/login$/, '/login')
            .replace(/^\/user\/logout$/, '/logout')
            .replace(/^\/user\/getInfo$/, '/getInfo'),
        },
        '/openApi': {
          target: openApiTarget,
          changeOrigin: true,
        },
        '/jsyApi': {
          target: jsyApiTarget,
          changeOrigin: true,
        },
        '/geoserver': {
          target: geoserverTarget,
          changeOrigin: true,
        },
        '/hy-result': {
          target: hyResultTileTarget,
          changeOrigin: true,
          configure: (proxy) => {
            proxy.on('proxyReq', (proxyReq, req) => {
              const cookie = hyResultTileCookie || req.headers.cookie;
              proxyReq.setHeader('Referer', 'https://screen3.qklny.cn/');
              proxyReq.setHeader('Origin', 'https://screen3.qklny.cn');
              if (cookie) proxyReq.setHeader('Cookie', cookie);
            });
          },
        },
        '/ezviz': {
          target: ezvizTarget,
          changeOrigin: true,
          rewrite: (proxyPath) => proxyPath.replace(/^\/ezviz/, ''),
        },
      },
    },
    envDir: '.',
    plugins: [vue()],
  });
};
