File tree 4 files changed +41
-12
lines changed
4 files changed +41
-12
lines changed Original file line number Diff line number Diff line change 5
5
6
6
module React
7
7
module JSX
8
- mattr_accessor :transform_options
8
+ mattr_accessor :transform_options , :transformer_class
9
+
10
+ # You can assign `React::JSX.transformer_class = `
11
+ # to provide your own transformer. It must implement:
12
+ # - #initialize(options)
13
+ # - #transform(code) => new code
14
+ self . transformer_class = Transformer
15
+
9
16
def self . transform ( code )
10
17
transformer . transform ( code )
11
18
end
12
19
13
20
def self . transformer
14
21
# lazily loaded during first request and reloaded every time when in dev or test
15
22
if @transformer . nil? || !::Rails . env . production?
16
- @transformer = Transformer . new ( transform_options )
23
+ @transformer = transformer_class . new ( transform_options )
17
24
end
18
25
@transformer
19
26
end
Original file line number Diff line number Diff line change 20
20
}).call(this);
21
21
eos
22
22
23
+ class NullTransformer
24
+ def initialize ( options = { } ) ; end
25
+ def transform ( code )
26
+ "TRANSFORMED CODE!;\n "
27
+ end
28
+ end
29
+
23
30
class JSXTransformTest < ActionDispatch ::IntegrationTest
24
- setup { clear_sprockets_cache }
25
- teardown { clear_sprockets_cache }
31
+ setup do
32
+ clear_sprockets_cache
33
+ end
34
+
35
+ teardown do
36
+ clear_sprockets_cache
37
+ React ::JSX . transformer_class = React ::JSX ::Transformer
38
+ React ::JSX . transform_options = { }
39
+ end
26
40
27
41
test 'asset pipeline should transform JSX' do
28
42
get '/assets/example.js'
29
- FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
30
43
assert_response :success
31
44
assert_equal EXPECTED_JS , @response . body
32
45
end
@@ -85,4 +98,11 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
85
98
assert_response :success
86
99
assert_equal 'test_confirmation_token_jsx_transformed;' , @response . body
87
100
end
101
+
102
+ test 'use a custom transformer' do
103
+ React ::JSX . transformer_class = NullTransformer
104
+ manually_expire_asset ( 'example2.js' )
105
+ get '/assets/example2.js'
106
+ assert_equal "TRANSFORMED CODE!;\n " , @response . body
107
+ end
88
108
end
Original file line number Diff line number Diff line change @@ -12,13 +12,7 @@ class ReactTest < ActionDispatch::IntegrationTest
12
12
app_react_file_path = File . expand_path ( "../dummy/vendor/assets/javascripts/react.js" , __FILE__ )
13
13
react_file_token = "'test_confirmation_token_react_content_non_production';\n "
14
14
File . write ( app_react_file_path , react_file_token )
15
-
16
- react_asset = Rails . application . assets [ 'react.js' ]
17
-
18
- # Sprockets 2 doesn't expire this asset correctly,
19
- # so override `fresh?` to mark it as expired.
20
- def react_asset . fresh? ( env ) ; false ; end
21
-
15
+ manually_expire_asset ( "react.js" )
22
16
react_asset = Rails . application . assets [ 'react.js' ]
23
17
24
18
get '/assets/react.js'
Original file line number Diff line number Diff line change @@ -19,6 +19,14 @@ def clear_sprockets_cache
19
19
end
20
20
end
21
21
22
+ # Sprockets 2 doesn't expire this assets well in
23
+ # this kind of setting,
24
+ # so override `fresh?` to mark it as expired.
25
+ def manually_expire_asset ( asset_name )
26
+ asset = Rails . application . assets [ asset_name ]
27
+ def asset . fresh? ( env ) ; false ; end
28
+ end
29
+
22
30
# Load support files
23
31
Dir [ "#{ File . dirname ( __FILE__ ) } /support/**/*.rb" ] . each { |f | require f }
24
32
You can’t perform that action at this time.
0 commit comments