Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Jekyll plugin to render LESS ( files during generation.
module Jekyll
class LessCssFile < StaticFile
def write(dest)
# do nothing
class LessJsGenerator < Generator
safe true
priority :low
def generate(site)
src_root = site.config['source']
dest_root = site.config['destination']
less_ext = /\.less$/i
lessc_bin = site.config['lessc'] || 'lessc'
# static_files have already been filtered against excludes, etc.
site.static_files.each do |sf|
next if not sf.path =~ less_ext
less_path = sf.path
css_path = less_path.gsub(less_ext, '.css').gsub(src_root, dest_root)
relative_dir = File.dirname(css_path).gsub(dest_root, '')
file_name = File.basename(css_path)
command = [lessc_bin,
].join(' ')
puts 'Compiling LESS: ' + command
puts `#{command}`
raise "LESS compilation error" if $?.to_i != 0
# Add this output file so it won't be "cleaned away"
site.static_files <<, site.source, relative_dir, file_name)

This comment has been minimized.

Copy link
Owner Author

commented Apr 19, 2011

Added the "LessCssFile" subclass of a StaticFile so that the newly generated CSS file won't be automatically "cleaned" out of the output directory after rendering.


This comment has been minimized.

Copy link

commented Apr 21, 2011

Cool! I'm planning to bundle this up with a few other plugins and stick it in a repo. There's an open ticket on the Jekyll project discussing workarounds for the new cleaning -- this is actually one of the cleaner workarounds. See jekyll/jekyll#268 for that.


This comment has been minimized.

Copy link

commented Dec 10, 2012

Hello. I have found a bug in this logic, at least when I am running it locally. By modifying the site.static_files array in the loop there is the potential for processing elements twice or skipping some entirely.

My fix is to use a copy of the array, so:
site.static_files.dup.each do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.