|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { build, type BuildConfig } from "bun"; |
| 3 | +import plugin from "bun-plugin-tailwind"; |
| 4 | +import { existsSync } from "fs"; |
| 5 | +import { rm } from "fs/promises"; |
| 6 | +import path from "path"; |
| 7 | + |
| 8 | +// Print help text if requested |
| 9 | +if (process.argv.includes("--help") || process.argv.includes("-h")) { |
| 10 | + console.log(` |
| 11 | +🏗️ Bun Build Script |
| 12 | +
|
| 13 | +Usage: bun run build.ts [options] |
| 14 | +
|
| 15 | +Common Options: |
| 16 | + --outdir <path> Output directory (default: "dist") |
| 17 | + --minify Enable minification (or --minify.whitespace, --minify.syntax, etc) |
| 18 | + --source-map <type> Sourcemap type: none|linked|inline|external |
| 19 | + --target <target> Build target: browser|bun|node |
| 20 | + --format <format> Output format: esm|cjs|iife |
| 21 | + --splitting Enable code splitting |
| 22 | + --packages <type> Package handling: bundle|external |
| 23 | + --public-path <path> Public path for assets |
| 24 | + --env <mode> Environment handling: inline|disable|prefix* |
| 25 | + --conditions <list> Package.json export conditions (comma separated) |
| 26 | + --external <list> External packages (comma separated) |
| 27 | + --banner <text> Add banner text to output |
| 28 | + --footer <text> Add footer text to output |
| 29 | + --define <obj> Define global constants (e.g. --define.VERSION=1.0.0) |
| 30 | + --help, -h Show this help message |
| 31 | +
|
| 32 | +Example: |
| 33 | + bun run build.ts --outdir=dist --minify --source-map=linked --external=react,react-dom |
| 34 | +`); |
| 35 | + process.exit(0); |
| 36 | +} |
| 37 | + |
| 38 | +// Helper function to convert kebab-case to camelCase |
| 39 | +const toCamelCase = (str: string): string => { |
| 40 | + return str.replace(/-([a-z])/g, g => g[1].toUpperCase()); |
| 41 | +}; |
| 42 | + |
| 43 | +// Helper function to parse a value into appropriate type |
| 44 | +const parseValue = (value: string): any => { |
| 45 | + // Handle true/false strings |
| 46 | + if (value === "true") return true; |
| 47 | + if (value === "false") return false; |
| 48 | + |
| 49 | + // Handle numbers |
| 50 | + if (/^\d+$/.test(value)) return parseInt(value, 10); |
| 51 | + if (/^\d*\.\d+$/.test(value)) return parseFloat(value); |
| 52 | + |
| 53 | + // Handle arrays (comma-separated) |
| 54 | + if (value.includes(",")) return value.split(",").map(v => v.trim()); |
| 55 | + |
| 56 | + // Default to string |
| 57 | + return value; |
| 58 | +}; |
| 59 | + |
| 60 | +// Magical argument parser that converts CLI args to BuildConfig |
| 61 | +function parseArgs(): Partial<BuildConfig> { |
| 62 | + const config: Record<string, any> = {}; |
| 63 | + const args = process.argv.slice(2); |
| 64 | + |
| 65 | + for (let i = 0; i < args.length; i++) { |
| 66 | + const arg = args[i]; |
| 67 | + if (!arg.startsWith("--")) continue; |
| 68 | + |
| 69 | + // Handle --no-* flags |
| 70 | + if (arg.startsWith("--no-")) { |
| 71 | + const key = toCamelCase(arg.slice(5)); |
| 72 | + config[key] = false; |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + // Handle --flag (boolean true) |
| 77 | + if (!arg.includes("=") && (i === args.length - 1 || args[i + 1].startsWith("--"))) { |
| 78 | + const key = toCamelCase(arg.slice(2)); |
| 79 | + config[key] = true; |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + // Handle --key=value or --key value |
| 84 | + let key: string; |
| 85 | + let value: string; |
| 86 | + |
| 87 | + if (arg.includes("=")) { |
| 88 | + [key, value] = arg.slice(2).split("=", 2); |
| 89 | + } else { |
| 90 | + key = arg.slice(2); |
| 91 | + value = args[++i]; |
| 92 | + } |
| 93 | + |
| 94 | + // Convert kebab-case key to camelCase |
| 95 | + key = toCamelCase(key); |
| 96 | + |
| 97 | + // Handle nested properties (e.g. --minify.whitespace) |
| 98 | + if (key.includes(".")) { |
| 99 | + const [parentKey, childKey] = key.split("."); |
| 100 | + config[parentKey] = config[parentKey] || {}; |
| 101 | + config[parentKey][childKey] = parseValue(value); |
| 102 | + } else { |
| 103 | + config[key] = parseValue(value); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return config as Partial<BuildConfig>; |
| 108 | +} |
| 109 | + |
| 110 | +// Helper function to format file sizes |
| 111 | +const formatFileSize = (bytes: number): string => { |
| 112 | + const units = ["B", "KB", "MB", "GB"]; |
| 113 | + let size = bytes; |
| 114 | + let unitIndex = 0; |
| 115 | + |
| 116 | + while (size >= 1024 && unitIndex < units.length - 1) { |
| 117 | + size /= 1024; |
| 118 | + unitIndex++; |
| 119 | + } |
| 120 | + |
| 121 | + return `${size.toFixed(2)} ${units[unitIndex]}`; |
| 122 | +}; |
| 123 | + |
| 124 | +console.log("\n🚀 Starting build process...\n"); |
| 125 | + |
| 126 | +// Parse CLI arguments with our magical parser |
| 127 | +const cliConfig = parseArgs(); |
| 128 | +const outdir = cliConfig.outdir || path.join(process.cwd(), "dist"); |
| 129 | + |
| 130 | +if (existsSync(outdir)) { |
| 131 | + console.log(`🗑️ Cleaning previous build at ${outdir}`); |
| 132 | + await rm(outdir, { recursive: true, force: true }); |
| 133 | +} |
| 134 | + |
| 135 | +const start = performance.now(); |
| 136 | + |
| 137 | +// Scan for all HTML files in the project |
| 138 | +const entrypoints = [...new Bun.Glob("**.html").scanSync("src")] |
| 139 | + .map(a => path.resolve("src", a)) |
| 140 | + .filter(dir => !dir.includes("node_modules")); |
| 141 | +console.log(`📄 Found ${entrypoints.length} HTML ${entrypoints.length === 1 ? "file" : "files"} to process\n`); |
| 142 | + |
| 143 | +// Build all the HTML files |
| 144 | +const result = await build({ |
| 145 | + entrypoints, |
| 146 | + outdir, |
| 147 | + plugins: [plugin], |
| 148 | + minify: true, |
| 149 | + target: "browser", |
| 150 | + sourcemap: "linked", |
| 151 | + define: { |
| 152 | + "process.env.NODE_ENV": JSON.stringify("production"), |
| 153 | + }, |
| 154 | + ...cliConfig, // Merge in any CLI-provided options |
| 155 | +}); |
| 156 | + |
| 157 | +// Print the results |
| 158 | +const end = performance.now(); |
| 159 | + |
| 160 | +const outputTable = result.outputs.map(output => ({ |
| 161 | + "File": path.relative(process.cwd(), output.path), |
| 162 | + "Type": output.kind, |
| 163 | + "Size": formatFileSize(output.size), |
| 164 | +})); |
| 165 | + |
| 166 | +console.table(outputTable); |
| 167 | +const buildTime = (end - start).toFixed(2); |
| 168 | + |
| 169 | +console.log(`\n✅ Build completed in ${buildTime}ms\n`); |
0 commit comments