Rails assets:precompile strange behavior - ruby-on-rails

I found myself in front of a strange behavior of the assets:precompile task, or at least in front of something I don't fully understand.
So, I am using Rails 3.1.3, Sprockets 2.0.3, Less 2.0.11 for my web application, plus I rely on Bootstrap for the layout, so I am using also less-rails 2.1.8 and less-rails-bootstrap 2.0.8.
I have customized the style like they say here.
The configuration of my assets is:
stylesheets
|--application.css.scss
|--custom-style/
|--variables.less
|--mixins.less
|--buttons.less
|--custom-style.css.less
In application.css.scss I do
//=require custom-style
And in custom-style I do
#import "twitter/bootstrap/reset";
//#import "twitter/bootstrap/variables"; // Modify this for custom colors, font-sizes, etc
#import "custom-style/variables";
//#import "twitter/bootstrap/mixins";
#import "custom-style/mixins";
// And all the other standar twitter/bootstrap imports...
// Other custom-style files to import
#import "custom-style/buttons"
//...
// And other rules here
//...
Finally in buttons.less I use some variables and mixins defined in the variables.less and mixins.less Bootstrap files, #white and .buttonBackground to be more specifc.
If I launch bundle exec rake assets:precompile with the above configuration, the task fails and I get this error:
$ bundle exec rake assets:precompile
/usr/local/rvm/rubies/ruby-1.9.3-p0/bin/ruby /usr/local/rvm/gems/ruby-1.9.3-p0/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
.buttonBackground is undefined
But is that if I do this changes
buttons.less --> buttons.css.less
#import "buttons" --> #import "buttons.css.less"
Everything works fine!!
Is it something related to the scope of less variables and functions when working with nested imports? Or something that has to do with the order the less parser, or Sprockets, processes the import tree?
Am I missing something or doing something in the wrong way?
Thanks :)
Note: I get the error even with the original variables and mixins files, so it's not connected with the overrides done into them.

I can think of one of two possibilities right now:
1) You should change application.css.scss to application.css.less and use:
#import "custom-style";
instead of the sprockets //= require ...
2) The LESS compiler is very finicky about its semicolons. I noticed that you have
#import "custom-style/buttons" - it should be #import "custom-style/buttons";
Dunno if the issue is really that simple, but it's a start.

Have you tried renaming the file to buttons.css.less but keeping the extension off the #import (i.e., #import "buttons")?
That's how I do it with SCSS and it works fine.
Another thought is to replace the //=require custom-style in application.css.less with an #import "custom-style" directive. The Rails Guide here recommends using #import (which is processed with SCSS/LESS) are over //=require (which is processed with Sprockets):
If you want to use multiple Sass files, you should generally use the Sass #import rule instead of these Sprockets directives. Using Sprockets directives all Sass files exist within their own scope, making variables or mixins only available within the document they were defined in.

This doesn't really answer your question, but is the only reason you are mixing sass and less to use twitter bootstrap? If you prefer to use just sass, there are some bootstrap gems where the authors convert the less to scss, like bootstrap-sass. Before I wasted too much time trying to juggle both in a project, I'd completely buy into one or the other, but that's just my opinion.

Related

Sass variable undefined during Rails asset precompile

I have several dozen sub directories in my stylesheets folder, many of which refer to each other with variables defined in the file _settings.sass. In my application.sass file, I importing settings first:
#import "settings"
#import "folder-one/some-sass-file"
#import "folder-two/some-other-sass-file"
This works well in development, but when I try to precompile, I get an error about $namespace being undefined in "folder-one/some-sass-file"--even though it's defined right there at the top in _settings.sass.
This seems to be happening because the compiler is trying to compile everything in sequential order, rather than looking at application.sass. Am I understanding this right? And if so, what can I do to remedy the problem?
You just need to add #import "settings" into your some-sass-file. Asset pipeline will minify it for you so there is no problem.

SCSS import relative to an imported stylesheet?

