From 0c2fa2479c6478fbf7a223258b3921632c84b0c4 Mon Sep 17 00:00:00 2001 From: Jaime Idolpx Date: Fri, 12 Jun 2026 06:45:53 -0400 Subject: [PATCH] feat(package): add postbuild script to gzip distribution files --- package.json | 1 + scripts/gzip-dist.mjs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 scripts/gzip-dist.mjs diff --git a/package.json b/package.json index 4097793..09b1c65 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "build": "vite build", + "postbuild": "node scripts/gzip-dist.mjs", "dev": "vite" }, "dependencies": { diff --git a/scripts/gzip-dist.mjs b/scripts/gzip-dist.mjs new file mode 100644 index 0000000..a4172ca --- /dev/null +++ b/scripts/gzip-dist.mjs @@ -0,0 +1,32 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { readdir, unlink } from 'node:fs/promises'; +import { extname, join } from 'node:path'; +import { pipeline } from 'node:stream/promises'; +import { createGzip } from 'node:zlib'; + +const EXTS = new Set(['.html', '.js', '.css', '.svg']); +const DIST = 'dist'; + +async function* walk(dir) { + for (const entry of await readdir(dir, { withFileTypes: true })) { + const full = join(dir, entry.name); + if (entry.isDirectory()) { + yield* walk(full); + } else if (EXTS.has(extname(entry.name))) { + yield full; + } + } +} + +let count = 0; +for await (const file of walk(DIST)) { + await pipeline( + createReadStream(file), + createGzip({ level: 9 }), + createWriteStream(`${file}.gz`), + ); + await unlink(file); + count++; + console.log(` gzip ${file}`); +} +console.log(`\nGzipped ${count} file${count === 1 ? '' : 's'}.`);