buildSingle.js
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);