-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
228 lines (191 loc) · 7.06 KB
/
build.ts
File metadata and controls
228 lines (191 loc) · 7.06 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bun
/**
* Production build script using Bun.build with Effect for error handling.
*
* Processes HTML entrypoints, bundles assets with Tailwind CSS,
* and outputs minified production builds with sourcemaps.
*/
import plugin from "bun-plugin-tailwind";
import path from "path";
import { Effect, Console } from "effect";
import { FileSystem } from "@effect/platform";
import { BunContext, BunRuntime } from "@effect/platform-bun";
import { CleanError, BuildError } from "./src/lib/errors";
if (process.argv.includes("--help") || process.argv.includes("-h")) {
console.log(`
🏗️ Bun Build Script
Usage: bun run build.ts [options]
Common Options:
--outdir <path> Output directory (default: "dist")
--minify Enable minification (or --minify.whitespace, --minify.syntax, etc)
--sourcemap <type> Sourcemap type: none|linked|inline|external
--target <target> Build target: browser|bun|node
--format <format> Output format: esm|cjs|iife
--splitting Enable code splitting
--packages <type> Package handling: bundle|external
--public-path <path> Public path for assets
--env <mode> Environment handling: inline|disable|prefix*
--conditions <list> Package.json export conditions (comma separated)
--external <list> External packages (comma separated)
--banner <text> Add banner text to output
--footer <text> Add footer text to output
--define <obj> Define global constants (e.g. --define.VERSION=1.0.0)
--help, -h Show this help message
Example:
bun run build.ts --outdir=dist --minify --sourcemap=linked --external=react,react-dom
`);
process.exit(0);
}
// CLI argument parsing utilities
/** Converts kebab-case to camelCase for CLI flags */
const toCamelCase = (str: string): string => str.replace(/-([a-z])/g, (_, char: string) => char.toUpperCase());
/** Parses string values into appropriate JS types (bool, number, array) */
const parseValue = (value: string): string | number | boolean | string[] => {
if (value === "true") return true;
if (value === "false") return false;
if (/^\d+$/.test(value)) return parseInt(value, 10);
if (/^\d*\.\d+$/.test(value)) return parseFloat(value);
// Comma-separated values become arrays
if (value.includes(",")) return value.split(",").map((v) => v.trim());
return value;
};
/**
* Parses CLI arguments into a config object for Bun.build.
* Supports: --flag, --no-flag, --key=value, --key value, --parent.child=value
*/
function parseArgs(): Record<string, unknown> {
const config: Record<string, unknown> = {};
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === undefined) continue;
if (!arg.startsWith("--")) continue;
// Handle --no-flag syntax for boolean false
if (arg.startsWith("--no-")) {
const key = toCamelCase(arg.slice(5));
config[key] = false;
continue;
}
// Handle standalone flags (--flag without value)
if (!arg.includes("=") && (i === args.length - 1 || args[i + 1]?.startsWith("--"))) {
const key = toCamelCase(arg.slice(2));
config[key] = true;
continue;
}
let key: string;
let value: string;
// Handle --key=value or --key value syntax
if (arg.includes("=")) {
[key, value] = arg.slice(2).split("=", 2) as [string, string];
} else {
key = arg.slice(2);
value = args[++i] ?? "";
}
key = toCamelCase(key);
// Handle nested keys (--minify.whitespace=true)
if (key.includes(".")) {
const [parentKey, childKey] = key.split(".");
if (parentKey && childKey) {
const existing = config[parentKey];
config[parentKey] = typeof existing === "object" && existing !== null ? existing : {};
(config[parentKey] as Record<string, unknown>)[childKey] = parseValue(value);
}
} else {
config[key] = parseValue(value);
}
}
return config;
}
/** Formats bytes into human-readable size (B, KB, MB, GB) */
const formatFileSize = (bytes: number): string => {
const units = ["B", "KB", "MB", "GB"];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(2)} ${units[unitIndex]}`;
};
/**
* Main build program using Effect for structured error handling.
* Cleans output directory, finds entrypoints, runs Bun.build, and reports results.
*/
const buildProgram = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
yield* Console.log("\n🚀 Starting build process...\n");
const cliConfig = parseArgs();
const outdir = typeof cliConfig.outdir === "string" ? cliConfig.outdir : path.join(process.cwd(), "dist");
// Clean previous build
const exists = yield* fs.exists(outdir);
if (exists) {
yield* Console.log(`🗑️ Cleaning previous build at ${outdir}`);
yield* fs.remove(outdir, { recursive: true }).pipe(
Effect.mapError((error) => new CleanError({
message: `Failed to clean directory: ${outdir}`,
cause: { directory: outdir, originalError: error }
}))
);
}
const start = performance.now();
// Find entrypoints
const entrypoints = [...new Bun.Glob("**.html").scanSync("src")]
.map((a: string) => path.resolve("src", a))
.filter((dir: string) => !dir.includes("node_modules"));
yield* Console.log(`📄 Found ${entrypoints.length} HTML ${entrypoints.length === 1 ? "file" : "files"} to process\n`);
// Run build
const result = yield* Effect.tryPromise({
try: () => Bun.build({
entrypoints,
outdir,
plugins: [plugin],
minify: true,
target: "browser",
sourcemap: "linked",
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
},
...cliConfig,
}),
catch: (error) => new BuildError({
message: `Build failed`,
cause: {
originalError: error,
entrypoints,
outdir,
config: cliConfig
}
})
});
const end = performance.now();
// Display results
const outputTable = result.outputs.map((output) => ({
File: path.relative(process.cwd(), output.path),
Type: output.kind,
Size: formatFileSize(output.size),
}));
// Use Effect Console for consistency
yield* Console.log("\nBuild Output:");
yield* Effect.sync(() => console.table(outputTable));
const buildTime = (end - start).toFixed(2);
yield* Console.log(`\n✅ Build completed in ${buildTime}ms\n`);
return result;
});
// Run build with BunRuntime for proper signal handling
// Provides BunContext layer for FileSystem access
BunRuntime.runMain(
buildProgram.pipe(
Effect.provide(BunContext.layer),
// Type-safe error handling by error tag
Effect.catchTag("CleanError", (error) =>
Console.error(`\n❌ Clean failed: ${error.message}\n`).pipe(
Effect.andThen(Effect.fail(error))
)
),
Effect.catchTag("BuildError", (error) =>
Console.error(`\n❌ Build failed: ${error.message}\n`).pipe(
Effect.andThen(Effect.fail(error))
)
)
)
);