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