Module: Babel

Extended by:
Babel
Included in:
Babel
Defined in:
lib/rails_com/utils/babel.rb

Instance Method Summary collapse

Instance Method Details

#context(file) ⇒ Object



14
15
16
# File 'lib/rails_com/utils/babel.rb', line 14

def context(file)
  `#{script_path} #{file}`
end

#script_pathObject



6
7
8
9
10
11
12
# File 'lib/rails_com/utils/babel.rb', line 6

def script_path
  @app_path = File.expand_path('.', Dir.pwd)
  node_modules_bin_path = ENV['WEBPACKER_NODE_MODULES_BIN_PATH'] || `yarn bin`.chomp
  babel_config = File.join(@app_path, '.babelrc')

  "#{node_modules_bin_path}/babel --config-file #{babel_config}"
end

#transform(code, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_com/utils/babel.rb', line 18

def transform(code, options = {})
  tmpfile = write_to_tempfile(code)

  begin
    r = context(tmpfile)
  ensure
    File.unlink(tmpfile)
  end

  r
end

#write_to_tempfile(contents) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rails_com/utils/babel.rb', line 30

def write_to_tempfile(contents)
  tmpfile = Tempfile.open(['babel', 'js'])
  tmpfile.write(contents)
  r = tmpfile.path
  tmpfile.close
  r
end