Files
NodeBB/minifier.js

69 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-05-29 19:44:30 -04:00
"use strict";
2014-04-03 15:35:59 -04:00
var uglifyjs = require('uglify-js'),
less = require('less'),
async = require('async'),
fs = require('fs'),
path = require('path'),
2014-07-28 15:25:04 -04:00
crypto = require('crypto'),
2014-04-03 15:35:59 -04:00
Minifier = {
js: {},
css: {}
};
/* Javascript */
2014-08-27 14:42:10 -04:00
Minifier.js.minify = function (scripts, relativePath, minify, callback) {
var options = {
compress: false
};
2014-05-29 19:44:30 -04:00
scripts = scripts.filter(function(file) {
return fs.existsSync(file);
});
2014-05-29 19:44:30 -04:00
if (!minify) {
options.sourceMapURL = '/nodebb.min.js.map';
2014-05-29 19:44:30 -04:00
options.outSourceMap = 'nodebb.min.js.map';
2014-08-27 14:42:10 -04:00
options.sourceRoot = relativePath;
2014-05-29 19:44:30 -04:00
options.mangle = false;
2014-07-23 14:27:01 -04:00
options.prefix = 1;
2014-05-29 19:44:30 -04:00
}
2014-04-03 17:27:26 -04:00
try {
2014-07-28 15:25:04 -04:00
var minified = uglifyjs.minify(scripts, options),
hasher = crypto.createHash('md5'),
hash;
// Calculate js hash
hasher.update(minified.code, 'utf-8');
hash = hasher.digest('hex');
process.send({
type: 'hash',
payload: hash.slice(0, 8)
});
2014-05-29 19:44:30 -04:00
callback({
js: minified.code,
map: minified.map
});
2014-04-03 17:27:26 -04:00
} catch(err) {
2014-07-28 15:25:04 -04:00
process.send({
type: 'error',
payload: err
});
2014-04-03 17:27:26 -04:00
}
2014-04-03 15:35:59 -04:00
};
process.on('message', function(payload) {
switch(payload.action) {
2014-05-29 19:44:30 -04:00
case 'js':
2014-08-27 14:42:10 -04:00
Minifier.js.minify(payload.scripts, payload.relativePath, payload.minify, function(data) {
2014-07-28 15:25:04 -04:00
process.send({
type: 'end',
data: data
2014-07-28 15:25:04 -04:00
});
2014-07-28 14:39:56 -04:00
});
2014-04-03 15:35:59 -04:00
break;
}
2014-07-28 14:39:56 -04:00
});