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 = <<-EOF
var input = #{JSON.dump(code)};
Babel.transform(input, {
"presets": ['stage-0', 'es2015', 'react'],
"plugins": ["transform-class-properties"]
}).code;
EOF
result = cxt.eval transormation
result = 'var Component = React.Component;' + result
[component_name, result]
end
|