简体   繁体   中英

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? I want to compile some LESS files into CSS. I have the cssless compiler installed globally.

The command line Less compiler invoked with the lessc command should work.

You can start with installing the Ruby version of Less, which make the Less compiler available for 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).

Now you should be able to run the following command: lessc -v . This should output something like that lessc 1.7.0 (LESS Compiler) [Ruby] 2.6.0 to the console.

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). Now the lessc -v command should output the following to the console lessc 1.7.5 (Less Compiler) [JavaScript]

Finally you can create a Rake task to compile Less. An example of such as task can be found at: https://gist.github.com/pfig/1969062 and will look like that shown below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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