Skip to content

Node.js based forum software built for the modern web

closes #2593

barisusakli810d3be5126f
diff --git a/public/src/utils.js b/public/src/utils.js
index 0c58817411..b48b4345ae 100644
--- a/public/src/utils.js
+++ b/public/src/utils.js
@@ -365,6 +365,18 @@
}
return i < 0;
};
+
+ String.prototype.endsWith = function(suffix) {
+ if (this.length < suffix.length) {
+ return false;
+ }
+ var len = this.length;
+ var suffixLen = suffix.length;
+ for (var i=1; (i <= suffixLen && this[len - i] === suffix[suffixLen - i]); ++i) {
+ continue;
+ }
+ return i > suffixLen;
+ };
}
if ('undefined' !== typeof window) {
diff --git a/src/meta/css.js b/src/meta/css.js
index d45b61f280..a8682b1038 100644
--- a/src/meta/css.js
+++ b/src/meta/css.js
@@ -10,7 +10,8 @@ var winston = require('winston'),
plugins = require('../plugins'),
emitter = require('../emitter'),
- db = require('../database');
+ db = require('../database'),
+ utils = require('../../public/src/utils');
module.exports = function(Meta) {
@@ -21,30 +22,46 @@ module.exports = function(Meta) {
Meta.css.defaultBranding = {};
Meta.css.minify = function(callback) {
- if (nconf.get('isPrimary') === 'true') {
- winston.verbose('[meta/css] Minifying LESS/CSS');
- 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'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla')),
- paths = [
- baseThemePath,
- path.join(__dirname, '../../node_modules'),
- path.join(__dirname, '../../public/vendor/fontawesome/less'),
- path.join(__dirname, '../../public/vendor/bootstrap/less')
- ],
- source = '@import "font-awesome";',
- acpSource,
- x;
-
-
- plugins.lessFiles = filterMissingFiles(plugins.lessFiles);
- for(x=0; x<plugins.lessFiles.length; ++x) {
- source += '\n@import ".' + path.sep + plugins.lessFiles[x] + '";';
- }
+ callback = callback || function() {};
+ if (nconf.get('isPrimary') !== 'true') {
+ winston.verbose('[meta/css] Cluster worker ' + process.pid + ' skipping LESS/CSS compilation');
+ return callback();
+ }
+
+ winston.verbose('[meta/css] Minifying LESS/CSS');
+ db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) {
+ if (err) {
+ return callback(err);
+ }
- plugins.cssFiles = filterMissingFiles(plugins.cssFiles);
- for(x=0; x<plugins.cssFiles.length; ++x) {
- source += '\n@import (inline) ".' + path.sep + plugins.cssFiles[x] + '";';
+ var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'),
+ baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla')),
+ paths = [
+ baseThemePath,
+ path.join(__dirname, '../../node_modules'),
+ path.join(__dirname, '../../public/vendor/fontawesome/less'),
+ path.join(__dirname, '../../public/vendor/bootstrap/less')
+ ],
+ source = '@import "font-awesome";';
+
+ plugins.lessFiles = filterMissingFiles(plugins.lessFiles);
+ plugins.cssFiles = filterMissingFiles(plugins.cssFiles);
+
+ async.waterfall([
+ function(next) {
+ getStyleSource(plugins.lessFiles, '\n@import ".', '.less', next);
+ },
+ function(src, next) {
+ source += src;
+ getStyleSource(plugins.cssFiles, '\n@import (inline) ".', '.css', next);
+ },
+ function(src, next) {
+ source += src;
+ next();
+ }
+ ], function(err) {
+ if (err) {
+ return callback(err);
}
source += '\n@import (inline) "..' + path.sep + '..' + path.sep + 'public/vendor/jquery/css/smoothness/jquery-ui-1.10.4.custom.min.css";';
@@ -52,7 +69,7 @@ module.exports = function(Meta) {
source += '\n@import (inline) "..' + path.sep + '..' + path.sep + 'public/vendor/jquery/textcomplete/jquery.textcomplete.css";';
source += '\n@import (inline) "..' + path.sep + '..' + path.sep + 'public/vendor/colorpicker/colorpicker.css";';
- acpSource = '\n@import "..' + path.sep + 'public/less/admin/admin";\n' + source;
+ var acpSource = '\n@import "..' + path.sep + 'public/less/admin/admin";\n' + source;
acpSource += '\n@import (inline) "..' + path.sep + 'public/vendor/colorpicker/colorpicker.css";';
source = '@import "./theme";\n' + source;
@@ -65,6 +82,10 @@ module.exports = function(Meta) {
minify(acpSource, paths, 'acpCache', next);
}
], function(err, minified) {
+ if (err) {
+ return callback(err);
+ }
+
// Propagate to other workers
if (process.send) {
process.send({
@@ -77,19 +98,41 @@ module.exports = function(Meta) {
emitter.emit('meta:css.compiled');
- if (typeof callback === 'function') {
- callback();
- }
+ callback();
});
});
- } else {
- winston.verbose('[meta/css] Cluster worker ' + process.pid + ' skipping LESS/CSS compilation');
- if (typeof callback === 'function') {
- callback();
- }
- }
+ });
};
+ function getStyleSource(files, prefix, extension, callback) {
+ var pluginDirectories = [],
+ source = '';
+
+ files.forEach(function(styleFile) {
+ if (styleFile.endsWith(extension)) {
+ source += prefix + path.sep + styleFile + '";';
+ } else {
+ pluginDirectories.push(styleFile);
+ }
+ });
+
+ async.each(pluginDirectories, function(directory, next) {
+ utils.walk(directory, function(err, styleFiles) {
+ if (err) {
+ return next(err);
+ }
+
+ styleFiles.forEach(function(styleFile) {
+ source += prefix + path.sep + styleFile + '";';
+ });
+
+ next();
+ });
+ }, function(err) {
+ callback(err, source);
+ });
+ }
+
Meta.css.commitToFile = function(filename) {
var file = (filename === 'acpCache' ? 'admin' : 'stylesheet') + '.css';
diff --git a/src/meta/js.js b/src/meta/js.js
index 3d8ced839c..85188ab3f7 100644
--- a/src/meta/js.js
+++ b/src/meta/js.js
@@ -94,7 +94,7 @@ module.exports = function(Meta) {
clientScripts = [];
pluginsScripts = plugins.clientScripts.filter(function(path) {
- if (path.indexOf('.js') !== -1) {
+ if (path.endsWith('.js')) {
return true;
} else {
pluginDirectories.push(path);