forked from eoinmcg/buzzoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.min.js
executable file
·46 lines (38 loc) · 1.35 KB
/
node.min.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
43
44
45
46
var fs = require('fs');
var uglify = require("uglify-js");
var zip = require('node-zip');
var cheerio = require('cheerio');
var input = 'dev.html',
output = 'index.html',
zipped = 'min.zip',
files = [];
var index = fs.readFileSync(input, 'utf8');
var $ = cheerio.load(index);
var script;
$('script').each(function() {
script = $(this).attr('src');
files.push(script);
});
var src = uglify.minify(files).code;
console.log('\n');
console.log('- Concated and uglified ' + files.length + ' js files');
// remove all script tags
index = index.replace(/<script[^>]*>.*?<\/script>/gi,'');
// append concatted and uglified as inline script
index = index.replace('</body>', '<script>'+src+'</script></body>');
// index = index.replace(/\s/g, "");
var zip = new require('node-zip')();
zip.file(output, index);
var data = zip.generate({base64:false,compression:'DEFLATE'});
fs.writeFileSync(zipped, data, 'binary');
console.log('- Zipped file: ' + zipped + ' created');
var stats = fs.statSync(zipped);
var bytes = stats.size;
var limit = 13312;
var remaining = limit - bytes;
var remaining_percent = (remaining / limit) * 100;
remaining_percent = Math.round(remaining_percent * 100) / 100;
console.log('Limit: ' + limit);
console.log('Bytes used: ' + bytes);
console.log('Bytes Remaining: ' + remaining);
console.log('Remaining: ' + remaining_percent + '%');