buildSingle.js 1.62 KB
import path from 'node:path';
import URL from 'node:url';

import terser from '@rollup/plugin-terser';
// 单独打包指定的包
import { rollup } from 'rollup';

const __filename = URL.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function buildSingle(packageName) {
  if (!packageName) {
    console.error('请指定要构建的包名');
    process.exit(1);
  }

  const packagePath = path.resolve(__dirname, `../../packages/${packageName}`);

  try {
    // 检查包是否存在
    const fs = await import('node:fs');
    if (!fs.existsSync(packagePath)) {
      console.error(`包 ${packageName} 不存在`);
      process.exit(1);
    }

    console.log(`正在构建包: ${packageName}`);

    // 清理 dist 目录
    const distPath = path.resolve(packagePath, 'dist');
    if (fs.existsSync(distPath)) {
      fs.rmSync(distPath, { recursive: true, force: true });
    }

    // 获取构建配置
    const { getRollupConfig } = await import('./buildBase.js');
    const config = await getRollupConfig(packagePath);

    console.log(`正在打包: ${packageName}`);
    const bundle = await rollup({
      input: config.input,
      plugins: [...config.plugins, terser()],
      external: config.external,
    });

    const tasks = [];
    for (const output of config.output) {
      tasks.push(bundle.write(output));
    }

    await Promise.all(tasks);
    console.log(`打包完成: ${packageName}`);
  } catch (error) {
    console.error(`构建失败: ${error.message}`);
    process.exit(1);
  }
}

// 从命令行参数获取包名
const packageName = process.argv[2];
buildSingle(packageName);