class React::JSX::JSXTransformer

A {React::JSX}-compliant transformer which uses the deprecated ‘JSXTransformer.js` to transform JSX.

Constants

DEFAULT_ASSET_PATH

Public Class Methods

new(options) click to toggle source
# File lib/react/jsx/jsx_transformer.rb, line 7
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

Public Instance Methods

jsx_transform_code() click to toggle source

search for transformer file using sprockets - allows user to override this file in their own application

# File lib/react/jsx/jsx_transformer.rb, line 28
def jsx_transform_code
  ::Rails.application.assets[@asset_path].to_s
end
transform(code) click to toggle source
# File lib/react/jsx/jsx_transformer.rb, line 21
def transform(code)
  result = @context.call('JSXTransformer.transform', code, @transform_options)
  result['code']
end