From c2fd89e050d4c28c98debf6c277e8cbc864a818a Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sun, 28 Sep 2014 18:33:27 -0400 Subject: [PATCH] committing of compiled js and css to file on startup --- .gitignore | 8 +++++++- src/meta/css.js | 17 ++++++++++++++++- src/meta/js.js | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 3b012c639e..c7e1d383b1 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,10 @@ pidfile # templates /public/templates -/public/sounds \ No newline at end of file +/public/sounds + +# compiled files +/public/stylesheet.css +/public/admin.css +/public/nodebb.min.js +/public/nodebb.min.js.map diff --git a/src/meta/css.js b/src/meta/css.js index eda07c996e..c8d3853e52 100644 --- a/src/meta/css.js +++ b/src/meta/css.js @@ -63,10 +63,21 @@ module.exports = function(Meta) { minify(acpSource, paths, 'acpCache', next); } ], callback); - }); }; + Meta.css.commitToFile = function(filename) { + winston.info('[meta/css] Committing stylesheet (' + filename + ') to disk'); + fs.writeFile(path.join(__dirname, '../../public/' + (filename === 'acpCache' ? 'admin' : 'stylesheet') + '.css'), Meta.css[filename], function(err) { + if (!err) { + winston.info('[meta/css] Stylesheet (' + filename + ') committed to disk.'); + } else { + winston.error('[meta/css] ' + err.message); + process.exit(0); + } + }); + } + function minify(source, paths, destination, callback) { var parser = new (less.Parser)({ paths: paths @@ -104,6 +115,10 @@ module.exports = function(Meta) { winston.info('[meta/css] Done.'); emitter.emit('meta:css.compiled'); + + // Save the compiled CSS in public/ so things like nginx can serve it + Meta.css.commitToFile(destination); + if (typeof callback === 'function') { callback(); } diff --git a/src/meta/js.js b/src/meta/js.js index 07a9e3c107..57f5b0461b 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -8,6 +8,7 @@ var winston = require('winston'), os = require('os'), nconf = require('nconf'), cluster = require('cluster'), + fs = require('fs'), plugins = require('../plugins'), emitter = require('../emitter'), @@ -148,6 +149,9 @@ module.exports = function(Meta) { cache: minifiedString, map: mapString }); + + // Save the minfile in public/ so things like nginx can serve it + Meta.js.commitToFile(); } if (typeof callback === 'function') { @@ -209,6 +213,21 @@ module.exports = function(Meta) { } }; + Meta.js.commitToFile = function() { + winston.info('[meta/js] Committing minfile to disk'); + async.parallel([ + async.apply(fs.writeFile, path.join(__dirname, '../../public/nodebb.min.js'), Meta.js.cache), + async.apply(fs.writeFile, path.join(__dirname, '../../public/nodebb.min.js.map'), Meta.js.map) + ], function (err) { + if (!err) { + winston.info('[meta/js] Client-side minfile and mapping committed to disk.'); + } else { + winston.error('[meta/js] ' + err.message); + process.exit(0); + } + }); + }; + function getPluginScripts(callback) { plugins.fireHook('filter:scripts.get', [], function(err, scripts) { if (err) callback(err, []);