Newer
Older
Lloyd Hilaiel
committed
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const
fs = require('fs'),
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify,
uglifycss = require('uglifycss'),
mkdirp = require('mkdirp'),
path = require('path');
function compressResource(staticPath, name, files, cb) {
var orig_code = "";
var info = undefined;
function writeFile(final_code) {
mkdirp(path.join(staticPath, path.dirname(name)), function (err) {
if (err) cb(err);
else {
fs.writeFile(path.join(staticPath, name), final_code, function(err) {
cb(err, info);
});
};
});
}
function compress() {
var final_code;
if (/\.js$/.test(name)) {
// compress javascript
var ast = jsp.parse(orig_code); // parse code and get the initial AST
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
final_code = pro.split_lines(pro.gen_code(ast), 32 * 1024); // compressed code here
} else if (/\.css$/.test(name)) {
// compress css
final_code = uglifycss.processString(orig_code);
} else {
return cb("can't determine content type: " + name);
}
writeFile(final_code);
}
function readNext() {
if (files.length) {
var f = files.shift();
fs.readFile(path.join(staticPath, f), function(err, data) {
if (err) cb(err);
else {
orig_code += data;
readNext();
}
});
} else {
compress();
}
}
function isBuildNeeded() {
// we'll check mtime on all files. if any is newer than the output file,
// build is needed
try {
var lastGen = fs.statSync(path.join(staticPath, name)).mtime;
for (var i = 0; i < files.length; i++) {
if (lastGen < fs.statSync(path.join(staticPath, files[i])).mtime) {
info = "rebuilt because " + files[i] + " was changed";
throw "newer";
}
};
// no rebuild needed
cb(null, "up to date");
} catch (e) {
readNext();
}
}
isBuildNeeded();
}
process.on('message', function(m) {
var startTime = new Date;
compressResource(m.staticPath, m.file, m.deps, function(err, info) {
if (err) process.send({ error: err });
else process.send({
time: ((new Date - startTime) / 1000.0).toFixed(2),
info: info
});
});
});