sprockets-plugin

Package assets into gems for non-Rails applications.

Installation

$ gem install sprockets-plugin

Usage

Sprockets::Plugin is meant to be used within gems, to package assets for reuse. So the first step is to add it as a dependency in your gemspec:

Gem::Specification.new do |s|
  # ...
  s.add_runtime_dependency "sprockets-plugin"
end

And then extend Sprockets::Plugin and add the necessary asset paths:

require "sprockets-plugin"

class MyPlugin < Sprockets::Plugin
  # Set the root path to use relative paths in `.append_path`
  root File.expand_path("../..", __FILE__)
  append_path "lib/assets/images"
  append_path "lib/assets/javascripts"
  append_path "lib/assets/stylesheets"
end 

Now any assets in the "lib/assets" directory will be available to applications that require this gem:

require "sprockets"
require "my_plugin"

map "/assets" do
  environment = Sprockets::Environment.new
  # The assets from MyPlugin will be automatically appended.
  environment.append_path "assets/images"
  environment.append_path "assets/javascripts"
  environment.append_path "assets/stylesheets"
  run environment
end

Advanced Usage

You can package assets for Rails and non-Rails applications in the following way:

if defined? Rails
  require "my_assets/engine"
elsif defined? Sprockets::Plugin
  require "my_assets/plugin"
end

Copyright (c) 2011 Peter Browne. See LICENSE for details.