I would like to use this npm module to compress files, but I'm a bit stuck by the documention.
In a linux shell :
npm install brotli # npm@4.1.2 # brotli@1.3.1
node # v6.9.4
Then inside node:
var fs = require('fs');
var brotli = require('brotli');
brotli.compress(fs.readFileSync('myfile.txt')); // output is numbers
fs.writeFile('myfile.txt.br', brotli.compress(fs.readFileSync('bin.tar')), function(err){ if (!err) { console.log('It works!');}});
"It works!"
But the file is full of numbers too...
I've never used streams and fs like that in node, can someone explains how to deal with this? Thanks!
With this simple JS code you are compressing each *.html
*.css
*.js
file inside the folder you choose (in this case /dist
)
const fs = require('fs');
const compress = require('brotli/compress');
const brotliSettings = {
extension: 'br',
skipLarger: true,
mode: 1, // 0 = generic, 1 = text, 2 = font (WOFF2)
quality: 10, // 0 - 11,
lgwin: 12 // default
};
fs.readdirSync('dist/').forEach(file => {
if (file.endsWith('.js') || file.endsWith('.css') || file.endsWith('.html')) {
const result = compress(fs.readFileSync('dist/' + file), brotliSettings);
fs.writeFileSync('dist/' + file + '.br', result);
}
});