Class: Opal::SimpleServer

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/simple_server.rb

Overview

Opal::SimpleServer is a very basic Rack server for Opal assets, it relies on Opal::Builder and Ruby corelib/stdlib. It's meant to be used just for local development.

For a more complete implementation see opal-sprockets (Rubygems) or opal-webpack (NPM).

Examples:

(CLI)

rackup -ropal -ropal/simple_server -b 'Opal.append_path("app"); run Opal::SimpleServer.new'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ SimpleServer

Returns a new instance of SimpleServer

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
22
23
# File 'opal/lib/opal/simple_server.rb', line 17

def initialize(options = {})
  @prefix = options.fetch(:prefix, 'assets')
  @main = options.fetch(:main, 'application')
  @index_path = nil
  yield self if block_given?
  freeze
end

Instance Attribute Details

#index_pathObject

Returns the value of attribute index_path



25
26
27
# File 'opal/lib/opal/simple_server.rb', line 25

def index_path
  @index_path
end

#mainObject

Returns the value of attribute main



25
26
27
# File 'opal/lib/opal/simple_server.rb', line 25

def main
  @main
end

Instance Method Details

#append_path(path) ⇒ Object

Deprecated.

It's here for compatibility with Opal::Sprockets::Server



29
30
31
32
# File 'opal/lib/opal/simple_server.rb', line 29

def append_path(path)
  Opal.deprecation 'Please use `Opal.append_path(path)` instead.'
  Opal.append_path path
end

#cache_invalidatorObject



78
79
80
# File 'opal/lib/opal/simple_server.rb', line 78

def cache_invalidator
  "?#{Time.now.to_i}"
end

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'opal/lib/opal/simple_server.rb', line 34

def call(env)
  case env['PATH_INFO']
  when %r{\A/#{@prefix}/(.*)\.map\z}
    path, cache_invalidator = $1.split('?', 2)
    call_map(path)
  when %r{\A/#{@prefix}/(.*)\z}
    path, cache_invalidator = $1.split('?', 2)
    call_asset(path)
  else call_index
  end
end

#call_asset(path) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'opal/lib/opal/simple_server.rb', line 46

def call_asset(path)
  asset = fetch_asset(path)
  [
    200,
    { 'Content-Type' => 'application/javascript',
      'X-SourceMap' => "/#{@prefix}/#{path}.map#{cache_invalidator}}" },
    [asset[:data]]
  ]
end

#call_indexObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'opal/lib/opal/simple_server.rb', line 82

def call_index
  if @index_path
    contents = File.read(@index_path)
    html = ERB.new(contents).result binding
  else
    html = <<-HTML
    <!doctype html>
    <html>
      <head>
        <meta charset="utf8">
        #{javascript_include_tag(main)}
      </head>
      <body></body>
    </html>
    HTML
  end
  [200, {'Content-Type' => 'text/html'}, [html]]
end

#call_map(path) ⇒ Object



56
57
58
59
60
61
62
63
# File 'opal/lib/opal/simple_server.rb', line 56

def call_map(path)
  asset = fetch_asset(path)
  [
    200,
    { 'Content-Type' => 'application/json' },
    [asset[:map]]
  ]
end

#fetch_asset(path) ⇒ Object



65
66
67
68
69
70
71
72
# File 'opal/lib/opal/simple_server.rb', line 65

def fetch_asset(path)
  builder = Opal::Builder.new
  builder.build(path.gsub(/(\.(?:rb|js|opal))*\z/, ''))
  {
    data: builder.to_s,
    map: builder.source_map.to_json
  }
end

#javascript_include_tag(path) ⇒ Object



74
75
76
# File 'opal/lib/opal/simple_server.rb', line 74

def javascript_include_tag(path)
  %{<script src="/#{@prefix}/#{path}.js#{cache_invalidator}"></script>}
end