Class: React::JSX::JSXTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/react/jsx/jsx_transformer.rb

Constant Summary collapse

DEFAULT_ASSET_PATH =
'JSXTransformer.js'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ JSXTransformer

Returns a new instance of JSXTransformer.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/react/jsx/jsx_transformer.rb', line 6

def initialize(options)
  @transform_options = {
    stripTypes: options.fetch(:strip_types, false),
    harmony:    options.fetch(:harmony, false),
  }

  @asset_path = options.fetch(:asset_path, DEFAULT_ASSET_PATH)

  # If execjs uses therubyracer, there is no 'global'. Make sure
  # we have it so JSX script can work properly.
  js_code = 'var global = global || this;' + jsx_transform_code
  @context = ExecJS.compile(js_code)
end

Instance Method Details

#jsx_transform_codeObject



26
27
28
29
30
# File 'lib/react/jsx/jsx_transformer.rb', line 26

def jsx_transform_code
  # search for transformer file using sprockets - allows user to override
  # this file in his own application
  ::Rails.application.assets[@asset_path].to_s
end

#transform(code) ⇒ Object



21
22
23
24
# File 'lib/react/jsx/jsx_transformer.rb', line 21

def transform(code)
  result = @context.call('JSXTransformer.transform', code, @transform_options)
  result["code"]
end