Files
NodeBB/src/meta/css.js

169 lines
4.8 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 async = require('async');
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');
var minifier = require('./minifier');
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
function filterMissingFiles(filepaths, callback) {
async.filter(filepaths, function (filepath, next) {
file.exists(path.join(__dirname, '../../node_modules', filepath), function (err, exists) {
if (!exists) {
winston.warn('[meta/css] File not found! ' + filepath);
2014-07-15 16:32:51 -04:00
}
2014-09-29 19:31:27 -04:00
next(err, exists);
2014-09-29 19:31:27 -04:00
});
}, callback);
}
2014-07-04 18:20:40 -04:00
function getImports(files, prefix, extension, callback) {
2017-05-27 01:44:26 -04:00
var pluginDirectories = [];
2017-02-17 20:20:42 -07:00
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);
});
}
function getBundleMetadata(target, callback) {
var paths = [
path.join(__dirname, '../../node_modules'),
path.join(__dirname, '../../public/vendor/fontawesome/less'),
];
async.waterfall([
function (next) {
if (target !== 'client') {
return next(null, null);
}
db.getObjectFields('config', ['theme:type', 'theme:id'], next);
},
function (themeData, next) {
if (target === 'client') {
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'));
paths.unshift(baseThemePath);
}
async.parallel({
less: function (cb) {
async.waterfall([
function (next) {
filterMissingFiles(plugins.lessFiles, next);
},
function (lessFiles, next) {
getImports(lessFiles, '\n@import ".', '.less', next);
},
], cb);
},
css: function (cb) {
async.waterfall([
function (next) {
filterMissingFiles(plugins.cssFiles, next);
},
function (cssFiles, next) {
getImports(cssFiles, '\n@import (inline) ".', '.css', next);
},
], cb);
},
}, next);
},
function (result, next) {
var cssImports = result.css;
var lessImports = result.less;
var imports = cssImports + '\n' + lessImports;
imports = buildImports[target](imports);
next(null, imports);
},
], function (err, imports) {
2014-09-24 18:36:30 -04:00
if (err) {
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
callback(null, { paths: paths, imports: imports });
2014-07-04 18:20:40 -04:00
});
2014-09-24 18:36:30 -04:00
}
2014-07-04 18:20:40 -04:00
Meta.css.buildBundle = function (target, fork, callback) {
async.waterfall([
function (next) {
getBundleMetadata(target, next);
},
function (data, next) {
var minify = global.env !== 'development';
minifier.css.bundle(data.imports, data.paths, minify, fork, next);
},
function (bundle, next) {
var filename = (target === 'client' ? 'stylesheet' : 'admin') + '.css';
fs.writeFile(path.join(__dirname, '../../build/public', filename), bundle.code, next);
},
], callback);
};
};