As far as I know, Less is not able to use custom functions. However, the stylus does. So if you want to switch to an alternative CSS preprocessor, then you will have a lot of fun! (Stylus is really very similar to Less, and you should not switch to it much. Plus, connect-assets already supports Stylus, so it should easily connect to your environment.)
server.js
var fs = require('fs'); var stylus = require('stylus'); stylus(fs.readFileSync("styles.styl", 'utf8')) //load stylus file .define("versionedFile", versionedFile) //add our custom function to the stylus env .render(function(err, css){ //render! if (err) { throw err; } fs.writeFileSync("styles.css", css); //write the compiled css to a .css file }); //our custom function function versionedFile(filename) { ... lookup versioned file of filename ... return "versionedFilename.png" }
styles.styl
.image-block { background-image: url(versionedFile('unversionedFilename.png')); //this is how we use our custom function }
What will be compiled into:
styles.css
.image-block { background-image: url("versionedFilename.png");
redhotvengeance
source share