1

In every language I can think of, placing the comment decorator at the start of a line will comment out that line of code (without any exceptions).

In rails, in application.js I see some lines starting with // and others starting with //=. I presume lines starting with // are comments and those lines are not executed.

Question

What's the difference between

  • lines starting with //
  • lines starting with //= and
  • lines starting with neither of those decorators?
stevec
  • 41,291
  • 27
  • 223
  • 311
  • ``//`` are standard Javascript comments. Sprockets uses ``//=`` as processor directives (see [here](https://github.com/rails/sprockets#directives) for a description). Lines that don't start with a comment are...code. – rmlockerd Oct 07 '20 at 01:36
  • 1
    No, require (in this usage) is purely a Sprockets thing. It tells Sprockets to add the referenced file to the concatenated file at that point. They don't 'run' at that point; it's a compilation directive. – rmlockerd Oct 07 '20 at 01:46
  • Thanks, @rmlockerd if you want to make it an answer, I will accept it. I have added an answer with what I have learned so far – stevec Oct 07 '20 at 01:47

1 Answers1

2

From here:

Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application’s JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files.

Also:

When it comes to asset bundling, the "Rails way" is webpack for JavaScript and Sprockets for everything else. The default setup in a fresh Rail 6 install, similar to what Basecamp uses, still compiles CSS, images, and fonts with Sprockets.

So basically

  • lines starting with //= is a sprocket processor directive (and it will get executed)
  • lines starting with // are simply comments (they won't get executed), and
  • lines starting with neither are javascript
stevec
  • 41,291
  • 27
  • 223
  • 311