A lean asset compiler for Sinatra developed specifically for Coffeescript and SASS. It stitches all assets for a route into a single file, no uglification, no minification (configure your server to use Gzip), just lean, fast compilation.
If you use Bundler, add it to your Gemfile.
gem install sinatra-asset-snack
Require and register the gem then configure your routes.
require 'sinatra/asset_snack'
class App < Sinatra::Base
register Sinatra::AssetSnack
asset_map '/javascript/application.js', ['assets/js/**/*.js', 'assets/js/**/*.coffee']
asset_map '/stylesheets/application.css', ['assets/stylesheets/**/*.css', 'assets/stylesheets/**/*.scss']
end
In your view / layout reference your assets using the relevant tag
<link rel="stylesheet" href="/stylesheets/application.css" type="text/css" />
<script src="/javascript/application.js" type="text/javascript"></script>
If you want - use the helpers to add cache busting
<link rel="stylesheet" href="<%= nocache '/stylesheets/application.css' %>" type="text/css" />
<script src="<%= nocache '/javascript/application.js' %>" type="text/javascript"></script>
Configuration options may be passed into each compiler. For example
Sinatra::AssetSnack.configuration.compilers[:coffee_script] = {bare: true}
Sinatra::AssetSnack.configuration.compilers[:sass] = {syntax: :scss}
See each compiler's repo for the list of configuration options
To precompile your assets, you must set a constant APP_FILE
to the
source file which defines your Sinatra app.
APP_FILE = "lib/app/app.rb"
require 'sinatra/asset_snack/rake'
This defines the rake task assetsnack:build
which will precompile your
assets.