sinatra assets pipeline example

添加gem ```ruby Gemfile gem 'sinatra-asset-pipeline' gem 'sprockets-helpers'



添加一些代码
```ruby App.rb
require 'sinatra/asset_pipeline'

class App < Sinatra::Base
  # Include these files when precompiling assets
  set :assets_precompile, %w(app.js app.css *.png *.jpg *.svg *.eot *.ttf *.woff)

  # Logical path to your assets
  set :assets_prefix, 'assets'

  # Use another host for serving assets
  set :asset_host, 'http://<id>.cloudfront.net'

  # Serve assets using this protocol
  set :assets_protocol, :http

  # CSS minification
  set :assets_css_compressor, :sass

  # JavaScript minification
  set :assets_js_compressor, :uglifier

  register Sinatra::AssetPipeline

  get '/' do
    haml :index
  end
end

添加rake命令 ```ruby Rakefile require 'sinatra/asset_pipeline/task.rb' Sinatra::AssetPipeline::Task.define! App


执行命令
```bash 
$ rake assets:precompile
$ rake assets:clean

assets构结如下

 + assets
  + images
    - my-jpg.jpg
    - my-png.png
  + javascripts
    - app.js
    - my-scripts.coffee
  + stylesheets
    - app.css
    - my-styles.sass

 + public
  + assets 
    - app-85fba2fe007c49def96d83adaaff4b33.js
    ...

sprockets-helpers像在rails中使用asset-pipeline的help方法

一些常用的方法

<%= stylesheet_tag 'application' %>
<%= javascript_tag 'application', :expand => true %>
<%= image_path 'rails.png' %>

在sass方法

  background: url("bgr.png")
  改为
  background: asset-url("bgr.png")

  生成css的如下:
  background: url("/assets/circle.skin/bgr-87b18b0c77f6cf701cf828a516b698b5.jpg")

另一些方法详见:https://github.com/petebrowne/sprockets-helpers.git

源码地址

参考:

https://github.com/petebrowne/sprockets-helpers.git

https://github.com/kalasjocke/sinatra-asset-pipeline.git

http://railscasts.com/episodes/279-understanding-the-asset-pipeline

http://thechangelog.com/sinatra-asset-pipeline-sprockets-for-sinatra/

https://mutelight.org/asset-pipeline

http://gogojimmy.net/2012/07/03/understand-assets-pipline/

http://stackoverflow.com/questions/6213218/rails-3-1-and-image-assets

Comments