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');
|
2016-11-02 14:57:14 -07:00
|
|
|
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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Meta) {
|
2014-10-08 16:53:22 -04:00
|
|
|
Meta.css = {};
|
2017-01-15 12:38:16 -07:00
|
|
|
|
|
|
|
|
var buildImports = {
|
|
|
|
|
client: function (source) {
|
|
|
|
|
return '@import "./theme";\n' + source + '\n' + [
|
|
|
|
|
'@import "font-awesome";',
|
2017-02-06 15:52:54 -07:00
|
|
|
'@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";',
|
2017-01-15 12:38:16 -07:00
|
|
|
'@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');
|
2017-01-15 12:38:16 -07:00
|
|
|
},
|
|
|
|
|
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');
|
2017-01-15 12:38:16 -07:00
|
|
|
},
|
|
|
|
|
};
|
2014-07-04 18:20:40 -04:00
|
|
|
|
2016-11-17 12:11:56 -05:00
|
|
|
Meta.css.minify = function (target, callback) {
|
2016-10-13 11:43:39 +02:00
|
|
|
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');
|
2016-10-13 11:43:39 +02:00
|
|
|
db.getObjectFields('config', ['theme:type', 'theme:id'], function (err, themeData) {
|
2015-03-20 19:36:18 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 12:38:16 -07:00
|
|
|
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'),
|
2017-01-15 12:38:16 -07:00
|
|
|
];
|
|
|
|
|
var source = '';
|
2015-03-20 19:36:18 -04:00
|
|
|
|
2017-01-15 12:38:16 -07:00
|
|
|
var lessFiles = filterMissingFiles(plugins.lessFiles);
|
|
|
|
|
var cssFiles = filterMissingFiles(plugins.cssFiles);
|
2015-03-20 19:36:18 -04:00
|
|
|
|
|
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-01-15 12:38:16 -07:00
|
|
|
getStyleSource(cssFiles, '\n@import (inline) ".', '.css', next);
|
2015-03-20 19:36:18 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (src, next) {
|
2015-03-20 19:36:18 -04:00
|
|
|
source += src;
|
2017-01-15 12:38:16 -07:00
|
|
|
getStyleSource(lessFiles, '\n@import ".', '.less', next);
|
2015-03-20 19:36:18 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (src, next) {
|
2015-03-20 19:36:18 -04:00
|
|
|
source += src;
|
|
|
|
|
next();
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02: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
|
|
|
|
2017-01-15 12:38:16 -07: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
|
|
|
|
2016-10-13 11:43:39 +02: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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
async.each(pluginDirectories, function (directory, next) {
|
2017-04-08 20:22:21 -06:00
|
|
|
file.walk(directory, function (err, styleFiles) {
|
2015-03-20 19:36:18 -04:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
styleFiles.forEach(function (styleFile) {
|
2015-03-20 19:36:18 -04:00
|
|
|
source += prefix + path.sep + styleFile + '";';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err) {
|
2015-03-20 19:36:18 -04:00
|
|
|
callback(err, source);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 12:38:16 -07:00
|
|
|
Meta.css.commitToFile = function (target, source, callback) {
|
|
|
|
|
var filename = (target === 'client' ? 'stylesheet' : 'admin') + '.css';
|
2014-10-08 15:05:02 -04:00
|
|
|
|
2017-01-15 12:38:16 -07:00
|
|
|
fs.writeFile(path.join(__dirname, '../../build/public/' + filename), source, function (err) {
|
2014-09-28 18:33:27 -04:00
|
|
|
if (!err) {
|
2017-01-15 12:38:16 -07:00
|
|
|
winston.verbose('[meta/css] ' + target + ' CSS committed to disk.');
|
2014-09-28 18:33:27 -04:00
|
|
|
} else {
|
|
|
|
|
winston.error('[meta/css] ' + err.message);
|
2017-01-15 12:38:16 -07:00
|
|
|
process.exit(1);
|
2014-09-28 18:33:27 -04:00
|
|
|
}
|
2016-03-30 14:37:00 -04:00
|
|
|
|
|
|
|
|
callback();
|
2014-09-28 18:33:27 -04:00
|
|
|
});
|
2014-10-04 18:56:33 -04:00
|
|
|
};
|
2014-09-28 18:33:27 -04:00
|
|
|
|
2017-01-15 12:38:16 -07: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,
|
2016-10-13 11:43:39 +02:00
|
|
|
}, 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
|
|
|
|
2017-02-18 00:04:34 -07: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());
|
|
|
|
|
});
|
|
|
|
|
|
2017-01-15 12:38:16 -07:00
|
|
|
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) {
|
2016-10-13 11:43:39 +02:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-02 14:57:14 -07:00
|
|
|
};
|