-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
110 lines (84 loc) · 2.83 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-env node */
import { BuildOptions, build } from 'esbuild';
import { bold, dim, cyan, green, magenta } from 'kleur/colors';
import { execa } from 'execa';
import { readFile, readdir, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
try {
for (const f of await readdir('dist')) {
await rm(join('dist', f));
}
} catch (error: unknown) {
if (!(error instanceof Error && typeof error.message.includes('ENOENT'))) {
throw error;
}
}
const size = async (f: string) => {
const data = await readFile(f);
let bytes = data.byteLength;
const thresh = 1000;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let u = -1;
const r = 10;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(1) + ' ' + units[u];
};
const commonOptions = {
format: 'esm',
platform: 'neutral',
bundle: true,
minify: true,
} satisfies BuildOptions;
const revision = await execa('git', ['rev-parse', '--short', 'HEAD']).then((p) => p.stdout);
const commonDefines = {
VENDFLARE_REVISION: JSON.stringify(revision),
} as const;
const honoModules = ['cors', 'timing', 'secure-headers'];
const tinyAlias = Object.fromEntries([
...honoModules.map((mod) => [`hono/${mod}`, `hono/${mod}`]),
['hono', 'hono/tiny'],
]) as Record<string, string>;
console.log(bold(`vendflare@${revision}`));
const columnWidth = 30;
const logBuild = async (name: string, file: string, color: (arg0: string) => string) => {
name = `${name} build`;
const sizeString = await size(join('dist', file));
const firstPad = columnWidth - name.length;
const secondPad = columnWidth - (file.length + 5);
console.log(
color(name) + ' '.repeat(firstPad + 2) + dim('dist/') + file + ' '.repeat(secondPad + 2) + color(sizeString),
);
};
const workerDeclaration = `
import worker from '../src/worker';
export default worker;
`.trimStart();
const durableDeclaration = `
export { UserData } from '../src/worker';
`.trimStart();
for (const preset of ['default', 'tiny'] as const) {
for (const backend of ['all', 'kv', 'do'] as const) {
const outfile = `worker${backend === 'all' ? '' : `.${backend}`}${preset === 'tiny' ? '.tiny' : ''}.js`;
await build({
...commonOptions,
entryPoints: backend === 'kv' ? ['src/worker.kv.ts'] : ['src/worker.ts'],
outfile: join('dist', outfile),
define: {
...commonDefines,
},
dropLabels: backend === 'kv' ? ['durable'] : backend === 'do' ? ['kv'] : [],
alias: preset === 'tiny' ? tinyAlias : undefined,
});
await writeFile(
join('dist', outfile.replace(/\.js$/, '.d.ts')),
workerDeclaration + (backend === 'kv' ? '' : durableDeclaration),
);
await logBuild(`${preset} ${backend}`, outfile, backend === 'all' ? cyan : backend === 'do' ? green : magenta);
}
}