Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/react/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ class Railtie < ::Rails::Railtie
if !sprockets_env.nil?
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.7.0")
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
sprockets_env.register_mime_type("application/jsx+coffee", extensions: [".jsx.coffee", ".js.jsx.coffee"])
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
sprockets_env.register_transformer("application/jsx+coffee", "application/jsx", Sprockets::CoffeeScriptProcessor)
sprockets_env.register_preprocessor("application/jsx", Sprockets::DirectiveProcessor.new(comments: ["//", ["/*", "*/"]]))
sprockets_env.register_preprocessor("application/jsx+coffee", Sprockets::DirectiveProcessor.new(comments: ["#", ["###", "###"]]))
elsif Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript")
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= require ./jsx_require_child_js
<div className="le-javascript" />

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div className="le-javascript-child" />
16 changes: 16 additions & 0 deletions test/react/jsx/jsx_prepocessor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

when_sprockets_available do
class JSXPreprocessorTest < ActionDispatch::IntegrationTest
EXPECTED_JS = <<javascript
React.createElement(\"div\", { className: \"le-javascript-child\" });
React.createElement(\"div\", { className: \"le-javascript\" });
javascript

test 'executes //= require directives' do
get '/assets/require_test/jsx_preprocessor_test.js'
assert_response :success
assert_compiled_javascript_matches(@response.body, EXPECTED_JS)
end
end
end
9 changes: 0 additions & 9 deletions test/react/jsx_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,5 @@ def test_babel_transformer_accepts_babel_transformation_options

assert !@response.body.include?('strict')
end


# Different processors may generate slightly different outputs,
# as some version inserts an extra "\n" at the beginning.
# Because appraisal is used, multiple versions of coffee-script are treated
# together. Remove all spaces to make test pass.
def assert_compiled_javascript_matches(javascript, expectation)
assert_equal expectation.gsub(/\s/, ''), javascript.gsub(/\s/, '')
end
end
end
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@ def when_sprockets_available
yield
end
end

class ActionDispatch::IntegrationTest

# Different processors may generate slightly different outputs,
# as some version inserts an extra "\n" at the beginning.
# Because appraisal is used, multiple versions of coffee-script are treated
# together. Remove all spaces to make test pass.
def assert_compiled_javascript_matches(javascript, expectation)
assert_equal expectation.gsub(/\s/, ''), javascript.gsub(/\s/, '')
end
end