module Humid

Constants

VERSION

Public Instance Methods

context() click to toggle source
# File lib/humid.rb, line 82
def context
  if @@context && Webpacker.env.development?
    handle_stale_files
  end

  @@context
end
create_context() click to toggle source
# File lib/humid.rb, line 97
def create_context
  ctx = MiniRacer::Context.new(config.context_options)
  ctx.attach("console.log", proc { |err| logger.debug(err.to_s) })
  ctx.attach("console.info", proc { |err| logger.info(err.to_s) })
  ctx.attach("console.error", proc { |err| logger.error(err.to_s) })
  ctx.attach("console.warn", proc { |err| logger.warn(err.to_s) })

  js = ""
  js << remove_functions
  js << renderer
  ctx.eval(js)

  public_path = Webpacker.config.public_path

  webpack_source_file = Webpacker.manifest.lookup(config.server_rendering_pack)
  if webpack_source_file.nil?
    raise FileNotFound.new("Humid could not find a built pack for #{config.server_rendering_pack}")
  end

  if config.use_source_map
    webpack_source_map = Webpacker.manifest.lookup("#{config.server_rendering_pack}.map")
    map_path = public_path.join(webpack_source_map[1..-1])
    ctx.attach("readSourceMap", proc { File.read(map_path) })
  end

  source_path = public_path.join(webpack_source_file[1..-1])
  filename = File.basename(source_path.to_s)
  @@current_filename = filename
  ctx.eval(File.read(source_path), filename: filename)

  @@context = ctx
end
dispose() click to toggle source
# File lib/humid.rb, line 90
def dispose
  if @@context
    @@context.dispose
    @@context = nil
  end
end
handle_stale_files() click to toggle source
# File lib/humid.rb, line 66
def handle_stale_files
  if Webpacker.compiler.stale?
    Webpacker.compiler.compile
  end

  public_path = Webpacker.config.public_path
  server_rendering_pack = config.server_rendering_pack
  source_path = public_path.join(Webpacker.manifest.lookup(server_rendering_pack)[1..-1])
  filename = File.basename(source_path.to_s)

  if @@current_filename != filename
    dispose
    create_context
  end
end
logger() click to toggle source
# File lib/humid.rb, line 53
def logger
  config.logger
end
remove_functions() click to toggle source
# File lib/humid.rb, line 42
  def remove_functions
    <<~JS
      delete this.setTimeout;
      delete this.setInterval;
      delete this.clearTimeout;
      delete this.clearInterval;
      delete this.setImmediate;
      delete this.clearImmediate;
    JS
  end
render(*args) click to toggle source
# File lib/humid.rb, line 130
def render(*args)
  ActiveSupport::Notifications.instrument("render.humid") do
    context.call("__renderer", *args)
  rescue MiniRacer::RuntimeError => e
    message = ([e.message] + e.backtrace.filter {|x| x.starts_with? "JavaScript"}).join("\n")
    render_error = Humid::RenderError.new(message)

    if config.raise_render_errors
      raise render_error
    else
      config.logger.error(render_error.inspect)
      ""
    end
  end
end
renderer() click to toggle source
# File lib/humid.rb, line 57
  def renderer
    <<~JS
      var __renderer;
      function setHumidRenderer(fn) {
        __renderer = fn;
      }
    JS
  end