Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Rails 5 support?
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatov committed Feb 5, 2019
1 parent 065a907 commit 11366ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
20 changes: 15 additions & 5 deletions lib/sprockets/coffee-react-script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ module Sprockets
# Preprocessor that runs CJSX source files through coffee-react-transform
# then compiles with coffee-script
class CoffeeReactScript < Tilt::Template
CJSX_EXTENSION = /\.cjsx[^\/]*?$/
CJSX_PRAGMA = /^\s*#[ \t]*@cjsx/i
CJSX_EXTENSION = %r{\.cjsx[^/]*?$}.freeze
CJSX_PRAGMA = /^\s*#[ \t]*@cjsx/i.freeze

def prepare
end
def prepare; end

def evaluate(scope, locals, &block)
def evaluate(scope, _locals)
if scope.pathname.to_s =~ /\.coffee\.cjsx/
::CoffeeReact.transform(data)
elsif scope.pathname.to_s =~ CJSX_EXTENSION || data =~ CJSX_PRAGMA
Expand All @@ -23,5 +22,16 @@ def evaluate(scope, locals, &block)
end
end

def self.call(input)
filename = (input[:source_path] || input[:filename]).to_s
data = input[:data]
if filename =~ /\.coffee(\.source-.*)?\.cjsx/
::CoffeeReact.transform(data)
elsif filename =~ CJSX_EXTENSION
::CoffeeScript.compile(::CoffeeReact.transform(data))
else
data
end
end
end
end
11 changes: 4 additions & 7 deletions lib/sprockets/coffee-react.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class CoffeeReact < Tilt::Template
CJSX_EXTENSION = /\.(:?cjsx|coffee)[^\/]*?$/
CJSX_PRAGMA = /^\s*#[ \t]*@cjsx/i

def prepare
end
def prepare; end

def evaluate(scope, locals, &block)
def evaluate(scope, _locals, &_block)
if scope.pathname.to_s =~ CJSX_EXTENSION || data =~ CJSX_PRAGMA
::CoffeeReact.transform(data)
else
Expand All @@ -21,10 +20,8 @@ def evaluate(scope, locals, &block)
end

def self.install(environment = ::Sprockets)
environment.register_preprocessor 'application/javascript', Sprockets::CoffeeReact
environment.register_postprocessor 'application/javascript', Sprockets::CoffeeReactPostprocessor
environment.register_engine '.cjsx', Sprockets::CoffeeReactScript
environment.register_engine '.js.cjsx', Sprockets::CoffeeReactScript
environment.register_mime_type 'application/x-cjsx', extensions: %w[.cjsx .js.cjsx]
environment.register_transformer 'application/x-cjsx', 'application/javascript', Sprockets::CoffeeReactScript
end
end
end
10 changes: 4 additions & 6 deletions lib/sprockets/coffee-react/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
module Sprockets
class CoffeeReact
class Engine < ::Rails::Engine
initializer :setup_coffee_react, :after => "sprockets.environment", :group => :all do |app|
initializer :setup_coffee_react, after: 'sprockets.environment', group: :all do |app|
if app.assets
configure_env app.assets
else
app.config.assets.configure { |env| configure_env env }
app.config.assets.configure(&method(:configure_env))
end
end

def configure_env(env)
env.register_preprocessor 'application/javascript', Sprockets::CoffeeReact
env.register_postprocessor 'application/javascript', Sprockets::CoffeeReactPostprocessor
env.register_engine '.cjsx', Sprockets::CoffeeReactScript
env.register_engine '.js.cjsx', Sprockets::CoffeeReactScript
env.register_mime_type 'application/x-cjsx', extensions: %w[.cjsx .js.cjsx]
env.register_transformer 'application/x-cjsx', 'application/javascript', Sprockets::CoffeeReactScript
end
end
end
Expand Down

0 comments on commit 11366ac

Please sign in to comment.