如何将充满 less css 文件的目录编译为 css?
我有一个充满 less css 文件的目录。将它们编译为普通 css 的最佳方法是什么? (用于部署)
我想运行如下命令:
lessc -r less/ css/
其中 lessc 是 less 编译器(通过节点包管理器安装)
I have a directory full of less css files. What is the best way to compile them to normal 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
做到这一点的方法就是 bootstrap 所做的 - 有一个文件导入所有其他文件并编译它。
The way to do this is what bootstrap does - have a file which imports all the others and compile that.
您可以使用以下 bash 单行代码将目录及其子目录中的所有 less 文件编译为单个 css 文件“combined.css”:
或缩小以用于生产:
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":
Or minified for production:
如果您想将多个 less 文件编译为多个 css 文件,请尝试这个单行 bash 脚本:
运行命令后,您将获得 .less.css 文件列表。
PS:它还最大程度地优化(-O2),压缩(-x)并使用YUI 压缩器 (--yui-compress) 具有严格的导入评估 (--strict-imports)。
已编辑:对于新的 YUI Compressor 版本,跳过 -02 和 --yui-compress 已弃用,因此:
If you want compile multiple less files into multiple css files try this one-liner bash script:
You will get a list of .less.css files after run the command.
PS: It addittionaly optimizes (-O2) at maximum, compress (-x) and minifies with YUI Compressor (--yui-compress) with strict import evaluation (--strict-imports).
EDITED: For new YUI Compressor versions skip -02 and --yui-compress deprecated, so:
这个 bash 脚本对我有用:
This bash script works for me:
我制作了这个非常简单的 bash 脚本来将目录中的所有 LESS 文件编译为 CSS
I made this VERY simple bash script to compile all LESS files in a directory to CSS
我只是在 @iloveitaly 的工作之上编写了脚本:
然后:
虽然我最喜欢 python 并且用它解决了大多数问题,但在 Linux 环境中使用 bash 仍然很高兴。
I just made the script on top of @iloveitaly's work:
and then:
Although I like python the best and solve most problems with it, it's still very happy to use bash in linux environment.
我写了一个解决这个问题的黑客脚本:
理想情况下有一个更好的解决方案。
I have written a hackish script that solves the problem:
Ideally there is a better solution.
基于 shakaran 的答案,但它不是 .less.css 文件,而是创建 .css 文件(不要在文件名中使用 less,仅在扩展名中使用 less):
Based shakaran's answer, but instead of .less.css files, it creates .css files (don't use less in the filename, well only in the extension that is):
刚刚找到了一个很棒的 npm 模块 来做到这一点! :)
安装它:
运行它:
其中 是要编译的Less文件的目录, 是输出文件的目录。如果 不存在,则会自动创建。这两个目录必须相对于命令行的当前路径。
Just found an awesome npm module to do that ! :)
Install it:
Run it:
Where ‹dir1› is the directory of Less files to compile, and ‹dir2› is the directory for output files. If ‹dir2› does not exist, it will automatically be created. Both directories must be relative to the current path of the command line.
最近,我在运行类似的东西时遇到了问题,
而不是采用不同的 LESS 并将它们输出到 foo.css 中,而是会修改列表中的第二个文件。这对我来说不行。
为了解决这个问题,我制作了一个新的 less 前端,名为
lessm
。您可以在 https://github.com/jive/lessm 查看。
你使用它的方式是
Recently I've been experiencing issues with running something like
Instead of taking the different LESS's and outputting them into foo.css it would modify the second file in the list. This is not OK with me.
To solve this problem, I made a new less frontend called
lessm
.You can check it out at https://github.com/jive/lessm.
The way you'd use it is
如果您希望将所有 LESS 文件编译到 Windows 中的文件夹和子文件夹树中。
以下解决方案是在Windows中使用批处理文件:
文件compile-less.bat:
@echo
cd ".\pages"
对于 (*.less) 中的 /r %%i 请调用 lessc --clean-css "%%~i" "%%~dpni.min.css"
cd ..
已编辑: 文件夹及其子文件夹中的所有 less 文件将被编译。
在 Windows 10 和 8.1 中测试
If you are looking to compile all LESS files into a folder and subfolder tree in Windows.
The following solution is using a batch file in Windows:
File compile-less.bat:
@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
在浏览所有其他答案时,没有一个对我有用。我找到了一个很好的解决方案来结合答案来处理我的情况。
首先,将所有 less 文件编译为 1 个 less 文件。这是因为它可能会引发错误(如引导程序错误)。
现在 less 文件夹中有 1 个 less 文件,您可以编译该 css 而不会引起任何问题:
在此之后,不要忘记扔掉组合的.less,否则这会搞乱下一次编译。
您可以通过将其设为批处理脚本来自动化此过程
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. This because it might throw errors (like the bootstrap one).
Now that you have 1 less file in the less folder, you can compile that one the css without it causing any issues:
After this, don't forget to throw away the combined.less, otherwise this will mess up the next compile.
You can automate this process by making it a batch script
我将 less 文件编译到当前文件夹树中相应的 css 文件的方式:
例如,如果文件夹树中某处有
main.less
,您将得到main.css 在同一文件夹中。
但它需要安装
rename
命令。要在 Homebrew 的帮助下安装它:The way I compiled less files to the corresponding css files in the current folder tree:
For example, if you have
main.less
somewhere in the folder tree, you will getmain.css
in the same folder.But it requires
rename
command to be installed. To install it with help of Homebrew:这是理想的解决方案。
步骤 1:创建一个新的 .less 文件,其中包含每个现有 .less 文件的 @import 语句。
步骤 2:编译组合的 .less 文件。
Here is the ideal solution.
Step 1: Create a new .less file that contains an @import statement for each of your existing .less files.
Step 2: Compile the combined .less file.