Rework R2 support for changes in sprockets
authorTom Hughes <tom@compton.nu>
Thu, 15 Sep 2016 18:02:18 +0000 (19:02 +0100)
committerTom Hughes <tom@compton.nu>
Thu, 15 Sep 2016 18:07:09 +0000 (19:07 +0100)
config/initializers/r2.rb

index 545d7bc0274db58afb8488d731ad9ed45f8c6315..7fbb5c115002480f2f969303f7ca0b3fefbc9be5 100644 (file)
@@ -1,23 +1,16 @@
-class R2Template < Tilt::Template
-  self.default_mime_type = "text/css"
+require "r2"
 
-  def self.engine_initialized?
-    defined? ::R2
-  end
-
-  def initialize_engine
-    require_template_library "r2"
-  end
-
-  def prepare
-    @output = R2.r2(data)
-  end
-
-  def evaluate(_scope, _locals, &_block)
-    @output
+class R2ScssProcessor < Sprockets::ScssProcessor
+  def self.call(input)
+    output = super(input)
+    data = R2.r2(output[:data])
+    output.delete(:map)
+    output.merge(:data => data)
   end
 end
 
 Rails.application.config.assets.configure do |env|
-  env.register_engine ".r2", R2Template
+  env.register_mime_type "text/r2+scss", :extensions => [".r2.scss"]
+  env.register_transformer "text/r2+scss", "text/css", R2ScssProcessor
+  env.register_preprocessor "text/r2+scss", Sprockets::DirectiveProcessor.new(:comments => ["//", ["/*", "*/"]])
 end