-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
71 lines (62 loc) · 1.73 KB
/
build.mjs
File metadata and controls
71 lines (62 loc) · 1.73 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
62
63
64
65
66
67
68
69
70
71
import * as esbuild from 'esbuild';
import { readdirSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// 获取所有需要构建的脚本
const scriptsDir = join(__dirname, 'src', 'scripts');
const scripts = readdirSync(scriptsDir)
.filter(file => file.endsWith('.ts'))
.map(file => join(scriptsDir, file));
const isWatch = process.argv.includes('--watch');
// 公共构建配置
const buildOptions = {
bundle: true,
platform: 'node',
target: 'node18',
format: 'cjs',
minify: false,
sourcemap: false,
// 保持代码可读性
keepNames: true,
// 添加 banner 注释
banner: {
js: '#!/usr/bin/env node\n// Auto-generated by build.js - DO NOT EDIT\n'
},
// 定义环境变量
define: {
'process.env.NODE_ENV': '"production"'
}
};
async function build() {
console.log('🔨 Building scripts...\n');
for (const script of scripts) {
const scriptName = script.split('/').pop().replace('.ts', '.js');
const outfile = join(__dirname, 'skill', 'scripts', scriptName);
try {
if (isWatch) {
const ctx = await esbuild.context({
...buildOptions,
entryPoints: [script],
outfile
});
await ctx.watch();
console.log(`👀 Watching ${scriptName}...`);
} else {
await esbuild.build({
...buildOptions,
entryPoints: [script],
outfile
});
console.log(`✅ Built ${scriptName}`);
}
} catch (error) {
console.error(`❌ Failed to build ${scriptName}:`, error.message);
process.exit(1);
}
}
if (!isWatch) {
console.log('\n🎉 Build completed!');
}
}
build();