diff --git a/scripts/build-ts.sh b/scripts/build-ts.sh deleted file mode 100755 index 27a7410..0000000 --- a/scripts/build-ts.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -started_at=$(date +%s%N) - -steps=( - "player:bun build ./static/player/main.ts --outdir ./dist/static/player --target browser --splitting" - "app:bun build ./static/app.ts --outdir ./dist/static --target browser --root ./static --entry-naming [name].js" -) - -for step in "${steps[@]}"; do - name="${step%%:*}" - cmd="${step#*:}" - echo "building $name..." - if ! eval "$cmd"; then - echo "ts build failed at $name" >&2 - exit 1 - fi -done - -mkdir -p ./dist/static -cp ./node_modules/htmx.org/dist/htmx.min.js ./dist/static/htmx-lib.js - -total_entries=2 -elapsed_ms=$((($(date +%s%N) - started_at) / 1000000)) -echo "ts build ok ($total_entries entries, ${elapsed_ms}ms)" \ No newline at end of file diff --git a/scripts/build-ts.ts b/scripts/build-ts.ts new file mode 100644 index 0000000..4712e9f --- /dev/null +++ b/scripts/build-ts.ts @@ -0,0 +1,70 @@ +import { spawnSync } from "node:child_process"; +import { cp, mkdir } from "node:fs/promises"; +import { performance } from "node:perf_hooks"; + +type BuildStep = { + name: string; + args: string[]; +}; + +const buildSteps: BuildStep[] = [ + { + name: "player", + args: [ + "build", + "./static/player/main.ts", + "--outdir", + "./dist/static/player", + "--target", + "browser", + "--splitting", + ], + }, + { + name: "app", + args: [ + "build", + "./static/app.ts", + "--outdir", + "./dist/static", + "--target", + "browser", + "--root", + "./static", + "--entry-naming", + "[name].js", + ], + }, +]; + +const runBuildStep = (step: BuildStep): void => { + console.log(`building ${step.name}...`); + + const result = spawnSync("bun", step.args, { stdio: "inherit" }); + if (result.error) { + throw new Error(`failed to start ${step.name} build: ${result.error.message}`); + } + if (result.signal) { + throw new Error(`ts build interrupted at ${step.name} (${result.signal})`); + } + if (result.status !== 0) { + throw new Error(`ts build failed at ${step.name}`); + } +}; + +const main = async (): Promise => { + const startedAt = performance.now(); + + buildSteps.forEach(runBuildStep); + + await mkdir("./dist/static", { recursive: true }); + await cp("./node_modules/htmx.org/dist/htmx.min.js", "./dist/static/htmx-lib.js"); + + const elapsedMs = Math.round(performance.now() - startedAt); + console.log(`ts build ok (${buildSteps.length} entries, ${elapsedMs}ms)`); +}; + +main().catch((error: unknown) => { + console.error(error instanceof Error ? error.message : String(error)); + process.exitCode = 1; +});