v1.18.x
Julian Lam 11 years ago
parent f3a78c2b6f
commit 7a993eb2ca

@ -10,31 +10,31 @@ var uglifyjs = require('uglify-js'),
/* Javascript */ /* Javascript */
Minifier.js.minify = function (scripts, callback) { Minifier.js.minify = function (scripts, callback) {
// winston.info('[meta/js] Minifying client-side libraries...'); try {
var minified = uglifyjs.minify(scripts); var minified = uglifyjs.minify(scripts);
callback(minified.code); callback(minified.code);
} catch(err) {
// winston.info('[meta/js] Done.'); process.send({
action: 'error',
error: err
});
}
}; };
Minifier.js.concatenate = function(scripts, callback) { Minifier.js.concatenate = function(scripts, callback) {
// winston.info('[meta/js] Concatenating client-side libraries into one file...');
async.map(scripts, function(path, next) { async.map(scripts, function(path, next) {
fs.readFile(path, { encoding: 'utf-8' }, next); fs.readFile(path, { encoding: 'utf-8' }, next);
}, function(err, contents) { }, function(err, contents) {
if (err) { if (err) {
// winston.error('[meta/js] Could not minify javascript! Error: ' + err.message); process.send({
console.log('ERROR'); action: 'error',
process.exit(); error: err
} });
} else {
callback(contents.reduce(function(output, src) { callback(contents.reduce(function(output, src) {
return output.length ? output + ';\n' + src : src; return output.length ? output + ';\n' + src : src;
}, '')); }, ''));
}
// winston.info('[meta/js] Done.');
}); });
}; };

@ -7,6 +7,7 @@ var fs = require('fs'),
nconf = require('nconf'), nconf = require('nconf'),
_ = require('underscore'), _ = require('underscore'),
less = require('less'), less = require('less'),
fork = require('child_process').fork,
utils = require('./../public/src/utils'), utils = require('./../public/src/utils'),
translator = require('./../public/src/translator'), translator = require('./../public/src/translator'),
@ -292,6 +293,30 @@ var fs = require('fs'),
Meta.js.prepared = true; Meta.js.prepared = true;
callback(); callback();
}); });
},
minify: function(minify) {
// Prepare js for minification/concatenation
var minifier = fork('minifier.js');
minifier.on('message', function(payload) {
if (payload.action !== 'error') {
winston.info('[meta/js] Compilation complete');
Meta.js.cache = payload.data;
minifier.kill();
} else {
winston.error('[meta/js] Could not compile client-side scripts!');
winston.error('[meta/js] ' + payload.error.message);
minifier.kill();
process.exit();
}
});
this.prepare(function() {
minifier.send({
action: minify ? 'js.minify' : 'js.concatenate',
scripts: Meta.js.scripts
});
});
} }
}; };

@ -7,7 +7,6 @@ var path = require('path'),
server, server,
winston = require('winston'), winston = require('winston'),
async = require('async'), async = require('async'),
fork = require('child_process').fork,
emailer = require('./emailer'), emailer = require('./emailer'),
db = require('./database'), db = require('./database'),
@ -90,28 +89,9 @@ if(nconf.get('ssl')) {
winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md'); winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md');
} }
// Front-end assets
plugins.ready(function() { plugins.ready(function() {
// Prepare js for minification/concatenation meta.js.minify(app.enabled('minification'));
var minifier = fork('minifier.js');
minifier.on('message', function(payload) {
switch(payload.action) {
case 'js.minify': // Intentional fall-through
case 'js.concatenate':
winston.info('[meta/js] Compilation complete');
meta.js.cache = payload.data;
break;
}
});
meta.js.prepare(function() {
minifier.send({
action: app.enabled('minification') ? 'js.minify' : 'js.concatenate',
scripts: meta.js.scripts
});
});
// Minify CSS
meta.css.minify(); meta.css.minify();
}); });

Loading…
Cancel
Save