Files
NodeBB/src/meta/css.js

176 lines
5.0 KiB
JavaScript
Raw Normal View History

2014-07-04 18:20:40 -04:00
'use strict';
2016-10-04 20:10:59 +03:00
var winston = require('winston');
var nconf = require('nconf');
var fs = require('fs');
var path = require('path');
var less = require('less');
var async = require('async');
var autoprefixer = require('autoprefixer');
var postcss = require('postcss');
var clean = require('postcss-clean');
2014-07-04 18:20:40 -04:00
2016-10-04 20:10:59 +03:00
var plugins = require('../plugins');
var db = require('../database');
var file = require('../file');
2014-07-04 18:20:40 -04:00
module.exports = function (Meta) {
2014-10-08 16:53:22 -04:00
Meta.css = {};
var buildImports = {
client: function (source) {
return '@import "./theme";\n' + source + '\n' + [
'@import "font-awesome";',
'@import (inline) "../public/vendor/jquery/css/smoothness/jquery-ui.css";',
'@import (inline) "../public/vendor/jquery/bootstrap-tagsinput/bootstrap-tagsinput.css";',
'@import (inline) "../public/vendor/colorpicker/colorpicker.css";',
2017-02-06 15:54:21 -07:00
'@import (inline) "../node_modules/cropperjs/dist/cropper.css";',
'@import "../../public/less/flags.less";',
'@import "../../public/less/blacklist.less";',
'@import "../../public/less/generics.less";',
'@import "../../public/less/mixins.less";',
'@import "../../public/less/global.less";',
2017-02-23 20:45:05 +00:00
].map(function (str) {
return str.replace(/\//g, path.sep);
}).join('\n');
},
admin: function (source) {
return source + '\n' + [
'@import "font-awesome";',
'@import "../public/less/admin/admin";',
'@import "../public/less/generics.less";',
'@import (inline) "../public/vendor/colorpicker/colorpicker.css";',
'@import (inline) "../public/vendor/jquery/css/smoothness/jquery-ui.css";',
'@import (inline) "../public/vendor/jquery/bootstrap-tagsinput/bootstrap-tagsinput.css";',
2017-02-23 20:45:05 +00:00
'@import (inline) "../public/vendor/mdl/material.css";',
].map(function (str) {
return str.replace(/\//g, path.sep);
}).join('\n');
},
};
2014-07-04 18:20:40 -04:00
Meta.css.minify = function (target, callback) {
callback = callback || function () {};
2014-07-04 18:20:40 -04:00
2015-03-20 19:36:18 -04:00
winston.verbose('[meta/css] Minifying LESS/CSS');
db.getObjectFields('config', ['theme:type', 'theme:id'], function (err, themeData) {
2015-03-20 19:36:18 -04:00
if (err) {
return callback(err);
}
var themeId = (themeData['theme:id'] || 'nodebb-theme-persona');
var baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla'));
var paths = [
baseThemePath,
path.join(__dirname, '../../node_modules'),
2017-02-17 19:31:21 -07:00
path.join(__dirname, '../../public/vendor/fontawesome/less'),
];
var source = '';
2015-03-20 19:36:18 -04:00
var lessFiles = filterMissingFiles(plugins.lessFiles);
var cssFiles = filterMissingFiles(plugins.cssFiles);
2015-03-20 19:36:18 -04:00
async.waterfall([
function (next) {
getStyleSource(cssFiles, '\n@import (inline) ".', '.css', next);
2015-03-20 19:36:18 -04:00
},
function (src, next) {
2015-03-20 19:36:18 -04:00
source += src;
getStyleSource(lessFiles, '\n@import ".', '.less', next);
2015-03-20 19:36:18 -04:00
},
function (src, next) {
2015-03-20 19:36:18 -04:00
source += src;
next();
2017-02-17 19:31:21 -07:00
},
], function (err) {
2015-03-20 19:36:18 -04:00
if (err) {
return callback(err);
2014-07-15 16:32:51 -04:00
}
2014-09-29 19:31:27 -04:00
minify(buildImports[target](source), paths, target, callback);
2014-09-29 19:31:27 -04:00
});
2015-03-20 19:36:18 -04:00
});
2014-09-24 18:36:30 -04:00
};
2014-07-04 18:20:40 -04:00
2015-03-20 19:36:18 -04:00
function getStyleSource(files, prefix, extension, callback) {
2017-02-17 20:20:42 -07:00
var pluginDirectories = [];
var source = '';
2015-03-20 19:36:18 -04:00
files.forEach(function (styleFile) {
2015-03-20 19:36:18 -04:00
if (styleFile.endsWith(extension)) {
source += prefix + path.sep + styleFile + '";';
} else {
pluginDirectories.push(styleFile);
}
});
async.each(pluginDirectories, function (directory, next) {
file.walk(directory, function (err, styleFiles) {
2015-03-20 19:36:18 -04:00
if (err) {
return next(err);
}
styleFiles.forEach(function (styleFile) {
2015-03-20 19:36:18 -04:00
source += prefix + path.sep + styleFile + '";';
});
next();
});
}, function (err) {
2015-03-20 19:36:18 -04:00
callback(err, source);
});
}
Meta.css.commitToFile = function (target, source, callback) {
var filename = (target === 'client' ? 'stylesheet' : 'admin') + '.css';
fs.writeFile(path.join(__dirname, '../../build/public/' + filename), source, function (err) {
if (!err) {
winston.verbose('[meta/css] ' + target + ' CSS committed to disk.');
} else {
winston.error('[meta/css] ' + err.message);
process.exit(1);
}
callback();
});
2014-10-04 18:56:33 -04:00
};
function minify(source, paths, target, callback) {
2016-12-13 15:43:20 +03:00
callback = callback || function () {};
2014-11-17 11:24:46 -05:00
less.render(source, {
2017-02-17 19:31:21 -07:00
paths: paths,
}, function (err, lessOutput) {
2014-09-24 18:36:30 -04:00
if (err) {
winston.error('[meta/css] Could not minify LESS/CSS: ' + err.message);
2016-12-13 15:43:20 +03:00
return callback(err);
2014-09-24 18:36:30 -04:00
}
2014-07-04 18:20:40 -04:00
postcss(global.env === 'development' ? [autoprefixer] : [
2017-02-16 13:33:28 -07:00
autoprefixer,
clean({
2017-02-17 19:31:21 -07:00
processImportFrom: ['local'],
2017-02-16 13:33:28 -07:00
}),
]).process(lessOutput.css).then(function (result) {
2015-12-31 12:15:31 -05:00
result.warnings().forEach(function (warn) {
winston.verbose(warn.toString());
});
return Meta.css.commitToFile(target, result.css, function () {
callback(null, result.css);
});
2015-12-31 12:15:31 -05:00
});
2014-07-04 18:20:40 -04:00
});
2014-09-24 18:36:30 -04:00
}
2014-07-04 18:20:40 -04:00
function filterMissingFiles(files) {
return files.filter(function (filePath) {
2015-09-29 18:22:41 -04:00
var exists = file.existsSync(path.join(__dirname, '../../node_modules', filePath));
2014-07-04 18:20:40 -04:00
if (!exists) {
2015-09-29 18:22:41 -04:00
winston.warn('[meta/css] File not found! ' + filePath);
2014-07-04 18:20:40 -04:00
}
return exists;
});
}
};