I have a directory full of less css files. What is the best way to compile them to normal css? (for deployment)
Id like to run a command as follows:
The way I compiled less files to the corresponding css files in the current folder tree:
find ./ -name '*.less' -type f -exec lessc {} {}.css \; -exec rename -f 's/.less.css/.css/' {}.css \;
For example, if you have main.less
somewhere in the folder tree, you will get main.css
in the same folder.
But it requires rename
command to be installed. To install it with help of Homebrew:
brew install rename
I just made the script on top of @iloveitaly's work:
#!/bin/bash
# file name: lesscdir
if [[ -z $1 || -z $2 ]];then
echo 'both arguments are needed'
exit
fi
find $1 -name '*.less' -printf '%P\n' | while read name; do
FROM=$(echo $1'/'$name)
TO=$(echo $2'/'$name | sed "s|\.less|\.css|")
TO_DIRNAME=$(dirname $TO)
if [ ! -e $TO_DIRNAME ];then
mkdir -p $TO_DIRNAME
fi
echo 'Compiling' $FROM '->' $TO
lessc $FROM $TO
done
and then:
$ chmod +x lesscdir
$ sudo ln -s $(readlink -f lesscdir) /usr/local/bin/
Although I like python the best and solve most problems with it, it's still very happy to use bash in linux environment.
This bash script works for me:
find "$PWD" -name '*.less' | while read line; do
REPLACE=`echo $line | sed "s|\.less|\.css|"`
# echo "$line --> $REPLACE"
(lessc "$line" "$REPLACE" &)
done
The way to do this is what bootstrap does - have a file which imports all the others and compile that.
@import "variables.less";
@import "mixins.less";
Here is the ideal solution.
Step 1: Create a new .less file that contains an @import statement for each of your existing .less files.
find less_directory/ -name '*.less' -exec echo "@import \"{}\";" \; > combined.less
Step 2: Compile the combined .less file.
lessc combined.less > compiled.css
I made this VERY simple bash script to compile all LESS files in a directory to CSS
#/bin/bash
echo "Compiling all LESS files to CSS"
for file in *.less
do
FROM=$file
TO=${file/.*/.css}
echo "$FROM --> $TO"
lessc $FROM $TO
done