Module: Render::React::Compiler

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

Class Method Summary collapse

Class Method Details

.cxtObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/render/react/compiler.rb', line 8

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

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

  @cxt
end

.load_componentsObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/render/react/compiler.rb', line 25

def load_components
  return if @components_loaded
  Config.paths.each do |path|
    files = Dir.glob(File.join(path, '**', '*.js'))
    files.each do |filename|
      name, code = Transpiler.babelify(filename)
      cxt.eval(code)
      lookup[name.to_sym] = true
    end
  end
  @components_loaded = true
end

.lookupObject



4
5
6
# File 'lib/render/react/compiler.rb', line 4

def lookup
  @lookup ||= {}
end

.render(component_class, **props) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/render/react/compiler.rb', line 38

def render(component_class, **props)
  unless lookup[component_class.to_sym]
    raise "#{component_class} component not found."
  end
  cxt.eval "    ReactDOMServer.renderToString(\n      React.createElement(\#{component_class}, \#{JSON.dump(props)})\n    );\n  EOS\nend\n"