2012-02-06 18:51:21 +00:00
|
|
|
# Get at available assets in loaded gems in Sprockets
|
|
|
|
|
2012-08-02 16:50:20 +00:00
|
|
|
Who says you need Rails Engines to use the vendored assets in gems? All you need is
|
|
|
|
`sprockets-vendor_gems`. Use those cool vendored JavaScript & CSS gems in anything!
|
|
|
|
|
|
|
|
The easiest way? Punch Sprockets right in the duck!
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
require 'sprockets-vendor_gems/extend_all'
|
|
|
|
```
|
|
|
|
|
|
|
|
So, for instance, in Sinatra with [sinatra-sprockets](https://github.com/amarshall/sinatra-sprockets):
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
# config.ru
|
|
|
|
|
|
|
|
require 'sinatra/base'
|
|
|
|
require 'sprockets-vendor_gems/extend_all'
|
|
|
|
require 'sinatra/sprockets'
|
|
|
|
|
|
|
|
class MyApp < Sinatra::Base
|
|
|
|
register Sinatra::Sprockets
|
|
|
|
|
|
|
|
get '/' do
|
|
|
|
"hi"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
map "/assets" do
|
|
|
|
run Sinatra::Sprockets.environment
|
|
|
|
end
|
|
|
|
|
|
|
|
run MyApp
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
*Every* `Sprockets::Environment` instance now has every vendored gems asset path added!
|
|
|
|
By default, that's the `javascripts`, `stylesheets`, and `images` directories. Need more?
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
Sprockets::VendorGems.default_types << "coolthings"
|
|
|
|
```
|
|
|
|
|
|
|
|
Do that before any `Sprockets::Environment`s get instantiated.
|
|
|
|
|
|
|
|
Want more manual control? Either get the list of asset paths:
|
2012-02-06 18:51:21 +00:00
|
|
|
|
|
|
|
``` ruby
|
|
|
|
require 'sprockets-vendor_gems'
|
|
|
|
|
|
|
|
env = Sprockets::Environment.new('.')
|
|
|
|
Sprockets.find_gem_vendor_paths(:for => :javascript).each do |path|
|
|
|
|
env.append_path path
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2012-08-02 16:50:20 +00:00
|
|
|
or, instead of duck punching all of Sprockets, get an Environment with those paths in there already:
|
2012-02-08 13:49:29 +00:00
|
|
|
|
2012-02-06 18:51:21 +00:00
|
|
|
``` ruby
|
|
|
|
require 'sprockets-vendor_gems'
|
|
|
|
|
2012-02-08 13:49:29 +00:00
|
|
|
env = Sprockets::EnvironmentWithVendoredGems.new
|
2012-02-06 18:51:21 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Yeah!
|
|
|
|
|
2012-08-02 16:50:20 +00:00
|
|
|
You may have to futz a bit with `require` and fake classes for particular gems, especially if they really
|
|
|
|
rely on Rails to get their job done.
|