Why Rails Asset Pipeline uses require instead of @

2019-09-01 15:39发布

问题:

If Rails (3, 4) uses SASS for precompile the CSS, Why uses "require" for default, instead of Sass directive "@import" (http://sass-lang.com/documentation/file.SASS_REFERENCE.html#import)

The same Rails documentation suggest to use "@import" (http://edgeguides.rubyonrails.org/asset_pipeline.html)

回答1:

As noted in the document you linked to, @import is preferable because Sass variables can be shared between files. But having the Sprockets-provided require directive allows you to include stylesheets from gems or bypass using Sass altogether.

There is usually more than one way to accomplish a task, it’s good to have options. For instance, Javascript has many solutions to dependency management, but the simple Sprockets require is often adequate for simple applications.