@@ -5,15 +5,21 @@ def initialize(options={})
5
5
@replay_console = options . fetch ( :replay_console , true )
6
6
7
7
filenames = options . fetch ( :files , [ "react.js" , "components.js" ] )
8
- js_code = SetupJavascript . new ( filenames ) . code
8
+ js_code = GLOBAL_WRAPPER + CONSOLE_POLYFILL
9
+
10
+ filenames . each do |filename |
11
+ js_code << ::Rails . application . assets [ filename ] . to_s
12
+ end
13
+
9
14
@context = ExecJS . compile ( js_code )
10
15
end
11
16
12
17
def render ( component_name , props )
13
18
# pass prerender: :static to use renderToStaticMarkup
14
- react_render_method = "renderToString"
15
19
if props . is_a? ( Hash ) && props [ :prerender ] == :static
16
20
react_render_method = "renderToStaticMarkup"
21
+ else
22
+ react_render_method = "renderToString"
17
23
end
18
24
19
25
if !props . is_a? ( String )
@@ -33,23 +39,14 @@ def render(component_name, props)
33
39
raise PrerenderError . new ( component_name , props , err )
34
40
end
35
41
36
- class SetupJavascript
37
- GLOBAL_WRAPPER = <<-JS
38
- var global = global || this;
39
- var self = self || this;
40
- var window = window || this;
41
- JS
42
-
43
- attr_reader :code
44
-
45
- def initialize ( filenames )
46
- @code = GLOBAL_WRAPPER + CONSOLE_POLYFILL
47
- filenames . each do |filename |
48
- @code << ::Rails . application . assets [ filename ] . to_s
49
- end
50
- end
51
- end
42
+ # Handle node.js & other RubyRacer contexts
43
+ GLOBAL_WRAPPER = <<-JS
44
+ var global = global || this;
45
+ var self = self || this;
46
+ var window = window || this;
47
+ JS
52
48
49
+ # Reimplement console methods for replaying on the client
53
50
CONSOLE_POLYFILL = <<-JS
54
51
var console = { history: [] };
55
52
['error', 'log', 'info', 'warn'].forEach(function (fn) {
@@ -59,6 +56,7 @@ def initialize(filenames)
59
56
});
60
57
JS
61
58
59
+ # Replay message from console history
62
60
CONSOLE_REPLAY = <<-JS
63
61
(function (history) {
64
62
if (history && history.length > 0) {
0 commit comments