feat: add dev/production modes to ts build script
This commit is contained in:
@@ -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<void> => {
|
||||
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<void> => {
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user