Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add opal/simple_server for simple non cached apps
The quickest way to get up and running with Opal: rackup -ropal -ropal/simple_server -b 'Opal.append_path("app"); run Opal::SimpleServer.new'
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require 'opal/deprecations' | ||
|
||
# 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' | ||
class Opal::SimpleServer | ||
require 'set' | ||
require 'erb' | ||
|
||
def initialize(options = {}) | ||
@prefix = options.fetch(:prefix, 'assets') | ||
@main = options.fetch(:main, 'application') | ||
yield self if block_given? | ||
freeze | ||
end | ||
|
||
attr_accessor :main, :index_path | ||
|
||
# @deprecated | ||
# It's here for compatibility with Opal::Sprockets::Server | ||
def append_path(path) | ||
Opal.deprecation 'Please use `Opal.append_path(path)` instead.' | ||
Opal.append_path path | ||
end | ||
|
||
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 | ||
|
||
def call_asset(path) | ||
asset = fetch_asset(path) | ||
[ | ||
200, | ||
{ 'Content-Type' => 'application/javascript', | ||
'X-SourceMap' => "/#{@prefix}/#{path}.map#{cache_invalidator}}" }, | ||
[asset[:data]] | ||
] | ||
end | ||
|
||
def call_map(path) | ||
asset = fetch_asset(path) | ||
[ | ||
200, | ||
{ 'Content-Type' => 'application/json' }, | ||
[asset[:map]] | ||
] | ||
end | ||
|
||
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 | ||
|
||
def javascript_include_tag(path) | ||
%{<script src="/#{@prefix}/#{path}.js#{cache_invalidator}"></script>} | ||
end | ||
|
||
def cache_invalidator | ||
"?#{Time.now.to_i}" | ||
end | ||
|
||
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 | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'lib/spec_helper' | ||
require 'rack/test' | ||
|
||
describe Opal::SimpleServer do | ||
include Rack::Test::Methods | ||
|
||
attr_accessor :app | ||
|
||
before do | ||
Opal.append_path "#{__dir__}/fixtures" | ||
self.app = described_class.new(main: 'console') | ||
end | ||
|
||
it 'serves opal assets' do | ||
response = get '/assets/console.js' | ||
expect(response.body).to eq(Opal::Builder.build('console').to_s) | ||
end | ||
|
||
it 'serves index for all non opal paths' do | ||
%w[/ /foo /foo/bar/baz].each do |path| | ||
response = get path | ||
expect(response.body).to include('<html>') | ||
expect(response.body).to include('<script') | ||
expect(response.body).to include('src="/assets/console.js') | ||
expect(response.headers['Content-type']).to eq('text/html') | ||
end | ||
end | ||
|
||
it 'serves the source map for the compiled asset' do | ||
response = get '/assets/console.map' | ||
expect(response.body).to eq(Opal::Builder.build('console').source_map.to_json) | ||
end | ||
|
||
it 'takes a :prefix option to set the assets prefix' do | ||
self.app = described_class.new(main: 'opal', prefix: 'foo') | ||
expect(get('/foo/console.js').body).to eq(Opal::Builder.build('console').to_s) | ||
self.app = described_class.new(main: 'opal', prefix: '/foo') | ||
expect(get('/foo/console.js').body).to eq(Opal::Builder.build('console').to_s) | ||
end | ||
|
||
it 'takes a :main option to set the main asset' do | ||
self.app = described_class.new(main: 'foo') | ||
expect(get('/').body).to include('src="/assets/foo.js') | ||
end | ||
end |