feat(package): add postbuild script to gzip distribution files

This commit is contained in:
Jaime Idolpx 2026-06-12 06:45:53 -04:00
parent 7e4b078d1f
commit 0c2fa2479c
2 changed files with 33 additions and 0 deletions

View File

@ -5,6 +5,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "vite build", "build": "vite build",
"postbuild": "node scripts/gzip-dist.mjs",
"dev": "vite" "dev": "vite"
}, },
"dependencies": { "dependencies": {

32
scripts/gzip-dist.mjs Normal file
View File

@ -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'}.`);