diff --git a/minifier.js b/minifier.js index 3bd306a07c..16ba513df9 100644 --- a/minifier.js +++ b/minifier.js @@ -34,43 +34,19 @@ Minifier.js.minify = function (scripts, minify, callback) { map: minified.map }); } catch(err) { - process.send({ - action: 'error', - error: { - message: err.message - } - }); + process.send(err.message); } }; -// 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': - Minifier.js.minify(payload.scripts, payload.minify, executeCallback); + Minifier.js.minify(payload.scripts, payload.minify, function(data) { + process.stdout.write(data.js); + process.send('end.script'); + process.stderr.write(data.map); + process.send('end.mapping'); + }); break; } -}); +}); \ No newline at end of file diff --git a/src/meta/js.js b/src/meta/js.js index 3476f891da..08cad37484 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -14,7 +14,8 @@ var winston = require('winston'), module.exports = function(Meta) { Meta.js = { - cache: undefined, + cache: '', + map: '', prepared: false, minFile: 'nodebb.min.js', scripts: [ @@ -136,20 +137,45 @@ module.exports = function(Meta) { }; Meta.js.minify = function(minify) { - var minifier = Meta.js.minifierProc = fork('minifier.js'); + var minifier = Meta.js.minifierProc = fork('minifier.js', { + silent: true + }), + minifiedStream = minifier.stdio[1], + mapStream = minifier.stdio[2], + step = 0, + onComplete = function() { + if (step === 0) { + return step++; + } - minifier.on('message', function(payload) { - if (payload.action !== 'error') { winston.info('[meta/js] Compilation complete'); - Meta.js.cache = payload.data.js; - Meta.js.map = payload.data.map; + emitter.emit('meta:js.compiled'); minifier.kill(); + }; - emitter.emit('meta:js.compiled'); - } else { - winston.error('[meta/js] Could not compile client-side scripts! ' + payload.error.message); + minifiedStream.on('data', function(buffer) { + Meta.js.cache += buffer.toString(); + }); + mapStream.on('data', function(buffer) { + Meta.js.map += buffer.toString(); + }); + + minifier.on('message', function(payload) { + switch(payload) { + case 'end.script': + winston.info('[meta/js] Successfully minified.'); + onComplete(); + break; + case 'end.mapping': + winston.info('[meta/js] Retrieved Mapping.'); + onComplete(); + break; + + default: + winston.error('[meta/js] Could not compile client-side scripts! ' + payload); minifier.kill(); process.exit(); + break; } }); @@ -169,11 +195,4 @@ module.exports = function(Meta) { Meta.js.minifierProc.kill('SIGTERM'); } }; - - // OS detection and handling - // if (os.platform() === 'win32') { - // Meta.js.scripts = Meta.js.scripts.map(function(script) { - // return script.replace(/\//g, '\\'); - // }); - // } }; \ No newline at end of file