-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.ts
125 lines (114 loc) · 2.58 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { exists, mkdir, unlink } from "node:fs/promises"
// Ex. ./src/main.ts
const mainModulePath = process.argv[2]
// Ensure file exists
if ((await exists(mainModulePath)) !== true) {
throw new Error(`module not found: ${mainModulePath}`)
}
// Get current architecture for build
const arch = process.arch === 'arm64' ? 'arm64' : 'x86_64'
// Bootstrap source should be in same directory as main
const bootstrapSourcePath = mainModulePath.replace(
/\.(ts|js|cjs|mjs)$/,
".bootstrap.ts",
)
// Read in bootstrap source
const bootstrapSource = await Bun.file("node_modules/bun-vercel/bootstrap.ts")
.text()
.catch(() => Bun.file("bootstrap.ts").text())
// Write boostrap source to bootstrap file
await Bun.write(
bootstrapSourcePath,
bootstrapSource.replace(
'import main from "./example/main"',
`import main from "./${mainModulePath.split("/").pop()}"`,
),
)
// Create output directory
await mkdir("./.vercel/output/functions/App.func", {
recursive: true,
})
// Create function config file
await Bun.write(
"./.vercel/output/functions/App.func/.vc-config.json",
JSON.stringify(
{
architecture: arch,
handler: "bootstrap",
maxDuration: 10,
memory: 1024,
runtime: "provided.al2",
supportsWrapper: false,
},
null,
2,
),
)
// Create routing config file
await Bun.write(
"./.vercel/output/config.json",
JSON.stringify(
{
framework: {
version: Bun.version,
},
overrides: {},
routes: [
{
headers: {
Location: "/$1",
},
src: "^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))/$",
status: 308,
},
{
handle: "filesystem",
},
{
check: true,
dest: "App",
src: "^.*$",
},
],
version: 3,
},
null,
2,
),
)
// Compile to a single bun executable
if (await exists("/etc/system-release")) {
await Bun.spawnSync({
cmd: [
"bun",
"build",
bootstrapSourcePath,
"--compile",
"--minify",
"--outfile",
".vercel/output/functions/App.func/bootstrap",
],
stdout: "pipe",
})
} else {
await Bun.spawnSync({
cmd: [
"docker",
"run",
"--platform",
`linux/${arch}`,
"--rm",
"-v",
`${process.cwd()}:/app`,
"-w",
"/app",
"oven/bun",
"bash",
"-cl",
`bun build ${bootstrapSourcePath} --compile --minify --outfile .vercel/output/functions/App.func/bootstrap`,
],
stdout: "pipe",
})
}
// Cleanup bootstrap file
await unlink(bootstrapSourcePath)