class Opal::SimpleServer
opal -Rserver app.rb
… or use the Server runner …
rackup -ropal -ropal/simple_server -b ‘Opal.append_path(“app”); run Opal::SimpleServer.new’
@example (CLI)
opal-webpack (NPM).
For a more complete implementation see opal-sprockets (Rubygems) or
development.
Opal::Builder and Ruby corelib/stdlib. It’s meant to be used just for local
Opal::SimpleServer is a very basic Rack server for Opal assets, it relies on
def append_path(path)
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
def builder(path)
def builder(path) builder = Opal::Builder.new builder.build(path.gsub(/(\.(?:rb|js|opal))*\z/, '')) end
def cache_invalidator
def cache_invalidator "?#{Time.now.to_i}" end
def call(env)
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
def call_index
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
def call_js(path)
def call_js(path) asset = fetch_asset(path) [ 200, { 'content-type' => 'application/javascript' }, [asset[:data], "\n", asset[:map].to_data_uri_comment], ] end
def fetch_asset(path)
def fetch_asset(path) builder = self.builder(path) { data: builder.to_s, map: builder.source_map } end
def initialize(options = {})
def initialize(options = {}) @prefix = options.fetch(:prefix, 'assets') @main = options.fetch(:main, 'application') @index_path = nil yield self if block_given? freeze end
def javascript_include_tag(path)
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