You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/minifier.js

81 lines
1.7 KiB
JavaScript

"use strict";
9 years ago
var uglifyjs = require('uglify-js');
var async = require('async');
var fs = require('fs');
var file = require('./src/file');
9 years ago
9 years ago
var Minifier = {
js: {}
};
/* Javascript */
10 years ago
Minifier.js.minify = function (scripts, minify, callback) {
9 years ago
scripts = scripts.filter(function(file) {
9 years ago
return file && file.endsWith('.js');
});
9 years ago
async.filter(scripts, function(script, next) {
file.exists(script, function(exists) {
if (!exists) {
console.warn('[minifier] file not found, ' + script);
}
next(exists);
});
}, function(scripts) {
9 years ago
if (minify) {
minifyScripts(scripts, callback);
} else {
concatenateScripts(scripts, callback);
}
});
};
process.on('message', function(payload) {
switch(payload.action) {
case 'js':
Minifier.js.minify(payload.scripts, payload.minify, function(minified/*, sourceMap*/) {
process.send({
type: 'end',
// sourceMap: sourceMap,
10 years ago
minified: minified
});
});
break;
}
});
10 years ago
function minifyScripts(scripts, callback) {
// The portions of code involving the source map are commented out as they're broken in UglifyJS2
// Follow along here: https://github.com/mishoo/UglifyJS2/issues/700
11 years ago
try {
10 years ago
var minified = uglifyjs.minify(scripts, {
// outSourceMap: "nodebb.min.js.map",
10 years ago
compress: false
9 years ago
});
11 years ago
callback(minified.code/*, minified.map*/);
11 years ago
} catch(err) {
11 years ago
process.send({
type: 'error',
9 years ago
message: err.message
11 years ago
});
11 years ago
}
}
10 years ago
function concatenateScripts(scripts, callback) {
async.map(scripts, fs.readFile, function(err, scripts) {
if (err) {
11 years ago
process.send({
type: 'error',
9 years ago
message: err.message
11 years ago
});
return;
}
10 years ago
scripts = scripts.join(require('os').EOL + ';');
10 years ago
callback(scripts);
});
}