feat(package): add postbuild script to gzip distribution files
This commit is contained in:
parent
7e4b078d1f
commit
0c2fa2479c
|
|
@ -5,6 +5,7 @@
|
|||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "vite build",
|
||||
"postbuild": "node scripts/gzip-dist.mjs",
|
||||
"dev": "vite"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
|||
32
scripts/gzip-dist.mjs
Normal file
32
scripts/gzip-dist.mjs
Normal 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'}.`);
|
||||
Loading…
Reference in New Issue
Block a user