require 'execjs' require 'mini_racer' module Tilt class ES6ModuleTranspilerTemplate < Tilt::Template self.default_mime_type = 'application/javascript' @mutex = Mutex.new @ctx_init = Mutex.new def self.call(input) filename = input[:filename] source = input[:data] context = input[:environment].context_class.new(input) result = new(filename){source}.render(context) context.metadata.merge(data: result) end def prepare # intentionally left empty # Tilt requires this method to be defined end def self.create_new_context # timeout any eval that takes longer than 15 seconds ctx = MiniRacer::Context.new(timeout: 15000) ctx.eval("var self = this; #{File.read("#{Rails.root}/vendor/assets/javascripts/babel.js")}") ctx.eval("module = {}; exports = {};"); ctx.attach("rails.logger.info", proc{|err| Rails.logger.info(err.to_s)}) ctx.attach("rails.logger.error", proc{|err| Rails.logger.error(err.to_s)}) ctx.eval <