/**
 * 全量构建
 * @param entryPath 入口文件路径
 * @param outputDirPath 导出的文件夹路径
 */
import { onWriteFile } from "./utils";
import { customConfig, rollupOptions } from "../config";
import { build, defineConfig, InlineConfig, UserConfig } from "vite";

// 生成组件的 package.json 文件
const createPackageJson = (outputDirPath: string) => {
  const fileStr = `{
    "name": "@wltech/mobile-ui",
    "version": "0.0.0",
    "main": "index.es.js"
  }`;
  return onWriteFile(`${outputDirPath}/package.json`, fileStr);
};

const buildAll = async (entryPath: string, outputDirPath: string) => {
  const buildConfig = {
    ...customConfig,
    build: {
      rollupOptions,
      lib: {
        entry: entryPath,
        name: "weilingMobileUi",
        fileName: () => "index.js",
        formats: ["es"],
      },
      outDir: outputDirPath,
    },
  };
  await build(defineConfig(buildConfig as UserConfig) as InlineConfig);
  await createPackageJson(outputDirPath);
};

export default buildAll;
