Skip to content

Instantly share code, notes, and snippets.

@crhallberg
Created July 1, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crhallberg/2b922eb1605413fdffebfd9b1d43c723 to your computer and use it in GitHub Desktop.
Save crhallberg/2b922eb1605413fdffebfd9b1d43c723 to your computer and use it in GitHub Desktop.
Automated sass compression for VuFind's grunt file
grunt.registerMultiTask('css', function (arg1, arg2) {
var fs = require('fs')
, path = require('path')
, options = (arguments.length > 0 && this.data[arg1] && this.data[arg1].options)
? this.data[arg1].options
: this.data.dist.options
, theme = (arguments.length > 1) ? arg2 : null
, themeFolder = this.data.options.themeFolder || 'themes'
, themeList = fs.readdirSync(path.resolve(themeFolder))
, sassConfig = {}
;
var inheritance = {};
for (var i in themeList) {
if (theme && themeList[i] !== theme) {
continue;
}
var config = fs.readFileSync(path.join(themeFolder, themeList[i], 'theme.config.php'), 'UTF-8');
inheritance[themeList[i]] = config.toLowerCase().replace(/[\s']/g, '').match(/extends=>(\w+)/)[1];
}
for (var i in themeList) {
var sassDir = path.join(themeFolder, themeList[i], 'sass');
var cssDir = path.join(themeFolder, themeList[i], 'css');
try {
fs.statSync(sassDir);
// Build load path
var loadPath = [];
var curr = themeList[i];
while (inheritance[curr] != 'root') {
loadPath.unshift(path.join(themeFolder, inheritance[curr], 'sass'));
curr = inheritance[curr];
}
if (loadPath.length > 0) {
options.loadPath = loadPath;
}
// Compile
var files = {};
files[path.join(cssDir, 'compiled.css')] = path.join(sassDir, themeList[i].replace(/\d/g, '')+'.scss');
sassConfig[themeList[i]] = {
options: options,
files: files
};
} catch (err) {
// silently suppress thrown errors when no sass sources exist in a theme
}
}
grunt.config.set('sass', sassConfig);
grunt.task.run('sass');
grunt.task.run('replace');
});
@crhallberg
Copy link
Author

Attempts to discover all themes with sass, build their inheritance paths, and compile their sass automatically. Call with grunt css. Change task name at top if you wish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment