Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Sprockets Railtie Setup

This file at actionpack/lib/sprockets/railtie.rb defines the Sprockets::Railtie, defining another Railtie for Rails. This Railtie is responsible for detecting if the application at-hand is using CoffeeScript, and if so will set config.app_generators.javascript_engine on the application to be :coffee.

Next, this defines an initializer called sprockets.set_configs which sets up ActionController::Base to either use or not use sprockets depending on the configuration option config.assets.enabled.

Finally, this file defines an after_initialize hook for the application which is the real meat of this Railtie. If assets are disabled (with the config.assets.enabled config option set to false) then this after_initialize hook will do nothing.

If assets are enabled then, this initializer first makes a call to an asset_environment protected method near the bottom of this file which requires the sprockets gem, creates a new Sprockets::Environment object, and then configures this object appropriately based off the configuration settings defined in railties/lib/rails/configuration.rb (near the top).

If this application is configured to compress assets -- it is in production, isn't in development; the setting is config.assets.compress -- then this railtie will also define a js_compressor and css_compressor setting for the new Sprockets::Environment object. These are configured using the config.assets.js_compressor and config.assets.css_compressor settings for the application.

Currently, js_compressor can take the symbols of :closure (closure-compiler gem), :uglifier (uglifier gem) and :yui (yui gem) while the css_compressor only takes :yui. With these settings configured, Sprockets will compress assets using the specified tool to minimise the size of the files being sent back to the client.

Once asset_environment is done, this Railtie next includes the Sprockets::Rails::Helper (defined in actionpack/lib/sprockets/helpers/rails_helper.rb) into ActionView::Base using an ActiveSupport.on_load hook. This helper overrides the javascript_include_tag and stylesheet_link_tag helpers to source their files from an /assets root path rather than simply /images or javascripts.

Finally, the after_initialize hook for the Sprockets::Railtie defines that this new Sprockets::Environment object is prepended to the routes file:

 app.routes.prepend do
   mount app.assets => assets.prefix
 end

The assets.prefix variable in this setting is simply /assets. This means that when an asset is requested using tags such as javascript_include_tag it will be served by this Sprockets::Environment object that is configured by this Railtie. The actual code to serving assets is within Sprockets itself, inside the Sprockets::Server module which is included into Sprockets::Environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.