Rails Asset Pipeline with sass #imports [duplicate] - ruby-on-rails

This question already has answers here:
Import regular CSS file in SCSS file?
(15 answers)
Closed 7 years ago.
I'm using Rails 4.2.
In my rails project directory, I have a frontend directory:
/railsproject/frontend
/railsproject/frontend/styles
/railsproject/frontend/styles/main.scss
/railsproject/frontend/styles/variables.scss
/railsproject/frontend/node_modules/normalize.css/
/railsproject/frontend/node_modules/normalize.css/normalize.css
(I know it's unusual to have a directory with a period in it, but I have tried changing the directory name and corresponding #import statement and it didn't make a difference)
I have added config.assets.paths << Rails.root.join("frontend","styles") to application.rb.
In /railsproject/app/assets/stylesheets/application.css:
#import "main.scss";
Everything is good so far, it loads/processes the main.scss file.
In /railsproject/frontend/styles/main.scss
#import 'variables'; // this works fine
#import '../node_modules/normalize.css/normalize.css'; // this does NOT work
The second import doesn't work...I get a GET error in the Chrome console, and the rails server says:
Started GET "/node_modules/normalize.css/normalize.css" for ::1 at 2015-07-02 15:55:38 +0800
ActionController::RoutingError (No route matches [GET] "/node_modules/normalize.css/normalize.css"):
What am I doing wrong?

SASS is not letting you import CSS files, it wants SCSS files to compile into CSS.
Either switch your file extension to SCSS or upgrade to SASS 3.2.10 which is supposed to let you import CSS files.
Reference: Import regular CSS file in SCSS file?

Related

How to include SCSS files for Angular 5 in Rails 5 with Webpacker

I'm trying to setup a new Rails project with webpacker and Angular. The basic setup is working, but I'm not able to include custom SCSS files. I followed the steps in the blog post Angular with Rails and Webpacker, but I wasn't able to get it up and running. I end up with the error
ERROR in ./node_modules/css-loader??ref--2-2!./node_modules/postcss-loader/lib??ref--2-3!./node_modules/sass-loader/lib/loader.js??ref--2-4!./node_modules/to-string-loader/src/to-string.js!./node_modules/css-loader!./node_modules/resolve-url-loader!./node_modules/sass-loader/lib/loader.js!./app/javascript/hello_angular/app/app.component.scss
Module build failed:
h1 {
^
Invalid CSS after "": expected 1 selector or at-rule, was "var result = requir"
in /Users/florianfeldhaus/IdeaProjects/rails-test/app/javascript/hello_angular/app/app.component.scss (line 1, column 1)
I documented all steps I carried out (I tried first without and then with the hack described in the blog post), but couldn't get it to work. I shared a minimal project (e.g. without the HTML template) on GitHub.
What do I need to do to include SCSS files for an Angular 5 App served with Rails 5 with Webpacker?
Is there any full, up to date tutorial or documentation available for this setup?
I finally found the answer to this myself. I believe the problem is that the sass-loader is already being included somewhere by default. When you go to add it to your loaders in environment.js, it's actually trying load sass-loader twice and causing a cryptic error. So the solution is to use the following code to overwrite the existing entry and then also include css-loader and to-string-loader in order to include it in your Angular component.
environment.loaders.insert('sass', {
test: /\.scss$/,
use: [
"to-string-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS
]
});

How to import SCSS mixins in a Rails app?

Trying to figure out how to import some SCSS files in a Rails 4 app. The files are:
#import "shared/mixins",
"shared/reset",
"shared/about-light";
I'm not sure how to do this properly and I don't know how to set the path either.
I put these files in a folder called "shared" and I put this folder inside of /lib/assets/css/ Is this the right way to do it?
Also tried to put the files in /vendor/assets/stylesheets/
How do I properly import these files?
Error:
File to import not found or unreadable: mixins.
Load paths:
We typically put mixins in a partial file called _mixins.scss directly in the app/assets/stylesheets/ directory, possibly under a subdir like shared if you want more organization. In your application.css you can then do (as you did)
#import 'shared/mixins';
#import 'shared/colors';
or whatever. If you really want them to sit somewhere else, you should look into the load_paths configuration setting for SASS. You can tell SASS where to look when it's importing files and probably lib/assets and vendor/assets are not included by default.
To add this configuration, in your environment.rb or other config file, you can do something like
config.sass.load_paths << File.expand_path('../../vendor/assets/stylesheets/')
which will make SASS look in vendor/assets/stylesheets in addition to all the other directories it searches in by default.
I think you are confusing partials and mixins.
From the Sass documentation:
Mixin
"A mixin lets you make groups of CSS declarations that you want to reuse throughout your site. You can even pass in values to make your mixin more flexible. You can think of a mixin like a function for css (awesome right!)." More about mixins here.
Partial
"You can create partial Sass files that contain little snippets of CSS that you can include in other Sass files. This is a great way to modularize your CSS and help keep things easier to maintain. A partial is simply a Sass file named with a leading underscore." More about partials here.
Therefore, what you are attempting to import are indeed partials and should be prefaced with an underscore.
Your directory structure should look like this:
assets
stylesheets
application.css.scss
shared
_mixins.scss
_reset.scss
_about-light.scss
And your application.css.scss file should look like this:
#import "mixins";
#import "rest";
#import "about-light";
Ok so I was able to work this out as follows:
In application.rb add the following:
config.sass.load_paths << File.expand_path('../../vendor/assets/stylesheets/')
In the CSS file import as:
#import "mixins",
"reset",
"about-light";
Put the files in to:
/vendor/assets/stylesheets/
Restart the server.

Rails / Sass: Import file from the vendor folder

I have a stylesheet app/assets/website/base.scss that starts with:
#import "bootstrap-select.min.css";
The bootstrap-select.min.css file exists in the vendor/stylesheets/ folder. When I try to access it in production, I get a 404:
"NetworkError: 404 Not Found - http://mysite.herokuapp.com/assets/bootstrap-select.min.css"
(It works fine on my development rig though.)
Here's what I've tried so far:
Tried using #import asset-path("bootstrap-select.min.css"). Got a syntax error (apparently asset-path doesn't work with imports).
Tried adding config.assets.precompile += %w(bootstrap-select.min.css) to config/environments/production.rb
Any idea why this might be happening?
Rails 4.0.4 / Ruby 2.1.2
Ended up changing it from bootstrap-select.min.css --> bootstrap-select.min.scss ..
It worked!
Sass actually places the contents of .scss files in place - With .css files it just links to the file with an #import, which is why it wasn't working (thanks #cimmanon!)
Actually, you could just remove .css suffix from #import "bootstrap-select.min.css"; Sass expects from you just the name of the assets and is smart enough to look in all possible asset placements and import whatever it can import (so the name should be format-independent)

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/"
]

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