Skip to content

Commit 004d3d9

Browse files
author
Robert Mosolgo
authored
Merge pull request #582 from seuros/patch-1
add preprocessor to handle comment directives in sprocket 3.7.0+ , fixes #580
2 parents 13ba683 + e9754b2 commit 004d3d9

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

lib/react/rails/railtie.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ class Railtie < ::Rails::Railtie
103103
if !sprockets_env.nil?
104104
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.7.0")
105105
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
106-
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
107106
sprockets_env.register_mime_type("application/jsx+coffee", extensions: [".jsx.coffee", ".js.jsx.coffee"])
107+
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
108108
sprockets_env.register_transformer("application/jsx+coffee", "application/jsx", Sprockets::CoffeeScriptProcessor)
109+
sprockets_env.register_preprocessor("application/jsx", Sprockets::DirectiveProcessor.new(comments: ["//", ["/*", "*/"]]))
110+
sprockets_env.register_preprocessor("application/jsx+coffee", Sprockets::DirectiveProcessor.new(comments: ["#", ["###", "###"]]))
109111
elsif Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
110112
sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript")
111113
else
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//= require ./jsx_require_child_js
2+
<div className="le-javascript" />
3+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div className="le-javascript-child" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'test_helper'
2+
3+
when_sprockets_available do
4+
class JSXPreprocessorTest < ActionDispatch::IntegrationTest
5+
EXPECTED_JS = <<javascript
6+
React.createElement(\"div\", { className: \"le-javascript-child\" });
7+
React.createElement(\"div\", { className: \"le-javascript\" });
8+
javascript
9+
10+
test 'executes //= require directives' do
11+
get '/assets/require_test/jsx_preprocessor_test.js'
12+
assert_response :success
13+
assert_compiled_javascript_matches(@response.body, EXPECTED_JS)
14+
end
15+
end
16+
end

test/react/jsx_test.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,5 @@ def test_babel_transformer_accepts_babel_transformation_options
6666

6767
assert !@response.body.include?('strict')
6868
end
69-
70-
71-
# Different processors may generate slightly different outputs,
72-
# as some version inserts an extra "\n" at the beginning.
73-
# Because appraisal is used, multiple versions of coffee-script are treated
74-
# together. Remove all spaces to make test pass.
75-
def assert_compiled_javascript_matches(javascript, expectation)
76-
assert_equal expectation.gsub(/\s/, ''), javascript.gsub(/\s/, '')
77-
end
7869
end
7970
end

test/test_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,14 @@ def when_sprockets_available
113113
yield
114114
end
115115
end
116+
117+
class ActionDispatch::IntegrationTest
118+
119+
# Different processors may generate slightly different outputs,
120+
# as some version inserts an extra "\n" at the beginning.
121+
# Because appraisal is used, multiple versions of coffee-script are treated
122+
# together. Remove all spaces to make test pass.
123+
def assert_compiled_javascript_matches(javascript, expectation)
124+
assert_equal expectation.gsub(/\s/, ''), javascript.gsub(/\s/, '')
125+
end
126+
end

0 commit comments

Comments
 (0)