Skip to content

Commit

Permalink
Adding Sprockets-aware caching
Browse files Browse the repository at this point in the history
  • Loading branch information
petebrowne committed Feb 24, 2012
1 parent 0441c2f commit 40a7491
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/sprockets/sass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

module Sprockets
module Sass
autoload :Importer, "sprockets/sass/importer"
autoload :CacheStore, "sprockets/sass/cache_store"
autoload :Importer, "sprockets/sass/importer"

class << self
# Global configuration for `Sass::Engine` instances.
Expand Down
27 changes: 27 additions & 0 deletions lib/sprockets/sass/cache_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "sass"

module Sprockets
module Sass
class CacheStore < ::Sass::CacheStores::Base
attr_reader :environment

def initialize(environment)
@environment = environment
end

def _store(key, version, sha, contents)
environment.send :cache_set, "sass/#{key}", { :version => version, :sha => sha, :contents => contents }
end

def _retrieve(key, version, sha)
if obj = environment.send(:cache_get, "sass/#{key}")
return unless obj[:version] == version
return unless obj[:sha] == sha
obj[:obj]
else
nil
end
end
end
end
end
22 changes: 17 additions & 5 deletions lib/sprockets/sass/sass_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SassTemplate < Tilt::SassTemplate
# A reference to the current Sprockets context
attr_reader :context

# Templates are initialized once the
# Templates are initialized once the functions are added.
def self.engine_initialized?
super && defined?(@@sass_functions_added)
end
Expand Down Expand Up @@ -46,6 +46,17 @@ def evaluate(context, locals, &block)

protected

# Returns a Sprockets-aware cache store for Sass::Engine.
def cache_store
return nil if context.environment.nil?

if defined?(Sprockets::SassCacheStore)
Sprockets::SassCacheStore.new context.environment
else
CacheStore.new context.environment
end
end

# A reference to the custom Sass importer, `Sprockets::Sass::Importer`.
def importer
Importer.new context
Expand All @@ -54,10 +65,11 @@ def importer
# Assemble the options for the `Sass::Engine`
def sass_options
merge_sass_options(default_sass_options, options).merge(
:filename => eval_file,
:line => line,
:syntax => syntax,
:importer => importer
:filename => eval_file,
:line => line,
:syntax => syntax,
:cache_store => cache_store,
:importer => importer
)
end

Expand Down
11 changes: 11 additions & 0 deletions spec/sprockets-sass_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@
asset.should_not be_fresh(@env)
end

it "uses the environment's cache" do
cache = {}
@env.cache = cache

@assets.file "main.css.scss", %($color: blue;;\nbody { color: $color; })

@env['main.css'].to_s
sass_cache = cache.keys.detect { |key| key =~ /main\.css\.scss/ }
sass_cache.should_not be_nil
end

it "adds the #asset_path helper" do
@assets.file "asset_path.css.scss", %(body { background: url(asset-path("image.jpg")); })
@assets.file "asset_url.css.scss", %(body { background: asset-url("image.jpg"); })
Expand Down

0 comments on commit 40a7491

Please sign in to comment.