import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import path from "path";

// 云服务器公网 IP：设置环境变量 PUBLIC_HOST=139.196.48.8 可在控制台显示公网访问地址
const publicHost = process.env.PUBLIC_HOST;
const port = 1420;

export default defineConfig({
  server: {
    port,
    host: true, // 监听 0.0.0.0，支持公网访问
    allowedHosts: publicHost ? [publicHost] : undefined,
  },
  plugins: [
    react(),
    tailwindcss(),
    // 云服务器：启动时打印公网访问地址
    publicHost
      ? {
          name: "log-public-url",
          configureServer() {
            return () => {
              console.log(`\n  ➜  公网:   http://${publicHost}:${port}/\n`);
            };
          },
        }
      : undefined,
  ].filter(Boolean),
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});
