简体   繁体   English

如何在Windows环境中通过Rake任务调用Nodejs来编译一些LESS文件?

[英]How do I call Nodejs in Windows environment from a Rake task to compile some LESS files?

What it is the correct way to call nodejs from a Rake task? 从Rake任务中调用nodejs的正确方法是什么? I want to compile some LESS files into CSS. 我想将一些LESS文件编译成CSS。 I have the cssless compiler installed globally. 我已经全局安装了cssless编译器。

The command line Less compiler invoked with the lessc command should work. 使用lessc命令调用的命令行Less编译器应该可以工作。

You can start with installing the Ruby version of Less, which make the Less compiler available for Ruby: sudo gem install less 您可以从安装Less的Ruby版本开始,这使Less编译器可用于Ruby: sudo gem install less

Notice that you also will have to install therubyracer ( sudo gem install therubyracer ) if you will get this compiler working (not required when you replace the compiler with the node version). 请注意,如果sudo gem install therubyracer此编译器正常工作,则还必须安装therubyracer( sudo gem install therubyracer )(用节点版本替换编译器时不需要)。

Now you should be able to run the following command: lessc -v . 现在,您应该能够运行以下命令: lessc -v This should output something like that lessc 1.7.0 (LESS Compiler) [Ruby] 2.6.0 to the console. 这应该向控制台输出类似lessc 1.7.0 (LESS Compiler) [Ruby] 2.6.0的内容。

After these steps you can run npm install -g less which will install the Node Less compiler (and replace the Ruby compiler, both command install the executable on the same location). 完成这些步骤后,您可以运行npm install -g less ,它将安装Node Less编译器(并替换Ruby编译器,两个命令都将可执行文件安装在同一位置)。 Now the lessc -v command should output the following to the console lessc 1.7.5 (Less Compiler) [JavaScript] 现在, lessc -v命令应将以下内容输出到控制台lessc 1.7.5 (Less Compiler) [JavaScript]

Finally you can create a Rake task to compile Less. 最后,您可以创建一个Rake任务来编译Less。 An example of such as task can be found at: https://gist.github.com/pfig/1969062 and will look like that shown below: 此类任务的示例可在以下网址找到: https//gist.github.com/pfig/1969062 ,其外观如下所示:

require 'rubygems'
require 'less'
require 'rake'

SOURCE = "."
LESS = File.join( SOURCE, "path", "to", "less", "files" )
CONFIG = {
  'less'   => File.join( LESS, "less" ),
  'css'    => File.join( LESS, "css" ),
  'input'  => "style.less",
  'output' => "style.css"
}

desc "Compile Less"
task :lessc do
  less   = CONFIG['less']

  input  = File.join( less, CONFIG['input'] )
  output = File.join( CONFIG['css'], CONFIG['output'] )

  source = File.open( input, "r" ).read

  parser = Less::Parser.new( :paths => [less] )
  tree = parser.parse( source )

  File.open( output, "w+" ) do |f|
    f.puts tree.to_css( :compress => true )
  end
end # task :lessc

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

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