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: FileNotFound, LogSubscriber, RenderError
Constant Summary
collapse
- VERSION =
"0.0.5".freeze
- @@context =
nil
Instance Method Summary
collapse
Instance Method Details
#context ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/humid.rb', line 82
def context
if @@context && Webpacker.env.development?
handle_stale_files
end
@@context
end
|
#create_context ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# 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 ⇒ Object
90
91
92
93
94
95
|
# File 'lib/humid.rb', line 90
def dispose
if @@context
@@context.dispose
@@context = nil
end
end
|
#handle_stale_files ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# 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 ⇒ Object
53
54
55
|
# File 'lib/humid.rb', line 53
def logger
config.logger
end
|
#remove_functions ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# 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) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# 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 ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/humid.rb', line 57
def renderer
<<~JS
var __renderer;
function setHumidRenderer(fn) {
__renderer = fn;
}
JS
end
|