import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
import AutoImport from "unplugin-auto-import/vite";
import legacy from "@vitejs/plugin-legacy";
import vueJsx from "@vitejs/plugin-vue-jsx";
import obfuscator from "rollup-plugin-obfuscator";

const SERVER_URL = "https://192.168.10.39";
const path = require("path");

const disableObfuse = process.env.DISABLE_OBFUSE;
// console.log(process.env);
// console.log(disableObfuse);
console.log("process.env.DISABLE_OBFUSE: ", disableObfuse);

// https://vitejs.dev/config/
export default defineConfig({
  base: "/oe-web/",
  plugins: [
    vue({ reactivityTransform: true }),
    vueJsx({}),
    Components({
      resolvers: [NaiveUiResolver()],
      dts: "src/types/components.d.ts",
    }),
    AutoImport({
      // targets to transform
      include: [
        /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        /\.vue$/,
        /\.vue\?vue/, // .vue
      ],
      resolvers: [(name) => (name === "logger" ? "@/utils/logger" : undefined)],
      dts: "src/types/auto-imports.d.ts",

      // Generate corresponding .eslintrc-auto-import.json file.
      // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
      eslintrc: {
        enabled: true, // Default `false`
        filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
        globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
      },
    }),
    legacy({
      targets: ["chrome >= 58"],
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
    }),
    process.env.NODE_ENV === "development"
      ? {}
      : obfuscator({
          // 不同的构建可能成功运行也可能不成功？
          // 看看是不是单个文件做transform，然后对最终文件做renderChunk导致的构建结果不一致
          fileOptions: false,
          // fileOptions: {
          //   optionsPreset: "low-obfuscation",
          //   debugProtection: false,
          //   transformObjectKeys: false,
          //   disableConsoleOutput: false,
          //   // controlFlowFlattening: true,
          //   controlFlowFlatteningThreshold: 0.9,
          //   // per-file:
          //   deadCodeInjection: true,
          //   deadCodeInjectionThreshold: 1,
          //   // stringArrayThreshold: 1,
          //   // numbersToExpressions: true,
          //   // controlFlowFlatteningThreshold: 1,
          //   // stringArrayCallsTransform: true,
          //   // stringArrayCallsTransformThreshold: 1,
          //   // stringArrayEncoding: ["rc4"],
          // },
          globalOptions: {
            optionsPreset: "low-obfuscation",
            // debugProtection: true,
            // debugProtectionInterval: 2000,
            domainLock: [
              ".exam-cloud.cn",
              ".test41v3.qmth.com.cn",
              ".ea100.com.cn",
              ".ecs.qmth.com.cn",
            ],
            // selfDefending: true,
            // sourceMap: false,
            // splitStrings: true,
            disableConsoleOutput: false,
            // // stringArrayCallsTransform: true,
            // deadCodeInjection: true,
            // numbersToExpressions: true,
            controlFlowFlattening: true,
            // stringArrayEncoding: ["none", "base64", "rc4"],
            // stringArrayRotate: true,
            // transformObjectKeys: true,
          },
          include: [
            // "src/**/**.ts",
            // "src/**/**.js",
            "src/utils/**.ts",
            // "**/UserLogin/**.ts",
            // "**/Examing/**/**.ts",
          ],
          exclude: ["node_modules/**"],
          ...(disableObfuse
            ? {
                fileOptions: false,
                globalOptions: false,
              }
            : {}),
        }),
  ],
  server: {
    port: 3000,
    // strictPort: true,
    fs: {
      strict: true,
      allow: ["./"],
    },
    proxy: {
      "/api": {
        target: SERVER_URL,
        changeOrigin: true,
        secure: false,
        ws: true,
      },
      "/admin": {
        target: SERVER_URL,
        changeOrigin: true,
        secure: false,
      },
    },
  },
  resolve: {
    alias: [{ find: "@", replacement: path.resolve(__dirname, "./src") }],
    extensions: [
      ".js",
      ".mjs",
      ".ts",
      ".tsx",
      ".vue",
      ".json",
      ".scss",
      ".css",
    ],
  },
  build: {
    ssr: false,
    // FIXME: 为初期测试开启的调试
    sourcemap: !!disableObfuse,
    target: ["chrome58"],
  },
  // define: {
  //   __VUE_PROD_DEVTOOLS__: true, // no effect
  // },
});
