This is a minimal demo app showing how to use Rails Assets with Sinatra application.
Rails Assets integrates Bower with Sprockets-based applications.
This application is using Sinatra Asset Pipeline that integrates Sinatra with Sprockets.
You also need to add appropriate source
and rails-assets-
gems.
source 'https://rubygems.org'
gem 'bundler', '>= 1.7.0'
gem 'sinatra', require: 'sinatra/base'
gem 'sinatra-asset-pipeline', require: 'sinatra/asset_pipeline'
gem 'uglifier'
gem 'slim'
source 'https://rails-assets.org' do
gem 'rails-assets-jquery'
gem 'rails-assets-bootstrap'
end
Integrating Rails Assets with Sprockets is as simple as appening its load paths (see application.rb).
class Application < Sinatra::Base
configure do
set :assets_precompile, %w(application.js application.css *.png *.jpg *.svg *.eot *.ttf *.woff)
set :assets_css_compressor, :sass
set :assets_js_compressor, :uglifier
register Sinatra::AssetPipeline
# Actual Rails Assets integration, everything else is Sprockets
if defined?(RailsAssets)
RailsAssets.load_paths.each do |path|
settings.sprockets.append_path(path)
end
end
end
get '/' do
slim :index
end
end
You use it the same way as in Rails.
#= require jquery
#= require bootstrap
Usually it's better to use @import
instead of require
:
@import "bootstrap";
You can use Sprockets Helpers to include application
manifests:
doctype html
html lang="en"
head
meta charset="utf-8"
title Sinatra with Rails Assets
meta name="viewport" content="width=device-width, initial-scale=1"
== stylesheet_tag 'application'
body
.container
== yield
== javascript_tag 'application'
You can use any ruby server, including bare rack:
bundle exec rackup --port 3000
Sinatra Asset Pipeline defines template for assets:precompile
task. It is enabled in Rakefile:
require_relative 'application'
require 'sinatra/asset_pipeline/task'
Sinatra::AssetPipeline::Task.define!(Application)
Heroku automatically runs assets:precompile
upon deploy, so you can just:
heroku create rails-assets-sinatra
git push heroku master
Deployed demo is available on: https://rails-assets-sinatra.herokuapp.com/
If you think you can improve this template in any way, please send a PR.
MIT