Skip to content

Commit 6f27347

Browse files
committed
fix(JSX) don't reload the transformer in dev; separate JSXTranformer test
1 parent 9bb7b2c commit 6f27347

File tree

5 files changed

+74
-83
lines changed

5 files changed

+74
-83
lines changed

lib/react/jsx.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@
66

77
module React
88
module JSX
9-
mattr_accessor :transform_options, :transformer_class
9+
DEFAULT_TRANSFORMER = BabelTransformer
10+
mattr_accessor :transform_options, :transformer_class, :transformer
1011

1112
# You can assign `React::JSX.transformer_class = `
1213
# to provide your own transformer. It must implement:
1314
# - #initialize(options)
1415
# - #transform(code) => new code
15-
self.transformer_class = BabelTransformer
16+
self.transformer_class = DEFAULT_TRANSFORMER
1617

1718
def self.transform(code)
18-
transformer.transform(code)
19-
end
20-
21-
def self.transformer
22-
# lazily loaded during first request and reloaded every time when in dev or test
23-
if @transformer.nil? || !::Rails.env.production?
24-
@transformer = transformer_class.new(transform_options)
25-
end
26-
@transformer
19+
self.transformer ||= transformer_class.new(transform_options)
20+
self.transformer.transform(code)
2721
end
2822
end
2923
end

lib/react/rails/railtie.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Railtie < ::Rails::Railtie
99
config.react.variant = (::Rails.env.production? ? :production : :development)
1010
config.react.addons = false
1111
config.react.jsx_transform_options = {}
12-
config.react.jsx_transformer_class = React::JSX::BabelTransformer
12+
config.react.jsx_transformer_class = nil # defaults to BabelTransformer
1313
# Server rendering:
1414
config.react.server_renderer_pool_size = 10
1515
config.react.server_renderer_timeout = 20 # seconds
@@ -23,8 +23,10 @@ class Railtie < ::Rails::Railtie
2323

