Skip to content

Commit 7d2bf37

Browse files
committed
feat(React::JSX): add JSX.transform_options
1 parent 6be8f9d commit 7d2bf37

File tree

7 files changed

+42
-6
lines changed

7 files changed

+42
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ Component = React.createClass
6868
`<ExampleComponent videos={this.props.videos} />`
6969
```
7070

71+
You can use the `--harmony` option by adding a configuration to `application.rb`:
72+
73+
```ruby
74+
config.react.jsx_transform_options = {harmony: true}
75+
```
7176

7277
### Unobtrusive JavaScript
7378

lib/react/jsx.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
module React
77
module JSX
8+
mattr_accessor :transform_options
9+
810
def self.context
911
# lazily loaded during first request and reloaded every time when in dev or test
1012
unless @context && ::Rails.env.production?
@@ -23,8 +25,8 @@ def self.context
2325
@context
2426
end
2527

26-
def self.transform(code)
27-
result = context.call('JSXTransformer.transform', code)
28+
def self.transform(code, options={})
29+
result = context.call('JSXTransformer.transform', code, options)
2830
return result['code']
2931
end
3032
end

lib/react/jsx/template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def prepare
1010
end
1111

1212
def evaluate(scope, locals, &block)
13-
@output ||= JSX::transform(data)
13+
@output ||= JSX::transform(data, JSX.transform_options)
1414
end
1515
end
1616
end

lib/react/rails/railtie.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Railtie < ::Rails::Railtie
88
# Sensible defaults. Can be overridden in application.rb
99
config.react.variant = (::Rails.env.production? ? :production : :development)
1010
config.react.addons = false
11+
config.react.jsx_transform_options = {}
1112
# Server-side rendering
1213
config.react.max_renderers = 10
1314
config.react.timeout = 20 #seconds
@@ -20,7 +21,8 @@ class Railtie < ::Rails::Railtie
2021
end
2122

2223
# Include the react-rails view helper lazily
23-
initializer "react_rails.setup_view_helpers" do
24+
initializer "react_rails.setup_view_helpers" do |app|
25+
React::JSX.transform_options = app.config.react.jsx_transform_options
2426
ActiveSupport.on_load(:action_view) do
2527
include ::React::Rails::ViewHelper
2628
end

test/dummy/app/assets/javascripts/application.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
//= require es5-shim/es5-shim
1818
//= require react
1919
//= require react_ujs
20-
//= require_tree .
20+
//= require_tree ./components
21+
//= require ./pages
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var HarmonyComponent = React.createClass({
2+
statics: {
3+
generateGreeting() {
4+
return "Hello Harmony!"
5+
},
6+
generateGreetingWithWrapper() {
7+
var insertedGreeting = this.generateGreeting();
8+
return `Your greeting is: '${insertedGreeting}'.`
9+
},
10+
},
11+
render: function(){
12+
var greeting = HarmonyComponent.generateGreeting();
13+
return (
14+
<h1 {...this.props}>{greeting}</h1>
15+
)
16+
},
17+
});

test/jsxtransform_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
3838
assert_equal EXPECTED_JS_2.gsub(/\s/, ''), @response.body.gsub(/\s/, '')
3939
end
4040

41-
test 'can use dropped in version of JSX transformer' do
41+
test 'can use dropped-in version of JSX transformer' do
4242
hidden_path = File.expand_path("../dummy/vendor/assets/react/JSXTransformer__.js", __FILE__)
4343
replacing_path = File.expand_path("../dummy/vendor/assets/react/JSXTransformer.js", __FILE__)
4444

@@ -51,4 +51,13 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
5151
assert_response :success
5252
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
5353
end
54+
55+
test 'accepts harmony: true option' do
56+
React::JSX.transform_options = {harmony: true}
57+
get 'assets/harmony_example.js'
58+
assert_response :success
59+
assert_match(/generateGreeting:function\(\)/, @response.body, "object literal methods")
60+
assert_match(/React.__spread/, @response.body, "spreading props")
61+
assert_match(/Your greeting is: '" \+ insertedGreeting \+ "'/, @response.body, "string interpolation")
62+
end
5463
end

0 commit comments

Comments
 (0)