Skip to content

Commit 434834e

Browse files
committed
Merge pull request #136 from rmosolgo/harmony-option
feat(React::JSX): add JSX.transform_options
2 parents 6be8f9d + 7b75612 commit 434834e

File tree

8 files changed

+59
-6
lines changed

8 files changed

+59
-6
lines changed

README.md

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

71+
You can use the `--harmony` or `--strip-types` options by adding a configuration to `application.rb`:
72+
73+
```ruby
74+
config.react.jsx_transform_options = {
75+
harmony: true,
76+
strip_types: true, # for removing Flow type annotations
77+
}
78+
```
7179

7280
### Unobtrusive JavaScript
7381

lib/react/jsx.rb

Lines changed: 8 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,12 @@ 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+
js_options = {
30+
stripTypes: options[:strip_types],
31+
harmony: options[:harmony],
32+
}
33+
result = context.call('JSXTransformer.transform', code, js_options)
2834
return result['code']
2935
end
3036
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function flowTypesExample(i: number, name: string): string {
2+
return "OK"
3+
}
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: 17 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,20 @@ 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:\s*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
63+
64+
test 'accepts strip_types: true option' do
65+
React::JSX.transform_options = {strip_types: true, harmony: true}
66+
get '/assets/flow_types_example.js'
67+
assert_response :success
68+
assert_match(/\(i\s*,\s*name\s*\)\s*\{/, @response.body, "type annotations are removed")
69+
end
5470
end

0 commit comments

Comments
 (0)