javascriptruby-on-railsasset-pipelinesprockets

What is "//= require" in a JavaScript file in a Rails app?


I'm looking at the source code of a Ruby on Rails app. Several of the JavaScript (*.js and *.es6) files in the source have one or more lines at the top of the file like:

//= require path/to/some_other_js_file

What are these lines called, and what are they doing?

(Potentially helpful SEO for folks who may be searching for a question like this, as I myself did prior to asking: slash slash equals require; double slash equals require)


Solution

  • These are "Sprockets directives," part of the Rails Asset Pipeline, part of how Rails performs combining and fingerprinting of JavaScript and CSS assets.

    See https://guides.rubyonrails.org/asset_pipeline.html, in particular section 2.4 Manifest Files and Directives.