I've installed foundation-rails in my Rails app and run rails g foundation:install. The project-specific file(s) (mostly foundation_and_overrides.scss) are properly installed. The gem is there and it had no trouble installing, and the dependencies (SASS, Compass) are also there. But I'm getting:
Error compiling CSS asset
SASS::SyntaxError: File to import not found or unreadable: global.
Originating from:
/Users/local/.rvm/gems/ruby-2.0.0-p353/gems/foundation-rails-5.0.2.0/vendor/assets/stylesheets/foundation/components/_accordion.scss:1
After investigating, I've discovered that _accordion.scss was #imported in the main foundation.scss file, located two directories up in stylesheets/:
#import 'foundation/components/accordion'
The "missing" file, _global.scss, meanwhile, is in that same directory. If I then change the #import code in _accordion.scss from #import 'global' to #import 'foundation/components/accordion', it clears and moves on to the next error (there are a lot of sub-imports here).
It's clear that what's happening is SASS is looking for _global.scss relative to the top stylesheet, foundation.scss, and not relative to the imported stylesheet asking for it (_accordion.scss).
I can't imagine this is a bug in Foundation/Foundation-Rails – this gem wouldn't work for anyone – and I don't want to modify the gem's contents myself.
So my question: do I have to change some SASS settings to allow #import relative to an imported stylesheet? I don't want to modify this gem to make it work (I'd like to allow for future updates to the gem).
Edit
Clarification of directory structure within the gem's vendor/assets/stylesheets directory:
foundation.scss
foundation/components/_accordion.scss
foundation/components/_global.scss
Edit 2
You can actually see the gem's code and structure on github
Edit 3
Thought I'd solved the problem, but I didn't: changing from an #import to a =require got rid of the errors, and included Foundation's CSS. But require does not import the SCSS functionality - variables, mixins - that Foundation provides. There's no way to change global values this way, or to retrieve them or the mixins from the main stylesheet or other #imported stylesheets.
It looks like Foundation thinks this is the best possible solution:
Manually add Foundation's stylesheet subdirectories (each of them) to SASS's :load_paths in the main block of application.rb:
config.sass.load_paths += [
"#{Gem.loaded_specs['foundation-rails'].full_gem_path}/vendor/assets/stylesheets/foundation/components",
"#{Gem.loaded_specs['foundation-rails'].full_gem_path}/vendor/assets/stylesheets/foundation/"
]

Undefined mixin with Bourbon and Neat gems

