Skip to content

Instantly share code, notes, and snippets.

@KitaitiMakoto
Last active August 29, 2015 14:16
Show Gist options
  • Save KitaitiMakoto/db566ab8e6dbfd65f0f2 to your computer and use it in GitHub Desktop.
Save KitaitiMakoto/db566ab8e6dbfd65f0f2 to your computer and use it in GitHub Desktop.
Custom Elements development server
# Start server:
#
# $ shotgun -s Puma -o 127.0.0.1 -p 9393 server.rb
#
# You can load ES 6 file like this:
#
# <style>
# <%=js '/src/my-custom-element.js' %>
# </style>
require 'sinatra'
require 'pathname'
require 'erb'
require 'babel/transpiler'
configure do
set :app_file, (ENV['APP_FILE'] || File.expand_path('.'))
end
helpers do
def js(path)
full_path = File.join(settings.app_file, path)
Babel::Transpiler.transform(File.read(full_path))['code']
end
def example(code)
<<EOH
<pre><code>
#{escape_html code}</code></pre>
#{code}
EOH
end
end
get '/*' do
path = File.join(settings.app_file, clean_path_info(unescape(request.path_info)))
path = Pathname(path)
if path.file?
content_type mime_type(path.extname)
erb path.read
elsif path.directory?
index = path + 'index.html'
if index.file?
erb index.read
else
404
end
else
404
end
end
not_found do
'Not Found'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment