From f3a78c2b6f7a1e35f422cfde620415f061aec0b5 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 3 Apr 2014 15:35:59 -0400 Subject: [PATCH 1/2] #1328 - first pass --- minifier.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ src/meta.js | 29 ------------------------ src/webserver.js | 22 +++++++++++++----- 3 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 minifier.js diff --git a/minifier.js b/minifier.js new file mode 100644 index 0000000000..f6ade8b9d8 --- /dev/null +++ b/minifier.js @@ -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) { + // winston.info('[meta/js] Minifying client-side libraries...'); + var minified = uglifyjs.minify(scripts); + + callback(minified.code); + + // winston.info('[meta/js] Done.'); +}; + +Minifier.js.concatenate = function(scripts, callback) { + // winston.info('[meta/js] Concatenating client-side libraries into one file...'); + + async.map(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); + console.log('ERROR'); + process.exit(); + } + + callback(contents.reduce(function(output, src) { + return output.length ? output + ';\n' + src : src; + }, '')); + + // winston.info('[meta/js] Done.'); + }); +}; + +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; + } +}) \ No newline at end of file diff --git a/src/meta.js b/src/meta.js index b3382025f7..37fbc18e15 100644 --- a/src/meta.js +++ b/src/meta.js @@ -292,35 +292,6 @@ var fs = require('fs'), Meta.js.prepared = true; 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); - process.exit(); - } - - Meta.js.cache = contents.reduce(function(output, src) { - return output.length ? output + ';\n' + src : src; - }, ''); - - winston.info('[meta/js] Done.'); - }); } }; diff --git a/src/webserver.js b/src/webserver.js index a2410e4e15..2b7b84f119 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -7,6 +7,7 @@ var path = require('path'), server, winston = require('winston'), async = require('async'), + fork = require('child_process').fork, emailer = require('./emailer'), db = require('./database'), @@ -91,14 +92,25 @@ if(nconf.get('ssl')) { plugins.ready(function() { // Prepare js for minification/concatenation - meta.js.prepare(function() { - if (app.enabled('minification')) { - meta.js.minify(); - } else { - meta.js.concatenate(); + 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(); }); From 7a993eb2ca4e7f2c1bf10c7b749a34595e3bdb3b Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 3 Apr 2014 17:27:26 -0400 Subject: [PATCH 2/2] closed #1328 --- minifier.js | 34 +++++++++++++++++----------------- public/src/widgets.js | 4 ++-- src/meta.js | 25 +++++++++++++++++++++++++ src/webserver.js | 24 ++---------------------- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/minifier.js b/minifier.js index f6ade8b9d8..cddcd92b51 100644 --- a/minifier.js +++ b/minifier.js @@ -10,31 +10,31 @@ var uglifyjs = require('uglify-js'), /* Javascript */ Minifier.js.minify = function (scripts, callback) { - // winston.info('[meta/js] Minifying client-side libraries...'); - var minified = uglifyjs.minify(scripts); - - callback(minified.code); - - // winston.info('[meta/js] Done.'); + try { + var minified = uglifyjs.minify(scripts); + callback(minified.code); + } catch(err) { + process.send({ + action: 'error', + error: err + }); + } }; Minifier.js.concatenate = function(scripts, callback) { - // winston.info('[meta/js] Concatenating client-side libraries into one file...'); - async.map(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); - console.log('ERROR'); - process.exit(); + process.send({ + action: 'error', + error: err + }); + } else { + callback(contents.reduce(function(output, src) { + return output.length ? output + ';\n' + src : src; + }, '')); } - - callback(contents.reduce(function(output, src) { - return output.length ? output + ';\n' + src : src; - }, '')); - - // winston.info('[meta/js] Done.'); }); }; diff --git a/public/src/widgets.js b/public/src/widgets.js index c82a0cd829..bc5036cb7b 100644 --- a/public/src/widgets.js +++ b/public/src/widgets.js @@ -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')); } }); }; diff --git a/src/meta.js b/src/meta.js index 37fbc18e15..add3bddb31 100644 --- a/src/meta.js +++ b/src/meta.js @@ -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'), @@ -292,6 +293,30 @@ var fs = require('fs'), Meta.js.prepared = true; 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 + }); + }); } }; diff --git a/src/webserver.js b/src/webserver.js index 2b7b84f119..6dacf88c22 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -7,7 +7,6 @@ var path = require('path'), server, winston = require('winston'), async = require('async'), - fork = require('child_process').fork, emailer = require('./emailer'), 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'); } + // Front-end assets plugins.ready(function() { - // Prepare js for minification/concatenation - 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.js.minify(app.enabled('minification')); meta.css.minify(); });