Import source: global/scripts/compress-dist.mjs
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
import { readdirSync, statSync, createReadStream, createWriteStream } from 'fs';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { createGzip, createBrotliCompress, constants } from 'zlib';
|
||||||
|
import { pipeline } from 'stream/promises';
|
||||||
|
|
||||||
|
const COMPRESS_EXT = ['.html', '.js', '.css', '.xml', '.json', '.svg'];
|
||||||
|
|
||||||
|
function walk(dir) {
|
||||||
|
const results = [];
|
||||||
|
for (const f of readdirSync(dir)) {
|
||||||
|
const p = join(dir, f);
|
||||||
|
if (statSync(p).isDirectory()) results.push(...walk(p));
|
||||||
|
else if (COMPRESS_EXT.some(e => f.endsWith(e))) results.push(p);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
let gz = 0, br = 0;
|
||||||
|
for (const file of walk('dist')) {
|
||||||
|
await pipeline(createReadStream(file), createGzip({ level: 9 }), createWriteStream(`${file}.gz`));
|
||||||
|
gz++;
|
||||||
|
await pipeline(
|
||||||
|
createReadStream(file),
|
||||||
|
createBrotliCompress({ params: { [constants.BROTLI_PARAM_QUALITY]: 11 } }),
|
||||||
|
createWriteStream(`${file}.br`)
|
||||||
|
);
|
||||||
|
br++;
|
||||||
|
}
|
||||||
|
console.log(`[compress] gzip: ${gz} files, brotli: ${br} files`);
|
||||||
Reference in New Issue
Block a user