forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minifier.js
76 lines (67 loc) · 1.47 KB
/
minifier.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
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
"use strict";
var uglifyjs = require('uglify-js'),
less = require('less'),
async = require('async'),
fs = require('fs'),
path = require('path'),
Minifier = {
js: {},
css: {}
};
/* Javascript */
Minifier.js.minify = function (scripts, minify, callback) {
var options = {};
scripts = scripts.filter(function(file) {
return fs.existsSync(file);
});
if (!minify) {
options.sourceMapURL = '/nodebb.min.js.map';
options.outSourceMap = 'nodebb.min.js.map';
options.mangle = false;
options.compress = false;
options.prefix = 1;
}
try {
var minified = uglifyjs.minify(scripts, options);
callback({
js: minified.code,
map: minified.map
});
} catch(err) {
process.send({
action: 'error',
error: {
message: err.message
}
});
}
};
// Minifier.js.concatenate = function(scripts, callback) {
// async.map(scripts, function(path, next) {
// fs.readFile(path, { encoding: 'utf-8' }, next);
// }, function(err, contents) {
// if (err) {
// process.send({
// action: 'error',
// error: err
// });
// } else {
// callback(contents.reduce(function(output, src) {
// return output.length ? output + ';\n' + src : src;
// }, ''));
// }
// });
// };
process.on('message', function(payload) {
var executeCallback = function(data) {
process.send({
action: payload.action,
data: data
});
};
switch(payload.action) {
case 'js':
Minifier.js.minify(payload.scripts, payload.minify, executeCallback);
break;
}
});