Manifest Files and Directives
suggest changeThe asset initializer (config/initializers/assets.rb) lists extra files to precompile.
# Precompile additional assets.
# application.coffee, application.scss, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
In this example, application.coffee and application.scss are manifest files. Use them to pull in other JavaScript or CSS assets. Sprockets directives:
require <path>: works like Ruby’srequire. It declares a dependency and loads that file only once, before the rest of the source file.require_directory <path>: requires all the files inside a single directory. It’s similar topath/*since it does not follow nested directories.require_tree <path>: requires all the nested files in a directory. Its glob equivalent ispath/**/*.require_self: causes the body of the current file to be inserted before any subsequentrequiredirectives. Useful in CSS files, where it’s common for the index file to contain global styles that need to be defined before other dependencies are loaded.stub <path>: remove a file from being includeddepend_on <path>: Allows you to state a dependency on a file without including it. This is used for caching purposes. Any changes made to the dependency file will invalidate the cache of the source file.
An application.scss file could look like:
/*
*= require bootstrap
*= require_directory .
*= require_self
*/
An application.coffee manifest that includes jQuery and Turbolinks. The require name must match a file Sprockets can find. jquery2 is only valid if that asset exists; the jquery-rails gem usually provides jquery.
#= require jquery2
#= require jquery_ujs
#= require turbolinks
#= require_tree .
If you don’t use CoffeeScript, but plain JavaScript, the syntax would be:
//= require jquery2
//= require jquery_ujs
//= require turbolinks
//= require_tree .
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents