From 53f1e4d3d41816fa02e84e7928209869abdbea55 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 25 Feb 2014 14:13:09 -0500 Subject: [PATCH 1/2] dedicated stylesheet.css route for LESS compilation, no longer usin less-middleware for base theme... --- package.json | 3 ++- public/templates/header.tpl | 2 +- src/plugins.js | 15 ++++++++++++ src/routes/theme.js | 47 +++++++++++++++++++++++++++++++++++++ src/webserver.js | 21 +++++++---------- 5 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/routes/theme.js diff --git a/package.json b/package.json index f35c875dc3..7fff110bd6 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "nodebb-widget-essentials": "~0.0", "nodebb-theme-vanilla": "~0.0.14", "nodebb-theme-cerulean": "~0.0.13", - "nodebb-theme-lavender": "~0.0.21" + "nodebb-theme-lavender": "~0.0.21", + "less": "^1.6.3" }, "optionalDependencies": { "redis": "0.8.3", diff --git a/public/templates/header.tpl b/public/templates/header.tpl index 5e4c0e2d4a..4a5aa70794 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -6,7 +6,7 @@ name="{metaTags.name}" property="{metaTags.property}" content="{metaTags.content}" /> - + link="{linkTags.link}" rel="{linkTags.rel}" type="{linkTags.type}" href="{linkTags.href}" /> diff --git a/src/plugins.js b/src/plugins.js index 8da34ebfe2..5dad12632a 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -16,6 +16,7 @@ var fs = require('fs'), Plugins.loadedHooks = {}; Plugins.staticDirs = {}; Plugins.cssFiles = []; + Plugins.lessFiles = []; Plugins.initialized = false; @@ -221,6 +222,20 @@ var fs = require('fs'), } else { next(); } + }, + function(next) { + // LESS files for plugins + if (pluginData.less && pluginData.less instanceof Array) { + if (global.env === 'development') { + winston.info('[plugins] Found ' + pluginData.less.length + ' LESS file(s) for plugin ' + pluginData.id); + } + + Plugins.lessFiles = Plugins.lessFiles.concat(pluginData.less.map(function(file) { + return path.join(pluginData.id, file); + })); + } + + next(); } ], function(err) { if (!err) { diff --git a/src/routes/theme.js b/src/routes/theme.js new file mode 100644 index 0000000000..f4e260352f --- /dev/null +++ b/src/routes/theme.js @@ -0,0 +1,47 @@ +var path = require('path'), + nconf = require('nconf'), + less = require('less'), + + meta = require('../meta'), + db = require('../database'), + plugins = require('../plugins'); + +(function (Meta) { + Meta.createRoutes = function(app) { + app.get('/stylesheet.css', function(req, res) { + db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) { + var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'), + baseThemePath = path.join(nconf.get('themes_path'), themeId), + paths = [baseThemePath, path.join(__dirname, '../../node_modules')], + source = '@import "./theme";', + x, numLESS; + + // Add the imports for each LESS file + for(x=0,numLESS=plugins.lessFiles.length;x Date: Tue, 25 Feb 2014 15:20:21 -0500 Subject: [PATCH 2/2] caching compiled CSS locally, so repeated calls to the stylesheet serve from cache --- src/meta.js | 4 ++++ src/routes/theme.js | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/meta.js b/src/meta.js index 419f293648..e494c4fccc 100644 --- a/src/meta.js +++ b/src/meta.js @@ -337,6 +337,10 @@ var fs = require('fs'), } }; + Meta.css = { + cache: undefined + }; + Meta.restart = function() { if (process.send) { process.send('nodebb:restart'); diff --git a/src/routes/theme.js b/src/routes/theme.js index f4e260352f..e8a9052029 100644 --- a/src/routes/theme.js +++ b/src/routes/theme.js @@ -9,6 +9,11 @@ var path = require('path'), (function (Meta) { Meta.createRoutes = function(app) { app.get('/stylesheet.css', function(req, res) { + if (meta.css.cache) { + res.type('text/css').send(200, meta.css.cache); + return; + } + db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) { var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'), baseThemePath = path.join(nconf.get('themes_path'), themeId), @@ -31,11 +36,11 @@ var path = require('path'), parser.parse(source, function(err, tree) { if (err) { res.send(500, err.message); - console.log(err); return; } - res.type('text/css').send(200, tree.toCSS()); + meta.css.cache = tree.toCSS(); + res.type('text/css').send(200, meta.css.cache); }); } else { // Bootswatch theme not supported yet