import { fileURLToPath, URL } from "node:url";
import createVitePlugins from "./vite/plugins";
import serveConfig from "./vite/vite.config.serve";
import buildConfig from "./vite/vite.config.serve";
import { defineConfig, loadEnv } from "vite";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV } = env;
  const config = command === "build" ? buildConfig : serveConfig;
  return {
    plugins: createVitePlugins(VITE_APP_ENV, command === "build"),
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    ...config,
    build: {
      rollupOptions: {
        output: {
          entryFileNames: 'js/[name]-[hash].js', // 指定 JS 文件的输出路径及命名规则
          chunkFileNames: 'js/[name]-[hash].js', // 指定分片文件的输出路径及命名规则
          assetFileNames: (assetInfo) => {
            // 设置不同类型文件的输出路径及命名规则
            if (
              assetInfo.type === 'asset' &&
              /\.(jpe?g|png|gif|svg)$/i.test(assetInfo.name as string)
            ) {
              return 'img/[name].[hash].[ext]' // 图像文件输出路径及命名规则
            }
            if (
              assetInfo.type === 'asset' &&
              /\.(ttf|woff|woff2|eot)$/i.test(assetInfo.name as string)
            ) {
              return 'fonts/[name].[hash].[ext]' // 字体文件输出路径及命名规则
            }
            return '[ext]/name1-[hash].[ext]' // 其他资源文件输出路径及命名规则
          }
        }
      }
    }
  };
});







