Class: Volt::ComponentHandler
- Defined in:
- lib/volt/server/component_handler.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #compile_for_component(component_name, for_client, map = false) ⇒ Object
-
#initialize(component_paths) ⇒ ComponentHandler
constructor
A new instance of ComponentHandler.
Constructor Details
#initialize(component_paths) ⇒ ComponentHandler
Returns a new instance of ComponentHandler.
7 8 9 |
# File 'lib/volt/server/component_handler.rb', line 7 def initialize(component_paths) @component_paths = component_paths end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/volt/server/component_handler.rb', line 11 def call(env) req = Rack::Request.new(env) path = req.path.strip request_source_map = (File.extname(path) == '.map') # TODO: Sanatize template path component_name = path.gsub(/^\/components\//, '').gsub(/[.](js|map)$/, '') javascript_code = compile_for_component(component_name, true, request_source_map) [200, { 'Content-Type' => 'application/javascript; charset=utf-8' }, StringIO.new(javascript_code)] end |
#compile_for_component(component_name, for_client, map = false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/volt/server/component_handler.rb', line 26 def compile_for_component(component_name, for_client, map=false) code = ComponentCode.new(component_name, @component_paths, for_client).code # Compile the code # javascript_code = Opal.compile(code) builder = Opal::Builder.new.build_str(code, 'app.rb') if map js_code = builder.source_map else js_code = builder.to_s + "\n//# sourceMappingURL=#{component_name}.map" end js_code end |