我有一个目录充满了较少的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)
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)
这个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)
我制作了这个非常简单的 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)
我写了一个解决这个问题的黑客脚本:
#!/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)
理想情况下有更好的解决方案。