Merge branch 'spawn-minifier'

v1.18.x
Julian Lam 11 years ago
commit b2c663925b

@ -0,0 +1,58 @@
var uglifyjs = require('uglify-js'),
less = require('less'),
async = require('async'),
fs = require('fs'),
Minifier = {
js: {},
css: {}
};
/* Javascript */
Minifier.js.minify = function (scripts, callback) {
try {
var minified = uglifyjs.minify(scripts);
callback(minified.code);
} catch(err) {
process.send({
action: 'error',
error: err
});
}
};
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.minify':
Minifier.js.minify(payload.scripts, executeCallback);
break;
case 'js.concatenate':
Minifier.js.concatenate(payload.scripts, executeCallback);
break;
}
})

@ -3,13 +3,13 @@
(function(ajaxify) {
ajaxify.widgets = {};
ajaxify.widgets.reposition = function(location) {
$('body [no-widget-class]').each(function() {
var $this = $(this);
if ($this.attr('no-widget-target') === location) {
$this.removeClass();
$this.addClass($this.attr('no-widget-class'));
$this.addClass($this.attr('no-widget-class'));
}
});
};

@ -7,6 +7,7 @@ var fs = require('fs'),
nconf = require('nconf'),
_ = require('underscore'),
less = require('less'),
fork = require('child_process').fork,
utils = require('./../public/src/utils'),
translator = require('./../public/src/translator'),
@ -293,33 +294,28 @@ var fs = require('fs'),
callback();
});
},
minify: function () {
var uglifyjs = require('uglify-js'),
minified;
winston.info('[meta/js] Minifying client-side libraries...');
minified = uglifyjs.minify(this.scripts);
this.cache = minified.code;
winston.info('[meta/js] Done.');
},
concatenate: function() {
winston.info('[meta/js] Concatenating client-side libraries into one file...');
async.map(this.scripts, function(path, next) {
fs.readFile(path, { encoding: 'utf-8' }, next);
}, function(err, contents) {
if (err) {
winston.error('[meta/js] Could not minify javascript! Error: ' + err.message);
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();
}
});
Meta.js.cache = contents.reduce(function(output, src) {
return output.length ? output + ';\n' + src : src;
}, '');
winston.info('[meta/js] Done.');
this.prepare(function() {
minifier.send({
action: minify ? 'js.minify' : 'js.concatenate',
scripts: Meta.js.scripts
});
});
}
};

@ -89,17 +89,9 @@ if(nconf.get('ssl')) {
winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md');
}
// Front-end assets
plugins.ready(function() {
// Prepare js for minification/concatenation
meta.js.prepare(function() {
if (app.enabled('minification')) {
meta.js.minify();
} else {
meta.js.concatenate();
}
});
// Minify CSS
meta.js.minify(app.enabled('minification'));
meta.css.minify();
});

Loading…
Cancel
Save