I am using bourbon and neat gems for create the design of a rails application. My application.css.scss contains this:
#import "bourbon";
#import "neat";
#import "main";
But if I run 'rake assets:precompile' then happens this error:
rake aborted!
Undefined mixin 'outer-container'.
(in /Users/anonymous/example/app/assets/stylesheets/admin/main.css.scss)
/Users/anonymous/example/app/assets/stylesheets/admin/main.css.scss:5:in `outer-container'
/Users/anonymous/example/app/assets/stylesheets/admin/main.css.scss:5
The file main.css.scss contains this:
footer#page_footer {
#include outer-container;
nav{
#include span-columns(6);
#include shift(3);
section#about_me_footer, section#contact_footer, section#miscellaneous_footer {
#include span-columns(2 of 6);
}
}
p {
#include span-columns(6);
#include shift(3);
}
}
Someone can give me some suggestions?
I was having the same problem. I was able to get it working in two different ways.
The first way is probably less desirable but you can add your code right in the application.css.scss file:
div.container {
#include outer-container;
}
Alternatively, you can add:
#import "bourbon";
#import "neat";
To the top of your main.css.scss file.
This allows you to keep your styles organized.
The bourbon site links to a page in their wiki regarding this problem, but the solution mentioned didn't work for me:
https://github.com/thoughtbot/bourbon/wiki/Rails-Help-%5C-Undefined-mixin
I had this same problem. The solution for me was to rename a partial file from layout.css.scss to _layout.css.scss. Any files making use of SASS mixins need to be included after those mixins are loaded in. In this case it was trying to precompile the layout.css file alone, though it did not require the source of the mixins it was referencing. Adding the underscore makes the precompiler ignore that file until another file requires it.
According to the Change Log the outer-container mixin has been removed as of version 2.0.0. The highest version you can use with outer-container is 1.8.0. When adding Neat via Bundler, you will get 2.0 or higher unless you specify a version in your Gemfile.
The new way to do this looks much simpler, but that's little comfort if you have a bunch of unsupported scss.
FWIW this is the issue reported https://github.com/thoughtbot/bourbon/issues/120, Using jacklin's comment about adding the import statements directly to my main css file resolved it. However, I'd like to have this problem fixed since I dont really want to keep adding those import statements to each file I wish to use the mixins for
I have had the same problem.
I had a div using #include outer-container, and a second div containing #include span-columns(8). The second div incorrectly sat outside the first, producing the misleading error "Undefined mixin 'outer-container'". Moving the second div inside the first (within the outer-container - in the CSS and HTML) corrected the problem.
For the problem above, you must do the same thing by making sure the p tag is a child of the footer.

SASS Global Variables + Rails 3.1

I am using Rails 3.1RC4 with default SASS setup. I have the following files
application.css.scss
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
_variables.css.scss
$var1 : somevalue;
...
site_layout.css.scss
#import "variables";
... Some Sass Code
But I can't access the variables defined in _variables.css.scss in site_layout. What am I doing wrong? Rails is apparently finding the variables file since it doesn't throw a "file not found" error, which it does if I change the import filename. Still the vars are not carried over.
Any ideas?
Thanks!
The issue seems to be fixed with the latest release of rails & sprockets. In your gemfile:
gem 'rails', :git => "git://github.com/rails/rails.git", :branch => "3-1-stable"
The first part of your question (the application.css.scss code snippet) doesn't seem to have any relevance here, since it's just commented-out code, so I'll skip that part.
For the second code snippet, I don't believe SASS is set up to import a "partial" quite like you're doing here. You'd be better off doing something like this:
_variables.scss (Renamed from _variables.css.scss):
$var1: somevalue;
Note that the underscore in your filename is not required like it would be for a Ruby partial. You can just as easily name it variables.scss as long as you change the import statement to #import "variables";
site_layout.scss (Renamed from site_layout.css.scss)
#import "_variables";
.test_thingy {
color: $var1;
}
This will generate a site_layout.css file that contains the following code replacement:
.test_thingy {
color: somevalue; }
Note that there's no need to have files named like filename.css.scss like you would with HAML/ERB files. Simply name the file what you want it to be. For example, filename.scss will cause SASS to generate a CSS file called filename.css automatically.

Carry Sass variables through the Assets Pipeline, Rails 3.1 rc1

I recently branched one of my Rails 3.0 projects with the 3.1 rc1 to try the new assets pipeline. I've been using Sass in the project before going 3.1 so I've been setting up some variables and functions in a separate configure file and let all my other sass files import that one file at the first line.
This has been working out great to not repeat some color codes and general geometry through in the stylesheets. Problem is now with the new Assets Pipeline is that as I've understood it converts the ".css.sass" files to raw css before appending it to the rest of the code.
So if i specify, in my "application.css":
/*
*= require ./configure
*= require ./what_ever_other_files_i_want_to_import
*/
I get errors like:
Sass::SyntaxError
Undefined variable: "$interactive".
When i try to access the file from: http://localhost:3000/assets/application.css
Any ideas?
Sass supports Partials. That way you can include your separate configuration in __configuration.sass_ and reference it with
#import "configuration";
from your main sass-file.
Unfortunately I found out that SASS variables are be page specific.
If you want to carry your variables across all files, remove the
*= require_tree . line from your application.css.scss file and replace it with the #import "layout.css.scss"; directive to manually import each sass file.
Yes you have to #import each file

Resources