diff --git a/lib/jade-rails/railtie.rb b/lib/jade-rails/railtie.rb index 16c0a94..fb298fd 100644 --- a/lib/jade-rails/railtie.rb +++ b/lib/jade-rails/railtie.rb @@ -5,12 +5,29 @@ class Railtie < Rails::Engine config.jade.pretty = Rails.env.development? config.jade.compile_debug = Rails.env.development? - config.before_initialize do |app| - register_template = -> (env) { (env || app.assets).register_engine('.jade', Jade::Template) } - if app.config.assets.respond_to?(:configure) - app.config.assets.configure { |env| register_template.call(env) } + def configure_assets(app) + if config.respond_to?(:assets) && config.assets.respond_to?(:configure) + # Rails 4.x + config.assets.configure { |env| yield(env) } else - register_template.call(nil) + # Rails 3.2 + yield(app.assets) + end + end + + initializer 'sprockets.jade', group: :all, after: 'sprockets.environment' do |app| + configure_assets(app) do |env| + # Sprockets 2, 3, and 4 + if env.respond_to?(:register_transformer) + env.register_mime_type 'text/x-jade', extensions: ['.jade'] + env.register_transformer 'text/x-jade', 'application/javascript', Jade::SprocketsTransformer + end + + if env.respond_to?(:register_engine) + args = ['.jade', Jade::SprocketsTransformer] + args << { mime_type: 'text/x-jade', silence_deprecation: true } if Sprockets::VERSION.start_with?('3') + env.register_engine(*args) + end end end end diff --git a/lib/jade-rails/template.rb b/lib/jade-rails/template.rb index 1461092..02e377a 100644 --- a/lib/jade-rails/template.rb +++ b/lib/jade-rails/template.rb @@ -1,12 +1,28 @@ # frozen_string_literal: true -require 'tilt' module Jade - class Template < Tilt::Template - def prepare + # Sprockets 2, 3 & 4 interface + # https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#registering-all-versions-of-sprockets-in-processors + class SprocketsTransformer + def initialize(filename, &block) + @filename = filename + @source = block.call end - def evaluate(context, locals, &block) - Jade.compile(data, filename: file, client: true) + def render(context, empty_hash_wtf) + self.class.run(@filename, @source, context) + end + + def self.run(filename, source, context) + Jade.compile(source, filename: filename, client: true) + end + + def self.call(input) + filename = input[:filename] + source = input[:data] + context = input[:environment].context_class.new(input) + + result = run(filename, source, context) + context.metadata.merge(data: result) end end end diff --git a/lib/pug-rails/railtie.rb b/lib/pug-rails/railtie.rb index 73cd6a9..407eb5d 100644 --- a/lib/pug-rails/railtie.rb +++ b/lib/pug-rails/railtie.rb @@ -5,12 +5,29 @@ class Railtie < Rails::Engine config.pug.pretty = Rails.env.development? config.pug.compile_debug = Rails.env.development? - config.before_initialize do |app| - register_template = -> (env) { (env || app.assets).register_engine('.pug', Pug::Template) } - if app.config.assets.respond_to?(:configure) - app.config.assets.configure { |env| register_template.call(env) } + def configure_assets(app) + if config.respond_to?(:assets) && config.assets.respond_to?(:configure) + # Rails 4.x + config.assets.configure { |env| yield(env) } else - register_template.call(nil) + # Rails 3.2 + yield(app.assets) + end + end + + initializer 'sprockets.pug', group: :all, after: 'sprockets.environment' do |app| + configure_assets(app) do |env| + # Sprockets 2, 3, and 4 + if env.respond_to?(:register_transformer) + env.register_mime_type 'text/x-pug', extensions: ['.pug'] + env.register_transformer 'text/x-pug', 'application/javascript+function', Pug::SprocketsTransformer + end + + if env.respond_to?(:register_engine) + args = ['.pug', Pug::SprocketsTransformer] + args << { mime_type: 'text/x-pug', silence_deprecation: true } if Sprockets::VERSION.start_with?('3') + env.register_engine(*args) + end end end end diff --git a/lib/pug-rails/template.rb b/lib/pug-rails/template.rb index da6b3e8..47ad624 100644 --- a/lib/pug-rails/template.rb +++ b/lib/pug-rails/template.rb @@ -1,12 +1,28 @@ # frozen_string_literal: true -require 'tilt' module Pug - class Template < Tilt::Template - def prepare + # Sprockets 2, 3 & 4 interface + # https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#registering-all-versions-of-sprockets-in-processors + class SprocketsTransformer + def initialize(filename, &block) + @filename = filename + @source = block.call end - def evaluate(context, locals, &block) - Pug.compile(data, filename: file, client: true) + def render(context, empty_hash_wtf) + self.class.run(@filename, @source, context) + end + + def self.run(filename, source, context) + Pug.compile(source, filename: filename, client: true) + end + + def self.call(input) + filename = input[:filename] + source = input[:data] + context = input[:environment].context_class.new(input) + + result = run(filename, source, context) + context.metadata.merge(data: result) end end end diff --git a/pug-rails.gemspec b/pug-rails.gemspec index e481406..53a556f 100644 --- a/pug-rails.gemspec +++ b/pug-rails.gemspec @@ -1,7 +1,7 @@ # frozen_string_literal: true Gem::Specification.new do |s| s.name = 'pug-rails' - s.version = '2.0.1' + s.version = '2.0.2' s.author = 'Yaroslav Konoplov' s.email = 'eahome00@gmail.com' s.summary = 'Pug/Jade template engine integration with Rails asset pipeline.' @@ -15,5 +15,4 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.add_dependency 'pug-ruby', '~> 1.0' - s.add_dependency 'tilt', '~> 2.0' end