SmartAsset

Smart asset packaging for Rails, Sinatra, and Stasis.

Build Status

Features

Similar to AssetPackager, but with the following changes:

  • Uses file size and super fast hashing (Murmur3) to determine if a new compressed package should be created
  • uglify-js for javascript compression
  • clean-css for css compression
  • Framework agnostic (adapters provided for Rails 2, Rails 3, Sinatra, and Stasis)

Installation

Install gem


gem install smart_asset

Install npm packages


npm install uglify-js clean-css -g

Install

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 UglifyJS

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