Skip to content
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ MyApp::Application.configure do
end

```
### Compile options

To enable the "Harmony" compile functionality of the JSX compiler (which turns on JS transformations such as ES6 classes, etc.):

```ruby
MyApp::Application.configure do
config.react.harmony = true
end
```


## CoffeeScript

Expand Down
4 changes: 3 additions & 1 deletion lib/react/jsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

module React
module JSX

def self.context
# lazily loaded during first request and reloaded every time when in dev or test
unless @context && ::Rails.env.production?
Expand All @@ -24,7 +25,8 @@ def self.context
end

def self.transform(code)
result = context.call('JSXTransformer.transform', code)
result = context.call('JSXTransformer.transform', code,
harmony: ::Rails.application.config.react.harmony)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we ever made any formal declaration that we don't support Ruby 1.8. I think all of our other hash uses continue with the rocket syntax.

Additionally, we should just make harmony an option to the transform function (or better, take an options hash) instead of relying on a global config like this.

return result['code']
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/react/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Railtie < ::Rails::Railtie
config.react.timeout = 20 #seconds
config.react.react_js = lambda {File.read(::Rails.application.assets.resolve('react.js'))}
config.react.component_filenames = ['components.js']
config.react.harmony = false # default to harmony compilation disabled

# Watch .jsx files for changes in dev, so we can reload the JS VMs with the new JS code.
initializer "react_rails.add_watchable_files" do |app|
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/assets/javascripts/example4.js.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @jsx React.DOM */
class Example4{}
var e=new Example4();
20 changes: 20 additions & 0 deletions test/jsxtransform_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
}).call(this);
eos

EXPECTED_JS_4 = <<eos
/** @jsx React.DOM */

function Example4(){"use strict";}
var e=new Example4();
eos


class JSXTransformTest < ActionDispatch::IntegrationTest

test 'asset pipeline should transform JSX' do
Expand All @@ -44,6 +52,18 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
assert_equal EXPECTED_JS_2.gsub(/\s/, ''), @response.body.gsub(/\s/, '')
end

test 'asset pipeline should transform JSX + Harmony' do
# multiple tests run simultaneously, so, the setting can't be flipped on
# and off for multiple tests (as they'll conflict)
Rails.application.config.react.harmony = true
get 'assets/example4.js'
assert_response :success
# remove any unnecessary trailing space from either example or response
# as it's not relevant to the test
assert_equal EXPECTED_JS_4.rstrip, @response.body.rstrip
end


test 'can use dropped in version of JSX transformer' do
hidden_path = File.expand_path("../dummy/vendor/assets/react/JSXTransformer__.js", __FILE__)
replacing_path = File.expand_path("../dummy/vendor/assets/react/JSXTransformer.js", __FILE__)
Expand Down