import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { VantResolver } from "@vant/auto-import-resolver";
import { fileURLToPath, URL } from "node:url";
const timestamp = Math.round(new Date().getTime() / 1000).toString();
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  return {
    base: env.VITE_PUBLIC_PATH, // 开发或生产环境服务的公共基础路径
    build: {
      minify: 'terser',
      outDir: env.VITE_MODE == 'production' ? 'dist' : 'dist-test', // 打包文件的输出目录
      assetsDir: 'static', // 静态资源的存放目录
      assetsInlineLimit: 4096, // 图片转 base64 编码的阈值
      terserOptions: {
        compress: {
          drop_console: JSON.parse(env.VITE_OPEN_CONSOLE),
          drop_debugger: JSON.parse(env.VITE_OPEN_CONSOLE),
        },
      },
      rollupOptions: {
        output: {
          entryFileNames: `assets/[name]_${timestamp}.js`,
          chunkFileNames: `assets/[name]_${timestamp}.js`,
          assetFileNames: `assets/[name]_${timestamp}.[ext]`
        }
      }
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        imports: ["vue", "vue-router"],
      }),
      Components({
        resolvers: [ElementPlusResolver(), VantResolver()],
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    server: {
      host: "0.0.0.0",
      port: 8081,
      proxy: {
        "/api": {
          target: "http://192.168.0.80:5052",
          changeOrigin: true,
          ws: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/api/, "/"),
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
          additionalData:
            '@import "./src/assets/styles/variables.scss";'
        },
      },
      postcss: {
        plugins: [],
      },
    },
  }
});
