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'
... or use the Server runner ...
opal -Rserver app.rb

Direct Known Subclasses

CliRunners::Server::App

Constant Summary collapse

NotFound =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SimpleServer.

Yields:

  • (_self)

Yield Parameters:



22
23
24
25
26
27
28
# File 'opal/lib/opal/simple_server.rb', line 22

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.



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

def index_path
  @index_path
end

#mainObject

Returns the value of attribute main.



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

def main
  @main
end

Instance Method Details

#append_path(path) ⇒ Object

Deprecated.

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



34
35
36
37
# File 'opal/lib/opal/simple_server.rb', line 34

def append_path(path)
  Opal.deprecation "`#{self.class}#append_path` is deprecated, please use `Opal.append_path(path)` instead (called from: #{caller(1, 1).first})"
  Opal.append_path path
end

#builder(path) ⇒ Object



59
60
61
62
# File 'opal/lib/opal/simple_server.rb', line 59

def builder(path)
  builder = Opal::Builder.new
  builder.build(path.gsub(/(\.(?:rb|js|opal))*\z/, ''))
end

#cache_invalidatorObject



81
82
83
# File 'opal/lib/opal/simple_server.rb', line 81

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

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'opal/lib/opal/simple_server.rb', line 39

def call(env)
  case env['PATH_INFO']
  when %r{\A/#{@prefix}/(.*)\.m?js\z}
    path, _cache_invalidator = Regexp.last_match(1).split('?', 2)
    call_js(path)
  else call_index
  end
rescue NotFound => error
  [404, {}, [error.to_s]]
end

#call_indexObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'opal/lib/opal/simple_server.rb', line 85

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="utf-8">
      </head>
      <body>
        #{javascript_include_tag(main)}
      </body>
    </html>
    HTML
  end
  [200, { 'content-type' => 'text/html' }, [html]]
end

#call_js(path) ⇒ Object



50
51
52
53
54
55
56
57
# File 'opal/lib/opal/simple_server.rb', line 50

def call_js(path)
  asset = fetch_asset(path)
  [
    200,
    { 'content-type' => 'application/javascript' },
    [asset[:data], "\n", asset[:map].to_data_uri_comment],
  ]
end

#fetch_asset(path) ⇒ Object



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

def fetch_asset(path)
  builder = self.builder(path)
  {
    data: builder.to_s,
    map: builder.source_map
  }
end

#javascript_include_tag(path) ⇒ Object



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

def javascript_include_tag(path)
  case Opal::Config.esm
  when true
    %{<script src="/#{@prefix}/#{path}.mjs#{cache_invalidator}" type="module"></script>}
  when false
    %{<script src="/#{@prefix}/#{path}.js#{cache_invalidator}"></script>}
  end
end