File tree Expand file tree Collapse file tree 9 files changed +91
-12
lines changed Expand file tree Collapse file tree 9 files changed +91
-12
lines changed Original file line number Diff line number Diff line change 5
5
6
6
module React
7
7
module JSX
8
- # lazily loaded during first request
9
8
def self . context
10
- # TODO: create React::Source::contents_for
11
- contents =
12
- # If execjs uses therubyracer, there is no 'global'. Make sure
13
- # we have it so JSX script can work properly.
14
- 'var global = global || this;' +
15
- File . read ( React ::Source . bundled_path_for ( 'JSXTransformer.js' ) )
16
- @context ||= ExecJS . compile ( contents )
9
+ # lazily loaded during first request and reloaded every time when in dev or test
10
+ unless @context && ::Rails . env . production?
11
+
12
+ # TODO: create React::Source::contents_for
13
+ contents =
14
+ # If execjs uses therubyracer, there is no 'global'. Make sure
15
+ # we have it so JSX script can work properly.
16
+ 'var global = global || this;' +
17
+ File . read ( React ::Source . bundled_path_for ( 'JSXTransformer.js' ) )
18
+ @context = ExecJS . compile ( contents )
19
+ end
20
+
21
+ @context
17
22
end
18
23
19
24
def self . transform ( code )
Original file line number Diff line number Diff line change @@ -28,9 +28,17 @@ class Railtie < ::Rails::Railtie
28
28
tmp_path . join ( 'react.js' ) )
29
29
FileUtils . cp ( ::React ::Source . bundled_path_for ( 'JSXTransformer.js' ) ,
30
30
tmp_path . join ( 'JSXTransformer.js' ) )
31
+ app . assets . prepend_path tmp_path
31
32
32
- # Make sure it can be found
33
- app . assets . append_path ( tmp_path )
33
+ # Allow overriding react files that are not based on environment
34
+ # e.g. /vendor/react/JSXTransformer.js
35
+ dropin_path = app . root . join ( "vendor/assets/react" )
36
+ app . assets . prepend_path dropin_path if dropin_path . exist?
37
+
38
+ # Allow overriding react files that are based on environment
39
+ # e.g. /vendor/react/react.js
40
+ dropin_path_env = app . root . join ( "vendor/assets/react/#{ variant } " )
41
+ app . assets . prepend_path dropin_path_env if dropin_path_env . exist?
34
42
end
35
43
end
36
44
end
Original file line number Diff line number Diff line change
1
+ /** @jsx React.DOM */
2
+ < div /> ;
Original file line number Diff line number Diff line change 5
5
# test suite. You never need to work with it otherwise. Remember that
6
6
# your test database is "scratch space" for the test suite and is wiped
7
7
# and recreated between test runs. Don't rely on the data there!
8
- config . cache_classes = true
8
+
9
+ # we need this to reload the jsx transformer when different version is dropped in
10
+ config . cache_classes = false
11
+ config . reload_plugins = true
12
+ config . assets . cache_store = :null_store
9
13
10
14
# Do not eager load code on boot. This avoids loading your whole application
11
15
# just for the purpose of running a single test. If you are using a tool that
33
37
34
38
# Print deprecation notices to the stderr.
35
39
config . active_support . deprecation = :stderr
40
+
41
+ config . react . variant = :test
36
42
end
Original file line number Diff line number Diff line change
1
+ var JSXTransformer = {
2
+ transform : function ( ) {
3
+ return {
4
+ code : 'test_confirmation_token_jsx_transformed;'
5
+ } ;
6
+ }
7
+ } ;
Original file line number Diff line number Diff line change
1
+ 'test_confirmation_token_react_content' ;
Original file line number Diff line number Diff line change 1
1
require 'test_helper'
2
+ require 'fileutils'
2
3
3
- # The transformer is inserting a newline after the docblock for some reason...
4
+ # Sprockets is inserting a newline after the docblock for some reason...
4
5
EXPECTED_JS = <<eos
5
6
/** @jsx React.DOM */
6
7
@@ -28,6 +29,19 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
28
29
get 'assets/example.js'
29
30
assert_response :success
30
31
assert_equal EXPECTED_JS , @response . body
32
+ FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
33
+ end
34
+
35
+ test 'can use dropped in version of JSX transformer' do
36
+ hidden_path = File . expand_path ( "../dummy/vendor/assets/react/JSXTransformer__.js" , __FILE__ )
37
+ replacing_path = File . expand_path ( "../dummy/vendor/assets/react/JSXTransformer.js" , __FILE__ )
38
+
39
+ FileUtils . mv hidden_path , replacing_path
40
+ get 'assets/example2.js'
41
+ assert_response :success
42
+ assert_equal 'test_confirmation_token_jsx_transformed;' , @response . body
43
+ FileUtils . mv replacing_path , hidden_path
44
+ FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
31
45
end
32
46
33
47
test 'asset pipeline should transform JSX + Coffeescript' do
Original file line number Diff line number Diff line change
1
+ require 'test_helper'
2
+ require 'fileutils'
3
+
4
+ class ReactTest < ActionDispatch ::IntegrationTest
5
+
6
+ test 'asset pipeline should deliver react file in a non-production variant' do
7
+ actual_react_file_path = File . expand_path ( "../dummy/tmp/react-rails/react.js" , __FILE__ )
8
+ actual_react_file_content = File . read actual_react_file_path
9
+
10
+ react_file_token = "'test_confirmation_token_react_content_non_production';\n " ;
11
+ File . open ( actual_react_file_path , 'w' ) { |f | f . write react_file_token }
12
+
13
+ get 'assets/react.js'
14
+ assert_response :success
15
+ assert_equal react_file_token , @response . body
16
+
17
+ File . open ( actual_react_file_path , 'w' ) { |f | f . write actual_react_file_content }
18
+ FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
19
+ end
20
+
21
+ test 'asset pipeline should deliver drop-in react file replacement' do
22
+ hidden_path = File . expand_path ( "../dummy/vendor/assets/react/test/react__.js" , __FILE__ )
23
+ replacing_path = File . expand_path ( "../dummy/vendor/assets/react/test/react.js" , __FILE__ )
24
+
25
+ FileUtils . mv hidden_path , replacing_path
26
+ get 'assets/react.js'
27
+ assert_response :success
28
+ assert_equal "'test_confirmation_token_react_content';\n " , @response . body
29
+ FileUtils . mv replacing_path , hidden_path
30
+ FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
31
+ end
32
+
33
+ end
Original file line number Diff line number Diff line change 3
3
4
4
require File . expand_path ( "../dummy/config/environment.rb" , __FILE__ )
5
5
require "rails/test_help"
6
+ require "pathname"
7
+
8
+ CACHE_PATH = Pathname . new File . expand_path ( "../dummy/tmp/cache" , __FILE__ )
6
9
7
10
Rails . backtrace_cleaner . remove_silencers!
8
11
You can’t perform that action at this time.
0 commit comments