Skip to content

Commit c8ecba2

Browse files
committed
Switch to the new Sprockets processor
1 parent 0573ae2 commit c8ecba2

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

lib/react/jsx.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'execjs'
2+
require 'react/jsx/processor'
23
require 'react/jsx/template'
34
require 'react/jsx/jsx_transformer'
45
require 'react/jsx/babel_transformer'

lib/react/jsx/processor.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module React
2+
module JSX
3+
class Processor
4+
def self.instance
5+
@instance ||= new
6+
end
7+
8+
def self.call(input)
9+
instance.call(input)
10+
end
11+
12+
def call(input)
13+
{data: JSX::transform(input[:data])}
14+
end
15+
end
16+
end
17+
end

lib/react/rails/engine.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ module Rails
33
class Engine < ::Rails::Engine
44
initializer "react_rails.setup_engine", :group => :all do |app|
55
sprockets_env = app.assets || Sprockets # Sprockets 3.x expects this in a different place
6-
sprockets_env.register_engine(".jsx", React::JSX::Template)
6+
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
7+
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx"])
8+
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
9+
else
10+
sprockets_env.register_engine(".jsx", React::JSX::Template)
11+
end
712
end
813
end
914
end

test/react/jsx/jsx_transformer_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class JSXTransformerTest < ActionDispatch::IntegrationTest
1919
FileUtils.rm replacing_path
2020

2121
assert_response :success
22-
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
22+
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body.strip
2323
end
2424

2525
test 'accepts harmony: true option' do
@@ -52,7 +52,7 @@ class JSXTransformerTest < ActionDispatch::IntegrationTest
5252

5353
FileUtils.rm_rf custom_path
5454
assert_response :success
55-
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
55+
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body.strip
5656
end
5757

5858
end

0 commit comments

Comments
 (0)