Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modified the generate method to cache generated stylesheets in tmp/le…

…ss-cache and check modified times in development mode. Greatly speeds up requests during development.
  • Loading branch information...
commit de4c4f6a543763ec95ec0502092a1733150214c9 1 parent 3b4096c
Dave Rapin authored Alexis Sellier committed
Showing with 28 additions and 7 deletions.
  1. +2 −1  install.rb
  2. +26 −6 lib/more.rb
View
3  install.rb
@@ -1,4 +1,5 @@
require "fileutils"
include FileUtils::Verbose
-mkdir_p File.join(Rails.root, "app", "stylesheets")
+mkdir_p File.join(Rails.root, "app", "stylesheets")
+mkdir_p File.join(Rails.root, "tmp", "less-cache")
View
32 lib/more.rb
@@ -108,6 +108,10 @@ def exists?(path_as_array)
pathname && pathname.exist?
end
+ def cache_path
+ File.join(RAILS_ROOT, 'tmp', 'less-cache')
+ end
+
# Generates the .css from a .less or .lss file in Less::More.source_path matching
# the given parameters.
#
@@ -118,15 +122,31 @@ def exists?(path_as_array)
def generate(path_as_array)
source = pathname_from_array(path_as_array)
- if source.extname == ".css"
- css = File.read(source)
+ # check the tmp directory for an existing file
+ destination_dir = source.dirname.to_s.gsub(source_path, cache_path)
+ destination = File.join(destination_dir, source.basename.to_s.gsub('.less', '.css').gsub('.lss', '.css'))
+
+ # compare the modified times
+ if File.exists?(destination) and File.new(destination).mtime >= File.new(source).mtime
+ # cached destination file is the same as the source, just return the cached file
+ css = File.read(destination)
else
- engine = File.open(source) {|f| Less::Engine.new(f) }
- css = engine.to_css
- css.delete!("\n") if self.compression?
- css = (HEADER % [source.to_s]) << css if self.header?
+ if source.extname == ".css"
+ css = File.read(source)
+ else
+ engine = File.open(source) {|f| Less::Engine.new(f) }
+ css = engine.to_css
+ css.delete!("\n") if self.compression?
+ css = (HEADER % [source.to_s]) << css if self.header?
+ end
+ # write the file to our cache
+ FileUtils.mkdir_p destination_dir
+ File.open(destination, "w") {|f|
+ f.puts css
+ }
end
+ # return the css
css
end

5 comments on commit de4c4f6

@crushlovely

Checking the mtime of the source file doesn't take into account possible changes to files being pulled in via @import. As a result, the files are being cached incorrectly. In development mode, is it even worth caching?

@augustl

I've argued against caching the whole time, but a lot of ppl wants it and someone else implemented it, so I happily pulled it in.

This issue sounds annoying, though, feel free to open an issue in the issue tracker.

@rapind

I agree that it is pretty annoying to have to touch the less file whenever you change one of it's imports, however in my case at least I found it even more annoying to regenerate the css on every request. The added delay is very noticeable. To each their own.

@grosser

mtime support for imported files is already added, so no need to worry :)

@rapind

sweet. I'll take a look.

Please sign in to comment.
Something went wrong with that request. Please try again.