Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 asset pipeline doesn't pick up changes to @import'd sheets

I use @import'd partial sheets to organize my css/sass:

/app
  /assets
    /stylesheets
      _constants.sass
      _layout.sass
      ...
      app.css.sass

app.css.sass:
  @import _constants.sass
  // basic styles
  @import _layout.sass
  @import ...

app.css.sass has an @import rule for _layout.sass, which allows me to share mixins and variables amongst the raw sass files before they're complied down into app.css

The problem is that Rails doesn't recognize changes to the @import'd partials (_layout.sass) and won't regenerate app.css until I make a change to the actual app.css.sass file itself. This dramatically slows down my workflow and means I have to add/remove blank lines from app.css.sass to see changes. Never had this problem in 3.0.

Is there a way of forcing sass assets to regenerate on every server request in development?

like image 736
John Lein Avatar asked Aug 12 '11 21:08

John Lein


People also ask

How does rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

What is asset Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .

What is Require_tree?

The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.

What are rails Sprockets?

Sprockets is a crucial Ruby library that is used to serve and compile web assets. However, in Rails 7.0 the library becomes an optional dependency unless the application needs to use Sprockets. In such situations, the sprockets-rails gem will have to be added to the Gemfile .


2 Answers

Using a depend_on directive may do what you require. This makes the given file a dependency of the base file (so the base file is regenerated when the dependency changes), but doesn't include it in the bundle.

For example:

/*
*= depend_on _layout.sass
*= depend_on _constants.sass
*/
@import _layout.sass
@import _constants.sass

See this sass and sprockets blog post as well as the sprockets documentation (especially the directives section).

like image 101
David Miani Avatar answered Oct 08 '22 00:10

David Miani


The answer from nanothief led me to the solution (thank you!) but in an indirect way. Once I finally found the time to get back on here and look up the referenced post, it had been updated to say that the fix was no longer needed.

Sure enough, it appears that this bug has been fixed in Rails 3.1.0 stable with sass-rails for 3.1.0. So good! I've confirmed that updating the gems does get everything back to working correctly, tested in a couple of apps.

like image 38
John Lein Avatar answered Oct 07 '22 23:10

John Lein