2424
# Include the react-rails view helper lazily
2525
initializer "react_rails.setup_view_helpers", group: :all do |app|
26-
React::JSX.transform_options = app.config.react.jsx_transform_options
26+
app.config.react.jsx_transformer_class ||= React::JSX::DEFAULT_TRANSFORMER
2727
React::JSX.transformer_class = app.config.react.jsx_transformer_class
28+
React::JSX.transform_options = app.config.react.jsx_transform_options
29+
2830
ActiveSupport.on_load(:action_view) do
2931
include ::React::Rails::ViewHelper
3032
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'test_helper'
2+
3+
class JSXTransformerTest < ActionDispatch::IntegrationTest
4+
setup do
5+
reset_transformer
6+
React::JSX.transformer_class = React::JSX::JSXTransformer
7+
end
8+
9+
teardown do
10+
reset_transformer
11+
end
12+
13+
test 'can use dropped-in version of JSX transformer' do
14+
hidden_path = Rails.root.join("vendor/assets/react/JSXTransformer__.js")
15+
replacing_path = Rails.root.join("vendor/assets/react/JSXTransformer.js")
16+
17+
FileUtils.cp hidden_path, replacing_path
18+
get '/assets/example3.js'
19+
FileUtils.rm replacing_path
20+
21+
assert_response :success
22+
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
23+
end
24+
25+
test 'accepts harmony: true option' do
26+
React::JSX.transform_options = {harmony: true}
27+
get '/assets/harmony_example.js'
28+
assert_response :success
29+
assert_match(/generateGreeting:\s*function\(\)/, @response.body, "object literal methods")
30+
assert_match(/React.__spread/, @response.body, "spreading props")
31+
assert_match(/Your greeting is: '" \+ insertedGreeting \+ "'/, @response.body, "string interpolation")
32+
assert_match(/active=\$__0\.active/, @response.body, "destructuring assignment")
33+
end
34+
35+
test 'accepts strip_types: true option' do
36+
React::JSX.transform_options = {strip_types: true, harmony: true}
37+
get '/assets/flow_types_example.js'
38+
assert_response :success
39+
assert_match(/\(i\s*,\s*name\s*\)\s*\{/, @response.body, "type annotations are removed")
40+
end
41+
42+
test 'accepts asset_path: option' do
43+
hidden_path = Rails.root.join("vendor/assets/react/JSXTransformer__.js")
44+
custom_path = Rails.root.join("vendor/assets/react/custom")
45+
replacing_path = custom_path.join("CustomTransformer.js")
46+
47+
React::JSX.transform_options = {asset_path: "custom/CustomTransformer.js"}
48+
49+
FileUtils.mkdir_p(custom_path)
50+
FileUtils.cp(hidden_path, replacing_path)
51+
get '/assets/example3.js'
52+
53+
FileUtils.rm_rf custom_path
54+
assert_response :success
55+
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
56+
end
57+
58+
end

test/react/jsx_test.rb

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
3636
reset_transformer
3737
end
3838

39-
def reset_transformer
40-
clear_sprockets_cache
41-
React::JSX.transformer_class = React::JSX::BabelTransformer
42-
React::JSX.transform_options = {}
43-
end
44-
4539
test 'asset pipeline should transform JSX' do
4640
get '/assets/example.js'
4741
assert_response :success
@@ -73,68 +67,4 @@ def test_babel_transformer_accepts_babel_transformation_options
7367

7468
assert !@response.body.include?('strict')
7569
end
76-
77-
end
78-
79-
class JSXTransformerTest < ActionDispatch::IntegrationTest
80-
81-
setup do
82-
reset_transformer
83-
end
84-
85-
teardown do
86-
reset_transformer
87-
end
88-
89-
def reset_transformer
90-
clear_sprockets_cache
91-
React::JSX.transformer_class = React::JSX::JSXTransformer
92-
React::JSX.transform_options = {}
93-
end
94-
95-
test 'can use dropped-in version of JSX transformer' do
96-
hidden_path = Rails.root.join("vendor/assets/react/JSXTransformer__.js")
97-
replacing_path = Rails.root.join("vendor/assets/react/JSXTransformer.js")
98-
99-
FileUtils.cp hidden_path, replacing_path
100-
get '/assets/example3.js'
101-
FileUtils.rm replacing_path
102-
103-
assert_response :success
104-
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
105-
end
106-
107-
test 'accepts harmony: true option' do
108-
React::JSX.transform_options = {harmony: true}
109-
get '/assets/harmony_example.js'
110-
assert_response :success
111-
assert_match(/generateGreeting:\s*function\(\)/, @response.body, "object literal methods")
112-
assert_match(/React.__spread/, @response.body, "spreading props")
113-
assert_match(/Your greeting is: '" \+ insertedGreeting \+ "'/, @response.body, "string interpolation")
114-
assert_match(/active=\$__0\.active/, @response.body, "destructuring assignment")
115-
end
116-
117-
test 'accepts strip_types: true option' do
118-
React::JSX.transform_options = {strip_types: true, harmony: true}
119-
get '/assets/flow_types_example.js'
120-
assert_response :success
121-
assert_match(/\(i\s*,\s*name\s*\)\s*\{/, @response.body, "type annotations are removed")
122-
end
123-
124-
test 'accepts asset_path: option' do
125-
hidden_path = Rails.root.join("vendor/assets/react/JSXTransformer__.js")
126-
custom_path = Rails.root.join("vendor/assets/react/custom")
127-
replacing_path = custom_path.join("CustomTransformer.js")
128-
129-
React::JSX.transform_options = {asset_path: "custom/CustomTransformer.js"}
130-
131-
FileUtils.mkdir_p(custom_path)
132-
FileUtils.cp(hidden_path, replacing_path)
133-
get '/assets/example3.js'
134-
135-
FileUtils.rm_rf custom_path
136-
assert_response :success
137-
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
138-
end
139-
14070
end

test/test_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def clear_sprockets_cache
2424
end
2525
end
2626

27+
def reset_transformer
28+
clear_sprockets_cache
29+
React::JSX.transformer_class = React::JSX::DEFAULT_TRANSFORMER
30+
React::JSX.transform_options = {}
31+
React::JSX.transformer = nil
32+
end
33+
2734
# Sprockets 2 doesn't expire this assets well in
2835
# this kind of setting,
2936
# so override `fresh?` to mark it as expired.

0 commit comments

Comments
 (0)