-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuild.ts
More file actions
171 lines (150 loc) · 4.88 KB
/
build.ts
File metadata and controls
171 lines (150 loc) · 4.88 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bun
/**
* Build script for backend
*/
import { existsSync } from 'node:fs';
import { rm, cp, mkdir } from 'node:fs/promises';
import { $ } from 'bun';
import { resolve, dirname } from 'node:path';
async function cleanBuild(outdir = 'dist') {
if (existsSync(outdir)) {
await rm(outdir, { recursive: true, force: true });
console.log(` Cleaned ${outdir} directory`);
}
}
async function copySharedModules() {
console.log(' Copying shared modules...');
// Copy and compile managers directory to dist/managers
if (existsSync('./src/managers')) {
await cp('./src/managers', './dist/managers', { recursive: true });
console.log(' Copied managers/');
}
// Copy and compile constants directory to dist/constants
if (existsSync('./src/constants')) {
await cp('./src/constants', './dist/constants', { recursive: true });
console.log(' Copied constants/');
}
// Compile the TypeScript files in place
try {
console.log(' Compiling shared modules...');
// Build managers
const managersResult = await Bun.build({
entrypoints: ['./dist/managers/cdp-transaction-manager.ts'],
outdir: './dist/managers',
target: 'node',
format: 'esm',
external: ['@elizaos/core', '@coinbase/cdp-sdk', 'viem', 'viem/accounts'],
naming: { entry: '[name].js' },
});
// Build constants
const constantsResult = await Bun.build({
entrypoints: ['./dist/constants/chains.ts'],
outdir: './dist/constants',
target: 'node',
format: 'esm',
external: ['viem/chains'],
naming: { entry: '[name].js' },
});
if (managersResult.success && constantsResult.success) {
console.log(' Shared modules compiled successfully');
} else {
console.warn(' Warning: Some shared modules failed to compile');
}
} catch (error) {
console.warn(' Warning: Failed to compile shared modules:', error);
}
}
async function build() {
const start = performance.now();
console.log(' Building backend...');
try {
// Clean previous build
await cleanBuild('dist');
// Build backend
const [buildResult, tscResult] = await Promise.all([
// Task 1: Build with Bun
(async () => {
console.log(' Bundling backend with Bun...');
const result = await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
target: 'node',
format: 'esm',
sourcemap: 'external', // Railway's Bun 1.2.10 requires string, not boolean
minify: false,
external: [
'dotenv',
'fs',
'path',
'https',
'node:*',
'@elizaos/core',
'@elizaos/plugin-bootstrap',
'@elizaos/plugin-sql',
'@elizaos/server',
'zod',
],
naming: {
entry: '[dir]/[name].[ext]',
},
// Add path resolution plugin to handle @/ aliases
plugins: [
{
name: 'path-alias-resolver',
setup(build) {
build.onResolve({ filter: /^@\// }, (args) => {
// Make these imports external and rewrite them to relative paths from dist/
const relativePath = args.path.slice(2); // Remove "@/"
// Return as external with the rewritten path
return { path: `./${relativePath}.js`, external: true };
});
},
},
],
});
if (!result.success) {
console.error(' Build failed:', result.logs);
return { success: false };
}
const totalSize = result.outputs.reduce((sum, output) => sum + output.size, 0);
const sizeMB = (totalSize / 1024 / 1024).toFixed(2);
console.log(` Built ${result.outputs.length} file(s) - ${sizeMB}MB`);
return result;
})(),
// Task 2: Generate TypeScript declarations
(async () => {
console.log(' Generating TypeScript declarations...');
try {
await $`tsc --emitDeclarationOnly`.quiet();
console.log(' TypeScript declarations generated');
return { success: true };
} catch (error) {
console.warn(' Failed to generate TypeScript declarations');
return { success: false };
}
})(),
]);
if (!buildResult.success) {
return false;
}
// Copy shared modules after build
await copySharedModules();
const elapsed = ((performance.now() - start) / 1000).toFixed(2);
console.log(` Backend build complete! (${elapsed}s)`);
return true;
} catch (error) {
console.error('Build error:', error);
return false;
}
}
// Execute the build
build()
.then((success) => {
if (!success) {
process.exit(1);
}
})
.catch((error) => {
console.error('Build script error:', error);
process.exit(1);
});