From 49abffb21aa4493e8dae4b07e0ccae7ebb59a809 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Fri, 3 Jul 2026 02:36:42 +0200 Subject: [PATCH] feat: add dev/production modes to ts build script --- scripts/build-ts.ts | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/scripts/build-ts.ts b/scripts/build-ts.ts index bf584775..814a4d3b 100644 --- a/scripts/build-ts.ts +++ b/scripts/build-ts.ts @@ -1,9 +1,12 @@ import { spawnSync } from "node:child_process"; -import { cp, mkdir } from "node:fs/promises"; +import { cp, mkdir, readFile, rm } from "node:fs/promises"; import { performance } from "node:perf_hooks"; +import { gzipSync } from "node:zlib"; type BuildStep = { name: string; args: string[] }; +const isProduction = !process.argv.includes("--development"); + const buildSteps: BuildStep[] = [ { name: "player", @@ -15,6 +18,7 @@ const buildSteps: BuildStep[] = [ "--target", "browser", "--splitting", + "--sourcemap=none", ], }, { @@ -30,14 +34,18 @@ const buildSteps: BuildStep[] = [ "./static", "--entry-naming", "[name].js", + "--sourcemap=none", ], }, ]; +const bundlePaths = ["./dist/static/app.js", "./dist/static/player/main.js"]; + const runBuildStep = (step: BuildStep): void => { console.log(`building ${step.name}...`); - const result = spawnSync("bun", step.args, { stdio: "inherit" }); + const args = isProduction ? [...step.args, "--minify"] : step.args; + const result = spawnSync("bun", args, { stdio: "inherit" }); if (result.error) { throw new Error(`failed to start ${step.name} build: ${result.error.message}`); } @@ -49,18 +57,39 @@ const runBuildStep = (step: BuildStep): void => { } }; +const formatBytes = (bytes: number): string => `${(bytes / 1024).toFixed(1)}KB`; + +const printBundleSizes = async (): Promise => { + console.log("bundle sizes:"); + + const results = await Promise.all( + bundlePaths.map(async (path) => { + const contents = await readFile(path); + return { path, raw: contents.byteLength, gzip: gzipSync(contents).byteLength }; + }), + ); + + for (const { path, raw, gzip } of results) { + console.log(` ${path}: ${formatBytes(raw)} raw, ${formatBytes(gzip)} gzip`); + } +}; + const main = async (): Promise => { const startedAt = performance.now(); + await rm("./dist/static", { recursive: true, force: true }); + await mkdir("./dist/static", { recursive: true }); + buildSteps.forEach((step) => { runBuildStep(step); }); - await mkdir("./dist/static", { recursive: true }); await cp("./node_modules/htmx.org/dist/htmx.min.js", "./dist/static/htmx-lib.js"); + await printBundleSizes(); const elapsedMs = Math.round(performance.now() - startedAt); - console.log(`ts build ok (${buildSteps.length} entries, ${elapsedMs}ms)`); + const mode = isProduction ? "production" : "development"; + console.log(`ts build ok (${buildSteps.length} entries, ${mode}, ${elapsedMs}ms)`); }; main().catch((error: unknown) => {