如何将一个包含较少css文件的目录编译为css?

oco*_*tts 47 css less

我有一个目录充满了较少的CSS文件.将它们编译为普通css的最佳方法是什么?(用于部署)

我喜欢运行如下命令:

lessc -r less/ css/
Run Code Online (Sandbox Code Playgroud)

其中lessc是较少的编译器(通过节点包管理器安装)

Dam*_*ian 74

执行此操作的方法是引导程序的作用 - 有一个文件可以导入所有其他文件并进行编译.

@import "variables.less"; 
@import "mixins.less";
Run Code Online (Sandbox Code Playgroud)

  • 原始问题(以及我刚刚尝试做的)讨论了如何监视整个目录并生成多个.less文件.因此虽然这个答案没有明确回答(并且lessc工具似乎也没有处理它)我放弃并使用这种方法将所有内容构建到all.css文件中,现在同意这是最好的选择: - ) (5认同)

Dan*_*ble 23

您可以使用以下bash one-liner将目录及其子目录中的所有较少文件编译为单个css文件"combined.css":

$ find less_directory/ -name '*.less' -exec lessc {} \; > combined.css
Run Code Online (Sandbox Code Playgroud)

或缩小生产:

$ find less_directory/ -name '*.less' -exec lessc -x {} \; > combined.css
Run Code Online (Sandbox Code Playgroud)


sha*_*ran 11

如果你想将多个较少的文件编译成多个css文件,请试试这个单行bash脚本:

for file in *.less; do lessc -x --yui-compress -O2 --strict-imports $file `basename $file`.css ; done
Run Code Online (Sandbox Code Playgroud)

运行该命令后,您将获得.less.css文件列表.

PS:它通过严格的导入评估(--strict-imports)添加最大优化(-O2),压缩(-x)并使用YUI Compressor(-- yui-compress)缩小.

编辑:对于新的YUI Compressor版本,请跳过-02和--yui-compress弃用,因此:

for file in *.less; do lessc -x --strict-imports $file `basename $file`.css ; done
Run Code Online (Sandbox Code Playgroud)


ilo*_*aly 6

这个bash脚本适合我:

find "$PWD" -name '*.less' | while read line; do
    REPLACE=`echo $line | sed "s|\.less|\.css|"`

    # echo "$line --> $REPLACE"
    (lessc "$line" "$REPLACE" &)
done
Run Code Online (Sandbox Code Playgroud)


Sam*_*ern 5

我制作了这个非常简单的 bash 脚本来将目录中的所有 LESS 文件编译为 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
Run Code Online (Sandbox Code Playgroud)


oco*_*tts 3

我写了一个解决这个问题的黑客脚本:

#!/usr/bin/env python


#This is responsible for "compiling" less.css files to regular css files for production. It also minifies the css at the same time. 

#Usage: give it a start directory as the first parameter and an end directory as the second parameter. It will recursivly run the appropriate command for all css files in the first subdirectory.


import os
import sys
import re
if len(sys.argv) < 3:
    sys.exit('ERROR: Too many paths!! No less css compiled')
if len(sys.argv) > 3:
    sys.exit('ERROR: Not enough paths!! No less css compiled')

start_dir=os.path.join(os.getcwd(),sys.argv[1])
end_dir=os.path.join(os.getcwd(),sys.argv[2])
pattern=r'\.css$'
pattern=re.compile(pattern)

files_compiled=0

def copy_files(start, end, add=''):
    global files_compiled
    try:
      os.mkdir(end)
    except:
      pass
    for folder in get_immediate_subdirectories(start):
      copy_files(os.path.join(start,folder), os.path.join(end+folder), add+folder+'/')
    for less in os.listdir(start):
      if pattern.search(less):
        os.system('lessc -x %s > %s'%(start+less,end+less))
        print add+less
        files_compiled=files_compiled+1

def get_immediate_subdirectories(dir):
    return [name for name in os.listdir(dir)
            if os.path.isdir(os.path.join(dir, name))]
Run Code Online (Sandbox Code Playgroud)

理想情况下有更好的解决方案。