// After the build: write brotli and gzip copies of every text file, so the // server can send them compressed without compressing on each request. import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'; import { extname, join } from 'node:path'; import { brotliCompressSync, constants, gzipSync } from 'node:zlib'; const root = new URL('../dist/', import.meta.url).pathname; const TEXT = new Set(['.js', '.mjs', '.css', '.html', '.svg', '.json', '.webmanifest', '.txt']); let count = 0; function walk(dir) { for (const name of readdirSync(dir)) { const path = join(dir, name); if (statSync(path).isDirectory()) walk(path); else if (TEXT.has(extname(name)) && statSync(path).size > 1024) { const data = readFileSync(path); writeFileSync(`${path}.br`, brotliCompressSync(data, { params: { [constants.BROTLI_PARAM_QUALITY]: 11 } })); writeFileSync(`${path}.gz`, gzipSync(data, { level: 9 })); count++; } } } walk(root); console.log(`Compressed ${count} files`);