import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path, { resolve } from "path";
import electron from "vite-plugin-electron";
import electronRenderer from "vite-plugin-electron-renderer";
import pkg from "./package.json";

import viteCompression from "vite-plugin-compression";

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  const isServe = command === "serve";
  const isBuild = command === "build";
  const sourcemap = isServe || !!process.env.VSCODE_DEBUG;

  return {
    base: "./",
    publicDir: path.resolve(__dirname, "public"),
    plugins: [
      react(),
      electron([
        {
          // Main-Process entry file of the Electron App.
          entry: "electron/main",
          onstart(options) {
            if (process.env.VSCODE_DEBUG) {
              console.log(
                /* For `.vscode/.debug.script.mjs` */ "[startup] Electron App"
              );
            } else {
              options.startup();
            }
          },
          vite: {
            build: {
              sourcemap,
              minify: isBuild,
              outDir: "dist-electron/main",
              rollupOptions: {
                external: Object.keys(
                  "dependencies" in pkg ? pkg.dependencies : {}
                ),
              },
            },
          },
        },
        {
          entry: "electron/preload",
          onstart(options) {
            // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
            // instead of restarting the entire Electron App.
            options.reload();
          },
          vite: {
            build: {
              sourcemap: sourcemap ? "inline" : undefined, // #332
              minify: isBuild,
              outDir: "dist-electron/preload",
              rollupOptions: {
                external: Object.keys(
                  "dependencies" in pkg ? pkg.dependencies : {}
                ),
              },
            },
          },
        },
        // {
        //   entry: "electron/sqlite",
        //   onstart(options) {
        //     options.reload();
        //   },
        //   vite: {
        //     build: {
        //       sourcemap: sourcemap ? "inline" : undefined, // #332
        //       minify: isBuild,
        //       outDir: "dist-electron/sqlite",
        //       rollupOptions: {
        //         external: Object.keys(
        //           "dependencies" in pkg ? pkg.dependencies : {}
        //         ),
        //       },
        //     },
        //   },
        // },
        // {
        //   entry: "electron/db/database.db",
        //   onstart(options) {
        //     options.reload();
        //   },
        //   vite: {
        //     build: {
        //       outDir: "dist-electron/db/database",
        //     },
        //   },
        // },
      ]),
      electronRenderer(),
      // 不知道后端是否支持,暂时注释掉
      viteCompression({
        // gzip静态资源压缩配置
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz",
      }),
    ],
    build: {
      // emptyOutDir: false, // 默认情况下,若 outDir 在 root 目录下,则 Vite 会在构建时清空该目录
      outDir: "dist-electron",
      cssCodeSplit: true,
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        input: "index.html",
        output: {
          // 静态资源打包做处理
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          },
        },
      },
      terserOptions: {
        // 清除console和debugger
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    server: {
      host: "localhost",
      port: 3010,
      strictPort: true,
      proxy: {
        "/api": {
          target: "http://localhost:3003",
          changeOrigin: true,
          // rewrite: (path) => path.replace(new RegExp(`/api`), "/api"),
        },
        "/notification": {
          target: "http://114.215.252.134:8989",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/notification/, ""),
        },
        "/userLogin": {
          target: "http://182.44.10.206:8000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/userLogin/, ""),
        },
      },
    },
    resolve: {
      alias: {
        "@": resolve(".", "./src"),
      },
    },
  };
});