class Opal::SimpleServer

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).

@example (CLI)

rackup -ropal -ropal/simple_server -b 'Opal.append_path("app"); run Opal::SimpleServer.new'
... or use the Server runner ...
opal -Rserver app.rb

Constants

NotFound

Attributes

index_path[RW]
main[RW]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File 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

Public Instance Methods

append_path(path) click to toggle source

@deprecated It’s here for compatibility with Opal::Sprockets::Server

# File 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) click to toggle source
# File lib/opal/simple_server.rb, line 59
def builder(path)
  builder = Opal::Builder.new
  builder.build(path.gsub(/(\.(?:rb|js|opal))*\z/, ''))
end
cache_invalidator() click to toggle source
# File lib/opal/simple_server.rb, line 81
def cache_invalidator
  "?#{Time.now.to_i}"
end
call(env) click to toggle source
# File 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_index() click to toggle source
# File 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) click to toggle source
# File 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) click to toggle source
# File 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) click to toggle source
# File 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