简体   繁体   English

每次保存文档时如何编译LESS文件?

[英]How do I compile LESS files every time I save a document?

I've installed Less via npm like this 我已经通过npm这样安装了Less

$ npm install -g less

Now every time that I want to compile source files to .css, I run 现在,每次我要将源文件编译为.css时,我都运行

$ lessc styles.less styles.css

Is there any way via the command line to make it listen when I save the document to compile it automatically? 保存文档以自动编译时,是否可以通过命令行使其监听?

The best solution out there I've found is the one recommended on the official LESS website: https://github.com/jgonera/autoless . 我找到的最好的解决方案是在LESS官方网站上推荐的一种解决方案: https : //github.com/jgonera/autoless It is dead simple to use. 它非常简单易用。 Also it listens to the changes in the imported files to compile. 它还会侦听导入文件中的更改以进行编译。

Have a look at this article: http://www.hongkiat.com/blog/less-auto-compile/ 看看这篇文章: http : //www.hongkiat.com/blog/less-auto-compile/

It offers GUI solutions (SimpLESS, WinLESS, LESS.app, and ChrunchApp) and a node solution. 它提供GUI解决方案(SimpLESS,WinLESS,LESS.app和ChrunchApp)和节点解决方案。 (deadsimple-less-watch-compiler) (deadsimple-less-watch-compiler)

Are you using less alone or with Node.JS ? 您是单独使用还是与Node.JS一起使用? Because if you are using it with node, there are easy ways to resolve this problem. 因为如果将它与node一起使用,则有解决此问题的简便方法。 The first two I can think of are (both these solutions go in your app.js ) : 我可以想到的前两个是(这两个解决方案都在您的app.js ):

  • using a middleware, like stated in this stack overflow discussion 使用中间件,如本堆栈溢出讨论中所述

     var lessMiddleware = require('less-middleware'); ... app.configure(function(){ //other configuration here... app.use(lessMiddleware({ src : __dirname + "/public", compress : true })); app.use(express.static(__dirname + '/public')); }); 
  • another method consists of making a system call as soon as you start your nodeJS instance (the method name may differ based on your NodeJS version) 另一种方法是在您启动nodeJS实例后立即进行系统调用(方法名称可能会因NodeJS版本而异)

     // before all the treatment is done execSync("lessc /public/stylesheets/styles.less /public/stylesheets/styles.css"); var app = express(); app.use(...); 

In both cases, Node will automatically convert the less files into css files. 在这两种情况下,Node都会自动将较少的文件转换为css文件。 Note that with the second option, Node was to be relaunched for the conversion to happen, whereas the first option will answer your need better, by always checking for a newer version in a given directory. 请注意,使用第二个选项时,将重新启动Node以进行转换,而第一个选项将始终检查给定目录中的较新版本,从而更好地满足您的需求。

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

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