mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
closes #2593
This commit is contained in:
107
src/meta/css.js
107
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;
|
||||
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.lessFiles = filterMissingFiles(plugins.lessFiles);
|
||||
for(x=0; x<plugins.lessFiles.length; ++x) {
|
||||
source += '\n@import ".' + path.sep + plugins.lessFiles[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();
|
||||
}
|
||||
|
||||
plugins.cssFiles = filterMissingFiles(plugins.cssFiles);
|
||||
for(x=0; x<plugins.cssFiles.length; ++x) {
|
||||
source += '\n@import (inline) ".' + path.sep + plugins.cssFiles[x] + '";';
|
||||
], 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';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user