Skip to content

Commit

Permalink
chore: minify via terser for gzip size
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Aug 2, 2024
1 parent b92b2ed commit 2ebc4ac
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
69 changes: 68 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions scripts/gzip.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { resolve } from 'jsr:@std/path';
import { minify } from 'npm:terser@~5';

// NOTE: run after "build" task
let input = resolve('npm/index.mjs');
let content = await Deno.readFile(input);
let text = await Deno.readTextFile(input);

let m = await minify(text, {
ecma: 2020,
mangle: true,
compress: true,
module: true,
});

if (m.code) {
console.log('\nMinified\n----------');
console.log(m.code + '\n');
} else {
throw new Error('Error w/ Terser');
}

let bytes = {
raw: content.length,
raw: text.length,
min: m.code.length,
gzip: 0,
};

let stream = new ReadableStream({
start(ctrl) {
ctrl.enqueue(content);
ctrl.enqueue(
new TextEncoder().encode(m.code),
);
ctrl.close();
},
}).pipeThrough(
Expand All @@ -28,7 +46,9 @@ while (true) {

let raw = bytes.raw.toLocaleString();
let gzip = bytes.gzip.toLocaleString();
let min = bytes.min.toLocaleString();
let max = Math.max(raw.length, gzip.length);

console.log('bytes (raw): ', raw.padStart(max, ' '), 'b');
console.log('bytes (min): ', min.padStart(max, ' '), 'b');
console.log('bytes (gzip):', gzip.padStart(max, ' '), 'b');

0 comments on commit 2ebc4ac

Please sign in to comment.