How to automatically compile LESS into CSS on the server?

前端 未结 3 627
温柔的废话
温柔的废话 2021-01-31 06:43

Friend designer of mine was compiling his LESS file manually and uploading it with Coda (Remote Site) spending lots of precious time. He asked me:

Is it possible to aut

3条回答
  •  无人及你
    2021-01-31 07:15

    I have modified @romaninsh's solution so that it will recompile when any Less files in the directory are changed. I have also added an echo statement before compiling the Less files, to provide some validation that a change has been detected in case compilation takes a few seconds.

    /usr/local/bin/lesscwatch:

    #!/bin/bash                                                                     
    # Detect changes in .less file and automatically compile into .css                 
    [ "$2" ] || { echo "Specify both .less and .css files"; exit 1; }                  
    inotifywait . -m -e close_write | while read x op f; do                            
        if [[ "$f" == *".less" ]]; then                                                
            echo "Change detected. Recompiling...";                                    
            lessc $1 > $2 && echo "`date`: COMPILED";                                                                                                                           
        fi                                                                             
    done 
    

    This more closely mimics the behaviour of Less.app for Mac that I am used to.

    When developing with Less, I usually have a bunch of files in the /style directory of my project and compile everything down into a single .css file using overrides.

    Usage example:

    base.less:

    @import "overrides.less";
    @import "variables.less";
    
    body {
       ...
    }
    

    The usage is the same as

    lesscwatch base.less base.css
    

提交回复
热议问题