Rails - Difference between @import and *= require?
I guess you understand the purpose of the CSS/SASS @import
option. require
is sprockets directive. Sprockets processes directives during compile session - simple concatenation of required files. The only difference is how they handle (share) context. In a nutshell - use always @import
to be safe.
Please, look details description here:https://github.com/rails/sass-rails#important-note
I was having a problem with very slow recompiles whenever I changed my CSS. According to this article, the difference between a Sprockets require
and a Sass @import
is a significant one, at least in terms of performance:
The asset pipeline treats Sass @imports differently that it treats Sprockets. In the case of imports, each save will go through and compile all the imports each time, no matter which partial you’ve saved. The way that Sprockets are treated inside stylesheets is that only the partial you’ve saved will recompile and then be injected onto the page locally when you refresh. Sprockets are the default way of loading multiple partials into a single file for production.
By using require
for my third-party vendor dependencies, CSS recompilation now takes 1.5 seconds instead of 25 seconds.
The include and require methods do very different things.
The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method.
The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.
So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require
.
Oddly enough, Ruby's require
is analogous to C's include
, while Ruby's include
is almost nothing like C's include
.