From 99cef1c9a67c558e9ca91cdd5d82cc74ed14e29a Mon Sep 17 00:00:00 2001 From: Marcus Mateus Date: Tue, 12 Jun 2012 22:17:26 -0500 Subject: [PATCH] Refactored and simplified sprockets asset caching * Done primarily so postprocessors would work properly (i.e. sprockets-commonjs) * Potentially eliminates need for all the JHW specific template engines, except NilTemplate --- lib/jasmine/headless/files_list.rb | 31 +++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index a97f11b..a26c560 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -153,10 +153,10 @@ module Jasmine::Headless register_engine ".#{extension}", Jasmine::Headless::NilTemplate end - register_engine '.coffee', Jasmine::Headless::CoffeeTemplate - register_engine '.js', Jasmine::Headless::JSTemplate - register_engine '.css', Jasmine::Headless::CSSTemplate - register_engine '.jst', Jasmine::Headless::JSTTemplate + # register_engine '.coffee', Jasmine::Headless::CoffeeTemplate + # register_engine '.js', Jasmine::Headless::JSTemplate + # register_engine '.css', Jasmine::Headless::CSSTemplate + # register_engine '.jst', Jasmine::Headless::JSTTemplate end @sprockets_environment @@ -211,7 +211,28 @@ module Jasmine::Headless alert_time = nil end - sprockets_environment.find_asset(file, :bundle => false).body + asset = sprockets_environment.find_asset(file, :bundle => false) + cache_file = File.join('.jhw-cache', 'code', asset.logical_path) + # Process & cache the asset only if needed + unless File.exist?(cache_file) && (File.mtime(file) > File.mtime(cache_file)) + FileUtils.mkdir_p File.dirname(cache_file) + asset.write_to(cache_file) + end + + html = '' + case asset.content_type + when 'application/javascript' + html = %{} + # NOTE: In future sprockets versions below could be simplified w/ asset.extensions.include?('.coffee') + if asset.pathname.basename.to_s.scan(/\.[^.]+/).include?('.coffee') + html += %{\n} + end + when 'text/css' + html = %{} + else + html = asset.body + end + html end.compact.reject(&:empty?) end