From c8ecba2fd799b72ad77dd926bb814eff498b9431 Mon Sep 17 00:00:00 2001 From: Eugenijus Radlinskas Date: Wed, 28 Oct 2015 20:55:55 +0200 Subject: [PATCH 1/2] Switch to the new Sprockets processor --- lib/react/jsx.rb | 1 + lib/react/jsx/processor.rb | 17 +++++++++++++++++ lib/react/rails/engine.rb | 7 ++++++- test/react/jsx/jsx_transformer_test.rb | 4 ++-- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 lib/react/jsx/processor.rb diff --git a/lib/react/jsx.rb b/lib/react/jsx.rb index d49475216..79b2e3494 100644 --- a/lib/react/jsx.rb +++ b/lib/react/jsx.rb @@ -1,4 +1,5 @@ require 'execjs' +require 'react/jsx/processor' require 'react/jsx/template' require 'react/jsx/jsx_transformer' require 'react/jsx/babel_transformer' diff --git a/lib/react/jsx/processor.rb b/lib/react/jsx/processor.rb new file mode 100644 index 000000000..d5821b90f --- /dev/null +++ b/lib/react/jsx/processor.rb @@ -0,0 +1,17 @@ +module React + module JSX + class Processor + def self.instance + @instance ||= new + end + + def self.call(input) + instance.call(input) + end + + def call(input) + {data: JSX::transform(input[:data])} + end + end + end +end diff --git a/lib/react/rails/engine.rb b/lib/react/rails/engine.rb index aef68a40c..c1c121f5d 100644 --- a/lib/react/rails/engine.rb +++ b/lib/react/rails/engine.rb @@ -3,7 +3,12 @@ module Rails class Engine < ::Rails::Engine initializer "react_rails.setup_engine", :group => :all do |app| sprockets_env = app.assets || Sprockets # Sprockets 3.x expects this in a different place - sprockets_env.register_engine(".jsx", React::JSX::Template) + if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0") + sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx"]) + sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor) + else + sprockets_env.register_engine(".jsx", React::JSX::Template) + end end end end diff --git a/test/react/jsx/jsx_transformer_test.rb b/test/react/jsx/jsx_transformer_test.rb index 8ed55593b..5e3f40ca8 100644 --- a/test/react/jsx/jsx_transformer_test.rb +++ b/test/react/jsx/jsx_transformer_test.rb @@ -19,7 +19,7 @@ class JSXTransformerTest < ActionDispatch::IntegrationTest FileUtils.rm replacing_path assert_response :success - assert_equal 'test_confirmation_token_jsx_transformed;', @response.body + assert_equal 'test_confirmation_token_jsx_transformed;', @response.body.strip end test 'accepts harmony: true option' do @@ -52,7 +52,7 @@ class JSXTransformerTest < ActionDispatch::IntegrationTest FileUtils.rm_rf custom_path assert_response :success - assert_equal 'test_confirmation_token_jsx_transformed;', @response.body + assert_equal 'test_confirmation_token_jsx_transformed;', @response.body.strip end end From a42be1e38abf24a704fa103c6f90862330dc0d05 Mon Sep 17 00:00:00 2001 From: Eugenijus Radlinskas Date: Thu, 29 Oct 2015 15:31:45 +0200 Subject: [PATCH 2/2] Simplify JSX processor --- lib/react/jsx/processor.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/react/jsx/processor.rb b/lib/react/jsx/processor.rb index d5821b90f..28dd6d9dd 100644 --- a/lib/react/jsx/processor.rb +++ b/lib/react/jsx/processor.rb @@ -1,16 +1,8 @@ module React module JSX class Processor - def self.instance - @instance ||= new - end - def self.call(input) - instance.call(input) - end - - def call(input) - {data: JSX::transform(input[:data])} + JSX::transform(input[:data]) end end end