Module: Humid

Extended by:
Humid
Includes:
ActiveSupport::Configurable
Included in:
Humid
Defined in:
lib/humid.rb,
lib/humid/version.rb,
lib/humid/log_subscriber.rb,
lib/humid/controller_runtime.rb

Defined Under Namespace

Modules: ControllerRuntime Classes: LogSubscriber, RenderError

Constant Summary collapse

VERSION =
"0.0.4".freeze

Instance Method Summary collapse

Instance Method Details

#contextObject



73
74
75
76
77
78
79
# File 'lib/humid.rb', line 73

def context
  if @@context && Webpacker.env.development?
    handle_stale_files
  end

  @@context
end

#create_contextObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/humid.rb', line 88

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
  server_rendering_file = config.server_rendering_file
  server_rendering_map = "#{config.server_rendering_file}.map"

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

  filename = File.basename(source_path.to_s)
  @@current_filename = filename
  ctx.eval(File.read(source_path), filename: filename)

  @@context = ctx
end

#disposeObject



81
82
83
84
85
86
# File 'lib/humid.rb', line 81

def dispose
  if @@context
    @@context.dispose
    @@context = nil
  end
end

#handle_stale_filesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/humid.rb', line 57

def handle_stale_files
  if Webpacker.compiler.stale?
    Webpacker.compiler.compile
  end

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

  if @@current_filename != filename
    dispose
    create_context
  end
end

#loggerObject



44
45
46
# File 'lib/humid.rb', line 44

def logger
  config.logger
end

#remove_functionsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/humid.rb', line 33

def remove_functions
  "    delete this.setTimeout;\n    delete this.setInterval;\n    delete this.clearTimeout;\n    delete this.clearInterval;\n    delete this.setImmediate;\n    delete this.clearImmediate;\n  JS\nend\n"

#render(*args) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/humid.rb', line 117

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")
    raise Humid::RenderError.new(message)
  end
end

#rendererObject



48
49
50
51
52
53
54
55
# File 'lib/humid.rb', line 48

def renderer
  "    var __renderer;\n    function setHumidRenderer(fn) {\n      __renderer = fn;\n    }\n  JS\nend\n"