Mass compile less files into css

Suppose you are using less and want to add the build step that does compile less into css over the whole project.

Here are the steps:

1. Install lessc

2. Install compresser if you need it

Now create following shell scripts and add them into your build process:

3. compile-one.sh compiles single less file (change --clean-css options to what you need)

#!/bin/sh
if [ $1 ]; 
  then 
    _basename=$(basename $1 .less)
    _dirname=$(dirname $1) 
  else
    echo "Usage: sh compile-one.sh <full_path_to_less>"
    echo "Example: sh compile-one.sh path/to/styles.less"
    exit
fi 

echo "Compiling "$1
lessc $1 $_dirname"/"$_basename".css" --clean-css="--s1 --advanced"

4. find-all-and-compile.sh compiles all less files in project/styles/dir and its descendants. styles.less -> styles.css

#!/bin/sh
find project/styles/dir -name *.less -exec sh compile-one.sh {} \;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.