-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.js
31 lines (29 loc) · 1002 Bytes
/
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
const brotli = require('brotli');
const fs = require('fs');
const zlib = require('zlib');
const brotliSettings = {
extension: 'br',
skipLarger: true,
mode: 1,
quality: 10,
lgwin: 12
}
let dirs = ['src', 'public']
dirs.forEach(dir => {
fs.readdirSync(dir).forEach(file => {
if (file.endsWith('.js') || file.endsWith('.css') || file.endsWith('html')) {
// brotli
const result = brotli.compress(fs.readFileSync(dir + '/' + file), brotliSettings);
fs.writeFileSync(dir + '/' + file + '.br', result);
// gzip
const fileContents = fs.createReadStream(dir + '/' + file);
const writeStream = fs.createWriteStream(dir + '/' + file + '.gz');
const zip = zlib.createGzip();
fileContents
.pipe(zip)
.on('error', err => console.error(err))
.pipe(writeStream)
.on('error', err => console.error(err))
}
})
})