aneta_bielska [:blog]

Assets management in Sinatra
#sinatra #assets #sprockets

If you wondered what is needed to integrate Sprockets with Sinatra, here is an example:

Example project tree:

# application tree

app
├── Gemfile
├── app.rb
├── config.ru
├── assets
│   ├── javascripts
│   │   ├── app.coffee
│   │   └── home.coffee
│   └── stylesheets
│       ├── app.scss
│       └── home.scss
└── views
    ├── index.slim
    └── layout.slim

Main application file:

# app/app.rb

# install and require all dependencies
require 'sinatra/base'
require 'sprockets'
require 'uglifier'
require 'sass'
require 'coffee-script'
require 'execjs'
require 'slim'

class App < Sinatra::Base
  # initialize new sprockets environment
  set :environment, Sprockets::Environment.new

  # append assets paths
  environment.append_path "assets/stylesheets"
  environment.append_path "assets/javascripts"

  # compress assets
  environment.js_compressor  = :uglify
  environment.css_compressor = :scss

  # get assets
  get "/assets/*" do
    env["PATH_INFO"].sub!("/assets", "")
    settings.environment.call(env)
  end

  get "/" do
    slim :index
  end
end
# app/config.ru

require './app'

run App.new

In main application js and css files require all files you need.

// app/assets/javascripts/app.coffee

//= require_tree .

Link to assets.

/ app/views/layout.slim

doctype html
head
  link rel="stylesheet" href="assets/app.css"
  script src="assets/app.js"
body
  == yield

That’s all you need to start.

For more check:

You may also enjoy:


#servers (1) #hosting (1) #sinatra (2) #assets (1) #sprockets (1) #react-js (1) #data-mapper (1) #ROM (1) #sequel (1) #ORM (1) #pg (1) #sqlite3 (1) #postgresql (1) #ruby (4) #ubuntu (1) #heroku (1) #git (1) #css (3) #cors (1) #same-origin-policy (1) #rake (1) #web-scraping (1) #ruby-on-rails (1) #brug (1) #elixir (1) #benchmark (1)