Module: React::JSX

Defined in:
lib/react/jsx.rb,
lib/react/jsx/template.rb

Defined Under Namespace

Classes: Template

Class Method Summary collapse

Class Method Details

.contextObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/react/jsx.rb', line 10

def self.context
  # lazily loaded during first request and reloaded every time when in dev or test
  unless @context && ::Rails.env.production?
    contents =
      # If execjs uses therubyracer, there is no 'global'. Make sure
      # we have it so JSX script can work properly.
      'var global = global || this;' +

      # search for transformer file using sprockets - allows user to override
      # this file in his own application
      File.read(::Rails.application.assets.resolve('JSXTransformer.js'))

    @context = ExecJS.compile(contents)
  end

  @context
end

.transform(code, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/react/jsx.rb', line 28

def self.transform(code, options={})
  js_options = {
    stripTypes: options[:strip_types],
    harmony: options[:harmony],
  }
  result = context.call('JSXTransformer.transform', code, js_options)
  return result['code']
end