module Babel

Public Instance Methods

context(file) click to toggle source
# File lib/rails_com/utils/babel.rb, line 14
def context(file)
  `#{script_path} #{file}`
end
script_path() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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