Home

Awesome

Roda::Sprockets

This Roda plugin provides support for integrating Sprockets with your Roda codebase.

This is a fork of roda-sprocket_assets. This release supports Roda 3.x and Sprockets 3.x and 4.x.

Installation

Add this line to your application's Gemfile:

gem 'roda-sprockets'

And then execute:

$ bundle

Usage

class App < Roda
   plugin :sprockets, precompile: %w(site.js site.css),
                      opal: true,
   plugin :public

   route do |r|
     r.public
     r.sprockets if %w[development test].include? ENV['RACK_ENV']
   end
end

Parameters for the plugin:

Templates:

In your layout.erb (take note of the stylesheet_tag and javascript_tag):

<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Website</title>
<%= stylesheet_tag 'site' %>
</head>
<body>
<h1>Website</h1>
<%= yield %>
<%= javascript_tag 'site' %>
</body>
</html>

Opal support:

Opal is the first citizen of this plugin. Add opal and opal-browser gems, require 'opal', require 'opal-browser' before this plugin gets loaded. Create assets/js/site.rb:

require 'opal'
require 'opal-browser'

$document.body << DOM {
  div "Hello world!"
}

You will need to tell Opal to load this file. Add this in your template after everything has been loaded (after your javascript_tag call, it is needed too!):

<%= opal_require 'site' %>

Note that it won't be needed for plain Javascript use, only Opal needs that line.

Caching:

To speed up page loads during development, you can enable cache. Be warned, there are some caveats with how Sprockets cache works. This will improve your experience, but be prepared for some rough edges.

To enable memory cache, add an argument to your plugin config:

cache: Sprockets::Cache::MemoryStore.new(65536)

To enable filesystem cache, for it to persist across application restarts, add an argument to your plugin config:

cache: Sprockets::Cache::FileStore.new("var/cache/")

Remember: with filesystem cache problems may happen if you, for instance, update your Gems. You will then have to remove the cache for it to get repopulated.

Rake precompilation tasks:

In your Rakefile:

require_relative 'app'
require 'roda/plugins/sprockets_task'

Roda::RodaPlugins::Sprockets::Task.define!(App)

And launch: rake assets:precompile or rake assets:clean

Sprockets extended configuration:

You can configure your environment directly by accessing a method sprockets_env (which is a Sprockets::Environment) on your App class.

For instance, to enable Brotli with sprockets-exporters_pack you will need to either add this code inside your App class:

sprockets_env.register_exporter %w(text/css application/javascript image/svg+xml), Sprockets::ExportersPack::BrotliExporter

Or at some other point:

App.sprockets_env.register_exporter %w(text/css application/javascript image/svg+xml), Sprockets::ExportersPack::BrotliExporter

Do note, that some extensions, like for example opal-optimizer won't need any configuration at all beside require "opal/optimizer/sprockets".

Contributing

  1. Fork it ( https://github.com/hmdne/roda-sprockets/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request