forked from Haleclipse/Claudix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.ts
More file actions
124 lines (113 loc) · 3.97 KB
/
esbuild.ts
File metadata and controls
124 lines (113 loc) · 3.97 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import esbuild from "esbuild";
import { createRequire } from "module";
import path from "path";
import fs from "fs/promises";
const production = process.argv.includes('--production');
const watch = process.argv.includes('--watch');
/**
* @type {import('esbuild').Plugin}
*/
const esbuildProblemMatcherPlugin = {
name: 'esbuild-problem-matcher',
setup(build: { onStart: (arg0: () => void) => void; onEnd: (arg0: (result: esbuild.BuildResult) => void) => void; }) {
build.onStart(() => {
console.log('[watch] build started');
});
build.onEnd((result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
if (location) {
console.error(` ${location.file}:${location.line}:${location.column}:`);
}
});
console.log('[watch] build finished');
});
},
};
/**
* 在构建完成后,将 SDK 的 CLI 文件复制到 dist/
* 这样运行时可通过显式 path 调用,避免 import.meta.url 在打包后失效。
* @type {import('esbuild').Plugin}
*/
const copyClaudeCliPlugin = {
name: 'copy-claude-cli',
setup(build: { onEnd: (arg0: () => Promise<void>) => void; }) {
const require = createRequire(import.meta.url);
build.onEnd(async () => {
try {
const pkgDir = path.dirname(require.resolve('@anthropic-ai/claude-code/cli.js'));
const outDir = path.resolve(process.cwd(), 'dist');
await fs.mkdir(outDir, { recursive: true });
// copy cli.js
const cliSrc = path.join(pkgDir, 'cli.js');
const cliDst = path.join(outDir, 'claude-cli.js');
await fs.copyFile(cliSrc, cliDst);
console.log(`[build] Copied Claude CLI -> ${path.relative(process.cwd(), cliDst)}`);
// copy yoga.wasm (required by CLI at runtime)
const wasmSrc = path.join(pkgDir, 'yoga.wasm');
try {
await fs.copyFile(wasmSrc, path.join(outDir, 'yoga.wasm'));
console.log(`[build] Copied yoga.wasm`);
} catch (e) {
console.warn('[build] yoga.wasm not found, SDK may fail at runtime');
}
// copy vendor directory if exists (CLI may read assets/configs)
const vendorSrc = path.join(pkgDir, 'vendor');
try {
const st = await fs.stat(vendorSrc);
if (st.isDirectory()) {
const vendorDst = path.join(outDir, 'vendor');
await copyDir(vendorSrc, vendorDst);
console.log('[build] Copied vendor/ directory');
}
} catch {}
} catch (err: any) {
console.warn('[build] copy-claude-cli failed:', err?.message || err);
}
});
},
};
async function copyDir(src: string, dst: string) {
await fs.mkdir(dst, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const ent of entries) {
const s = path.join(src, ent.name);
const d = path.join(dst, ent.name);
if (ent.isDirectory()) {
await copyDir(s, d);
} else if (ent.isFile()) {
await fs.copyFile(s, d);
}
}
}
async function main() {
const ctx = await esbuild.context({
entryPoints: [
'src/extension.ts'
],
bundle: true,
format: 'cjs',
minify: production,
sourcemap: !production,
sourcesContent: false,
platform: 'node',
outfile: 'dist/extension.cjs',
external: ['vscode'],
logLevel: 'silent',
plugins: [
/* add to the end of plugins array */
esbuildProblemMatcherPlugin,
// copyClaudeCliPlugin,
],
});
if (watch) {
await ctx.watch();
} else {
await ctx.rebuild();
await ctx.dispose();
}
}
main().catch(e => {
console.error(e);
process.exit(1);
});