Created
July 1, 2016 14:25
-
-
Save crhallberg/2b922eb1605413fdffebfd9b1d43c723 to your computer and use it in GitHub Desktop.
Automated sass compression for VuFind's grunt file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.