forked from stephanrauh/extended-pdf-viewer-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.js
42 lines (37 loc) · 1.32 KB
/
compress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const brotli = require('brotli');
const fs = require('fs');
var dir = require('node-dir');
const start = new Date().getTime();
dir.files('dist/pdf-showcase', function(err, files) {
if (err) throw err;
files.forEach(f => compress(f));
const end = new Date().getTime();
console.log('Compressing took ' + (end - start) / 1000 + ' seconds');
});
function compress(file) {
if (file.endsWith('.js') || file.endsWith('.css') || file.endsWith('.html') || file.endsWith('.pdf') || file.endsWith('.md')) {
const content = fs.readFileSync(file);
if (content.length > 1000) {
const result = brotli.compress(content, {
mode: 0, // 0 = generic, 1 = text, 2 = font (WOFF2)
quality: 11, // 0 - 11
lgwin: 22 // window size
});
if (result != null && result.length < content.length) {
fs.writeFileSync(file + '.br', result);
const reduction = content.length - result.length;
console.log(file + ' Reduction: ' + reduction + ' bytes ' + Math.round((reduction * 100) / content.length) + '%');
}
}
}
}
/*
const content = fs.readFileSync('package.json');
console.log(content);
const result = brotli.compress(content, {
mode: 0, // 0 = generic, 1 = text, 2 = font (WOFF2)
quality: 11, // 0 - 11
lgwin: 22 // window size
});
fs.writeFileSync('README.md.br', result);
*/