Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

174 lines (119 sloc) 3.764 kb

SmartAsset

Smart asset packaging for Rails and Sinatra.

Features

Similar to AssetPackager, but with the following changes:

  • Git modified date/time for package version (only re-packages files that have been modified)
  • Google Closure Compiler for javascript compression
  • Framework agnostic (adapters provided for Rails 2, Rails 3, and Sinatra)

Installation

Install Gem

gem install smart_asset

Rails 2

config/environment.rb

config.gem 'smart_asset'

Rails 3

Gemfile

gem 'smart_asset'

Sinatra

require 'sinatra/base'
require 'smart_asset'

class Application < Sinatra::Base
  include SmartAsset::Adapters::Sinatra
end

Create Configuration File

config/assets.yml

javascripts:
  package_1:
    - jquery/jquery
    - underscore
  package_2:
    - front_page
stylesheets:
  package_1:
    - blueprint/blueprint
    - 960
  package_2:
    - front_page

By default, SmartAsset will look for assets in public/javascripts and public/stylesheets.

Create Packaged Assets

cd to your project and run

smart_asset

If your project is Git version controlled, only the assets that have changed are repackaged.

Otherwise, all packages generate every time.

Include Packages in Your Template

<%= javascript_include_merged :package_1, :package_2 %>
<%= stylesheet_link_merged :package_1, :package_2 %>

Migrating from AssetPackager

  • rm vendor/plugins/asset_packager
  • Install SmartAsset
  • Move config/asset_packages.yml to config/assets.yml
  • Instead of running rake asset:packager:build_all, run smart_asset

Other Options

config/assets.yml

You may add extra options to your config/assets.yml file.

Below are the default values (excluding asset_host):

# Append random numbers to script paths on each request
append_random:
  development: true

# Asset host URL (defaults to ActionController::Base.asset_host or nil)
asset_host:
  production: http://assets%d.mydomain.com

# How many asset hosts you have (use if asset_host defined with %d)
asset_host_count: 4

# Public directory
public: public

# Package destination directory (within the public directory)
destination:
  javascripts: javascripts/packaged
  stylesheets: stylesheets/packaged

# Asset source directories (within the public directory)
sources:
  javascripts: javascripts
  stylesheets: stylesheets

smart_asset

You may use environment variables with the smart_asset command to alter its behavior.

DEBUG=1
Output commands that are running, leave the tmp file around for inspection

PACKAGE=package_1
Only compress a specific package

MODIFIED='12/1/2010 12:00'
Use a default modified time other than Time.now for non-version controlled files

WARN=1
Get compression warnings from Closure Compiler and YUI Compressor

STOP_ON_ERROR=1
smart_asset will exit if Closure Compiler or YUI Compressor return a non-zero exit code.

Example:

WARN=1 smart_asset

Running Specs

Forks and contributions to this project are much appreciated, but please make sure the specs run!

To run the basic specs:

spec spec

There are also framework specs to make sure the helpers work in Rails 2, Rails 3, and Sinatra 1:

spec/run
Jump to Line
Something went wrong with that request. Please try again.