Class: Opal::Server

Inherits:
Object show all
Defined in:
opal/lib/opal/sprockets/server.rb

Defined Under Namespace

Classes: Index

Constant Summary

SOURCE_MAPS_PREFIX_PATH =
'/__OPAL_SOURCE_MAPS__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug_or_options = {}) {|_self| ... } ⇒ Server

Returns a new instance of Server

Yields:

  • (_self)

Yield Parameters:

  • _self (Opal::Server)

    the object that the method was called on



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'opal/lib/opal/sprockets/server.rb', line 22

def initialize debug_or_options = {}
  unless Hash === debug_or_options
    warn "passing a boolean to control debug is deprecated.\n"+
         "Please pass an Hash instead: Server.new(debug: true)"
    options = {:debug => debug_or_options}
  else
    options = debug_or_options
  end

  @use_index   = true
  @public_root = nil
  @public_urls = ['/']
  @sprockets   = options.fetch(:sprockets, ::Sprockets::Environment.new)
  @debug       = options.fetch(:debug, true)
  @prefix      = options.fetch(:prefix, '/assets')

  Opal.paths.each { |p| @sprockets.append_path(p) }

  yield self if block_given?
  create_app
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def debug
  @debug
end

#index_pathObject

Returns the value of attribute index_path



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def index_path
  @index_path
end

#mainObject

Returns the value of attribute main



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def main
  @main
end

#prefixObject

Returns the value of attribute prefix



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def prefix
  @prefix
end

#public_rootObject

Returns the value of attribute public_root



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def public_root
  @public_root
end

#public_urlsObject

Returns the value of attribute public_urls



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def public_urls
  @public_urls
end

#sprocketsObject

Returns the value of attribute sprockets



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def sprockets
  @sprockets
end

#use_indexObject

Returns the value of attribute use_index



19
20
21
# File 'opal/lib/opal/sprockets/server.rb', line 19

def use_index
  @use_index
end

Instance Method Details

#append_path(path) ⇒ Object



57
58
59
# File 'opal/lib/opal/sprockets/server.rb', line 57

def append_path path
  @sprockets.append_path path
end

#call(env) ⇒ Object



94
95
96
# File 'opal/lib/opal/sprockets/server.rb', line 94

def call(env)
  @app.call env
end

#create_appObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'opal/lib/opal/sprockets/server.rb', line 65

def create_app
  server, sprockets, prefix = self, @sprockets, self.prefix
  sprockets.logger.level ||= Logger::DEBUG
  source_map_enabled = self.source_map_enabled
  if source_map_enabled
    maps_prefix = SOURCE_MAPS_PREFIX_PATH
    maps_app = SourceMapServer.new(sprockets, maps_prefix)
    ::Opal::Sprockets::SourceMapHeaderPatch.inject!(maps_prefix)
  end

  @app = Rack::Builder.app do
    not_found = lambda { |env| [404, {}, []] }
    use Rack::Deflater
    use Rack::ShowExceptions
    use Index, server if server.use_index
    if source_map_enabled
      map(maps_prefix) do
        require 'rack/conditionalget'
        require 'rack/etag'
        use Rack::ConditionalGet
        use Rack::ETag
        run maps_app
      end
    end
    map(prefix)      { run sprockets }
    run Rack::Static.new(not_found, root: server.public_root, urls: server.public_urls)
  end
end

#public_dir=(dir) ⇒ Object



44
45
46
47
# File 'opal/lib/opal/sprockets/server.rb', line 44

def public_dir=(dir)
  @public_root = dir
  @public_urls = ["/"]
end

#source_map=(enabled) ⇒ Object



49
50
51
# File 'opal/lib/opal/sprockets/server.rb', line 49

def source_map=(enabled)
  Opal::Processor.source_map_enabled = enabled
end

#source_map_enabledObject



53
54
55
# File 'opal/lib/opal/sprockets/server.rb', line 53

def source_map_enabled
  Opal::Processor.source_map_enabled
end

#use_gem(gem_name) ⇒ Object



61
62
63
# File 'opal/lib/opal/sprockets/server.rb', line 61

def use_gem gem_name
  @sprockets.use_gem gem_name
end