Module: Render::React::Transpiler

Defined in:
lib/render/react/transpiler.rb

Class Method Summary collapse

Class Method Details

.babelify(filepath) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/render/react/transpiler.rb', line 21

def babelify(filepath)
  code = File.read(filepath)
  component_name_match = code.match(/export default (\w+?);/)
  raise "can't find component name in #{filepath}" unless component_name_match
  component_name = component_name_match[1]

  code.gsub!(/export[^;]+;/, '')
  code.gsub!(/import[^;]+;/, '')
  code.gsub!(/require[^;]+;/, '')

  transormation = "    var input = \#{JSON.dump(code)};\n    Babel.transform(input, {\n      \"presets\": ['stage-0', 'es2015', 'react'],\n      \"plugins\": [\"transform-class-properties\"]\n    }).code;\n  EOF\n  result = cxt.eval transormation\n  result = 'var Component = React.Component;' + result\n\n  [component_name, result]\nend\n"

.cxtObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/render/react/transpiler.rb', line 4

def cxt
  return @cxt if @cxt
  @cxt = Config.new_context

  js_lib_files = Dir.glob(
    File.join(
      Config.gem_js_path,
      'transpiler',
      '**',
      '*.js'
    )
  )
  js_lib_files.each { |file| @cxt.load(file) }

  @cxt
end