forked from icellan/bap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
87 lines (76 loc) · 1.88 KB
/
build.ts
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
import type { Target } from 'bun';
import { spawn } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { gzipSync } from 'node:zlib';
const commonConfig = {
entrypoints: ['./src/index.ts'],
external: ['@bsv/sdk'],
sourcemap: "external" as const,
minify: true,
target: "bun" as Target,
};
async function generateTypes() {
return new Promise((resolve, reject) => {
const tsc = spawn('tsc', ['--emitDeclarationOnly', '--declaration'], {
stdio: 'inherit',
shell: true
});
tsc.on('close', (code) => {
if (code === 0) {
resolve(undefined);
} else {
reject(new Error(`tsc exited with code ${code}`));
}
});
});
}
async function build() {
// Generate type declarations
console.log('\nGenerating type declarations...');
await generateTypes();
// Build bundles
console.log('\nBuilding bundles...');
// ESM build (modern)
await Bun.build({
...commonConfig,
outdir: './dist',
format: 'esm',
naming: {
entry: 'index.modern.js',
chunk: '[name]-[hash].modern.js',
},
});
// ESM build (module)
await Bun.build({
...commonConfig,
outdir: './dist',
format: 'esm',
naming: {
entry: 'index.module.js',
chunk: '[name]-[hash].module.js',
},
});
// CJS build
await Bun.build({
...commonConfig,
outdir: './dist',
format: 'cjs',
naming: {
entry: 'index.cjs',
chunk: '[name]-[hash].cjs',
},
});
// Report sizes
const files = [
'dist/index.modern.js',
'dist/index.module.js',
'dist/index.cjs'
];
console.log('\nBuild sizes:');
for (const file of files) {
const content = readFileSync(file);
const gzipped = gzipSync(content);
console.log(`${file}: ${(content.length / 1024).toFixed(2)} kB (${(gzipped.length / 1024).toFixed(2)} kB gzipped)`);
}
}
build().catch(console.error);