如何在服务器上自动编译LESS到CSS?

时间:2012-11-28 16:45:02

标签: css linux events less iowait

我的朋友设计师正在手动编译他的LESS文件,并使用Coda(远程站点)上传它,花费了大量宝贵的时间。他问我:

是否可以自动检测Linux服务器上的文件更改并立即编译?

4 个答案:

答案 0 :(得分:11)

我制作了一个脚本并发布了详细信息:

  • 易于设计师使用
  • 保存文件后立即执行LESS编译器,而不消耗服务器资源
  • 任何能够进行远程编辑的编辑器都可以使用此解决方案 - Code,Sublime Text,Textmate

首先,您需要在服务器上安装“npm”,方法是在控制台中输入:

sudo apt-get install npm inotify-tools
sudo npm install -g less
sudo nano /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" == "$1" ]; then.
        lessc $f > $2 && echo "`date`: COMPILED";.
    fi
done

保存,退出,然后执行:

sudo chmod +x /usr/local/bin/lesscwatch

你们都完成了。下次需要使用LESS文件时,需要打开终端(Coda有内置),转到文件夹(使用cd)并执行:

lesscwatch main.less main.css

它将输出有关成功编译或错误的信息。享受。

答案 1 :(得分:3)

我修改了@ romaninsh的解决方案,以便在目录中的任何Less文件发生更改时重新编译。我还在编译Less文件之前添加了一个echo语句,以提供一些验证,即在编译需要几秒钟的情况下检测到更改。

的/ 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 

这更接近于模仿我以前使用的Less.app for Mac的行为。

使用Less进行开发时,我通常在项目的/ style目录中有一堆文件,并使用覆盖将所有文件编译成单个.css文件。

用法示例:

base.less:

@import "overrides.less";
@import "variables.less";

body {
   ...
}

用法与

相同
lesscwatch base.less base.css

答案 2 :(得分:2)

我喜欢bash脚本,但是在ubuntu 12.10中使用sublime时遇到了一些麻烦。 好, 脚本做了同样的Ian_Marcinkowski, 但我确信它会在第一个事件之后继续工作,并监视所有文件(有时是崇高文本,使用tmp文件,不要更改原始文件 - !?!)。

#!/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
    echo $f
    echo "Change detected. Recompiling...";
    lessc $2 > $3 && echo "`date`: COMPILED";
done

调用脚本如:

./monitor.sh  </path/to/dir>  <main.less> <main.css>

答案 3 :(得分:0)

这对我有用:

  1. 安装:
    sudo apt install node-lessenter

2.这是如何使用它。
lessc style.less style.css