How do I compile a directory full of less css files to css?

后端 未结 14 607
执念已碎
执念已碎 2020-12-12 22:09

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:

         


        
相关标签:
14条回答
  • 2020-12-12 22:38

    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
    
    0 讨论(0)
  • 2020-12-12 22:39

    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.

    0 讨论(0)
  • 2020-12-12 22:43

    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
    
    0 讨论(0)
  • 2020-12-12 22:46

    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";
    
    0 讨论(0)
  • 2020-12-12 22:46

    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
    
    0 讨论(0)
  • 2020-12-12 22:47

    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
    
    0 讨论(0)
提交回复
热议问题