简体   繁体   English

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

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

I have a directory full of less css files. 我有一个目录充满了较少的CSS文件。 What is the best way to compile them to normal css? 将它们编译为普通css的最佳方法是什么? (for deployment) (用于部署)

Id like to run a command as follows: 我喜欢运行如下命令:

lessc -r less/ css/

where lessc is the less compiler (installed via node package manager) 其中lessc是较少的编译器(通过节点包管理器安装)

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";

You can use the following bash one-liner to compile and all less files in a directory and its subdirectories into a single css file "combined.css": 您可以使用以下bash one-liner将目录及其子目录中的所有较少文件编译为单个css文件“combined.css”:

$ find less_directory/ -name '*.less' -exec lessc {} \; > combined.css

Or minified for production: 或缩小生产:

$ find less_directory/ -name '*.less' -exec lessc -x {} \; > combined.css

If you want compile multiple less files into multiple css files try this one-liner bash script: 如果你想将多个较少的文件编译成多个css文件,请试试这个单行bash脚本:

for file in *.less; do lessc -x --yui-compress -O2 --strict-imports $file `basename $file`.css ; done

You will get a list of .less.css files after run the command. 运行该命令后,您将获得.less.css文件列表。

PS: It addittionaly optimizes ( -O2 ) at maximum, compress ( -x ) and minifies with YUI Compressor ( --yui-compress ) with strict import evaluation ( --strict-imports ). PS:它通过严格的导入评估( --strict-imports )添加最大优化( -O2 ),压缩( -x )并使用YUI Compressor (-- yui-compress缩小

EDITED : For new YUI Compressor versions skip -02 and --yui-compress deprecated, so: 编辑 :对于新的YUI Compressor版本,请跳过-02和--yui-compress弃用,因此:

for file in *.less; do lessc -x --strict-imports $file `basename $file`.css ; done

This bash script works for me: 这个bash脚本适合我:

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

    # echo "$line --> $REPLACE"
    (lessc "$line" "$REPLACE" &)
done

I made this VERY simple bash script to compile all LESS files in a directory to CSS 我制作了非常简单的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

I have written a hackish script that solves the problem: 我写了一个解决问题的hackish脚本:

#!/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))]

Ideally there is a better solution. 理想情况下有一个更好的解决方案。

I just made the script on top of @iloveitaly's work: 我刚刚在@ iloveitaly的工作上创建了脚本:

#!/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. 虽然我最喜欢python并且用它来解决大多数问题,但是仍然很高兴在linux环境中使用bash。

Recently I've been experiencing issues with running something like 最近我遇到了运行类似问题的问题

lessc directory/*.less foo.css

Instead of taking the different LESS's and outputting them into foo.css it would modify the second file in the list. 它不会取出不同的LESS并将它们输出到foo.css中,而是修改列表中的第二个文件。 This is not OK with me. 这跟我不一样。

To solve this problem, I made a new less frontend called lessm . 为了解决这个问题,我创建了一个名为lessm的新前端。

You can check it out at https://github.com/jive/lessm . 您可以访问https://github.com/jive/lessm查看。

The way you'd use it is 你使用它的方式是

lessm foo.less foo2.less ../bar/bazz.less -o output.css

基于shakaran的答案,但是它不是.less.css文件,而是创建.css文件(不要在文件名中使用less,只在扩展名中使用):

for file in *.less; do lessc -x --strict-imports $file `basename $file | sed -e "s/less/css/"` ; done

Just found an awesome npm module to do that ! 刚刚找到一个很棒的npm模块来做到这一点! :) :)

Install it: 安装它:

$ npm install [-g] lessc-each

Run it: 运行:

$ lessc-each  ‹dir1›  ‹dir2›

Where ‹dir1› is the directory of Less files to compile, and ‹dir2› is the directory for output files. 其中<dir1>是要编译的Less files的目录, <dir2>是输出文件的目录。 If ‹dir2› does not exist, it will automatically be created. 如果<dir2>不存在,将自动创建。 Both directories must be relative to the current path of the command line. 两个目录必须相对于命令行的当前路径。

While browsing through all other answers, none of them worked for me. 在浏览所有其他答案时,它们都没有为我工作。 I found a good solution to handle my situation in a combination of answers. 我找到了一个很好的解决方案来处理我的情况。

First, you compile all less files into 1 less file. 首先,将所有较少的文件编译为1个较少的文件。 This because it might throw errors (like the bootstrap one). 这是因为它可能会抛出错误(如引导错误)。

cat less_directory/* > less_directory/combined.less

Now that you have 1 less file in the less folder, you can compile that one the css without it causing any issues: 现在你在less文件夹中减少了1个文件,你可以编译那个没有它的css而导致任何问题:

lessc less_directory/combined.less > css/style.css

After this, don't forget to throw away the combined.less, otherwise this will mess up the next compile. 在此之后,不要忘记扔掉组合。否则这将搞乱下一次编译。

rm -f less_directory/combined.less

You can automate this process by making it a batch script 您可以通过将其作为批处理脚本来自动执行此过程

The way I compiled less files to the corresponding css files in the current folder tree: 我将较少的文件编译到当前文件夹树中相应的css文件的方式:

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. 例如,如果在文件夹树中的某处有main.less ,则会在同一文件夹中获得main.css

But it requires rename command to be installed. 但它需要安装rename命令。 To install it with help of Homebrew: 在Homebrew的帮助下安装它:

brew install rename

If you are looking to compile all LESS files into a folder and subfolder tree in Windows. 如果您要将所有LESS文件编译到Windows中的文件夹和子文件夹树中。 The following solution is using a batch file in Windows: 以下解决方案是在Windows中使用批处理文件:

File compile-less.bat: @echo
cd ".\\pages" for /r %%i in (*.less) do call lessc --clean-css "%%~i" "%%~dpni.min.css" cd ..
文件compile-less.bat: @echo
cd ".\\pages" for /r %%i in (*.less) do call lessc --clean-css "%%~i" "%%~dpni.min.css" cd ..
@echo
cd ".\\pages" for /r %%i in (*.less) do call lessc --clean-css "%%~i" "%%~dpni.min.css" cd ..

EDITED: All the less file in the folder and subfolders will be compiled. 编辑:将编译文件夹和子文件夹中的所有较少文件。 Tested in Windows 10 and 8.1 在Windows 10和8.1中测试过

Here is the ideal solution. 这是理想的解决方案。

Step 1: Create a new .less file that contains an @import statement for each of your existing .less files. 步骤1:创建一个新的.less文件,其中包含每个现有.less文件的@import语句。

find less_directory/ -name '*.less' -exec echo "@import \"{}\";" \; > combined.less

Step 2: Compile the combined .less file. 第2步:编译组合的.less文件。

lessc combined.less > compiled.css

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM