-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbun.build.js
More file actions
151 lines (135 loc) · 3.56 KB
/
bun.build.js
File metadata and controls
151 lines (135 loc) · 3.56 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
// bun.build.js
import { rmSync, copyFileSync, mkdirSync } from 'node:fs';
import { resolve, join } from 'node:path';
// Clean output dir
const outdir = "dist";
const outdirCLI = join(outdir, "cli");
const outdirBrowser = join(outdir, "browser");
rmSync(outdir, { recursive: true, force: true });
mkdirSync(outdir, { recursive: true });
// Bundles via Bun.build()
// Helper to check success
async function doBuild(opts) {
const result = await Bun.build(opts);
if (!result.success) {
for (const log of result.logs) {
console.error(log.text || log);
}
throw new Error("Build failed");
}
}
// ---- Node runtime (ESM) ----
await doBuild({
entrypoints: ["packages/node-runtime/src/index.ts"],
outdir: outdir,
minify: true,
format: "esm",
external: ["argon2-browser", "@node-rs/argon2"],
target: "node",
naming: {
entry: "cryptit.index.[ext]",
chunk: "[name]-[hash].[ext]",
asset: "[name]-[hash].[ext]",
},
sourcemap: "external",
});
// ---- Node runtime (CJS) ----
await doBuild({
entrypoints: ["packages/node-runtime/src/index.ts"],
outdir: outdir,
minify: true,
format: "cjs",
external: ["argon2-browser", "@node-rs/argon2"],
target: "node",
naming: {
entry: "cryptit.index.cjs",
chunk: "[name]-[hash].[ext]",
asset: "[name]-[hash].[ext]",
},
sourcemap: "external",
});
// ---- CLI (CJS) ----
await doBuild({
entrypoints: ["packages/node-runtime/src/cli.ts"],
outdir: outdirCLI,
minify: true,
format: "cjs",
external: ["argon2-browser", "@node-rs/argon2"],
target: "node",
naming: {
entry: "cryptit.cli.cjs",
chunk: "[name]-[hash].[ext]",
asset: "[name]-[hash].[ext]",
},
sourcemap: "external",
});
// ---- CLI (ESM) ----
await doBuild({
entrypoints: ["packages/node-runtime/src/cli.ts"],
outdir: outdirCLI,
minify: true,
format: "esm",
external: ["argon2-browser", "@node-rs/argon2"],
target: "node",
naming: {
entry: "cryptit.cli.[ext]",
chunk: "[name]-[hash].[ext]",
asset: "[name]-[hash].[ext]",
},
sourcemap: "external",
});
// Compile CLI to standalone binary (fallback to CLI flag)
await Bun.spawn({
cmd: [
"bun",
"build",
"--compile",
`--outfile=${resolve("local-compiled", "bin", "cryptit")}`,
"packages/node-runtime/src/cli.ts",
],
});
// ---- Browser bundle ----
await doBuild({
entrypoints: ["packages/browser-runtime/src/index.ts"],
outdir: outdirBrowser,
minify: {
whitespace: true,
identifiers: false,
syntax: true,
keepNames: true,
},
format: "esm",
external: ["commander", "buffer", "process", "@node-rs/argon2"],
target: "browser",
naming: {
entry: "cryptit.browser.min.[ext]",
chunk: "[name]-[hash].[ext]",
asset: "[name].[ext]",
},
sourcemap: "external",
});
/*
// ---- Browser bundle ----
await doBuild({
entrypoints: ["packages/browser-runtime/src/index.ts"],
outdir: outdirBrowser,
minify: true,
format: "iife",
external: ["commander", "buffer", "process", "argon2"],
target: "browser",
footer: "globalThis.createCryptit = createCryptit;",
naming: {
entry: "cryptit.browser.global.min.[ext]",
chunk: "[name]-[hash].[ext]",
asset: "[name].[ext]",
},
sourcemap: "external",
});*/
// Post-build WASM handling
const wasmSrc = join(outdirBrowser, "argon2.wasm");
const wasmDst = join("examples", "assets", "argon2.wasm");
const browserSrc = join(outdirBrowser, "cryptit.browser.min.js");
const browserDst = join("examples", "assets", "cryptit.browser.min.js");
copyFileSync(wasmSrc, wasmDst);
copyFileSync(browserSrc, browserDst);
console.log("All builds complete");