Sass Conditionals Based on Environment Setting - sass

In a Compass config.rb file, you can set the environment to :development or :production.
Is it possible to use this setting in Sass conditionals? Here's what I want to do:
#if (environment === :development) {
#import 'debug';
}
Solved: I found the answer while drafting my question. Will post anyway since I didn't find anything definitive that actually explains this.

Thanks in part to this issue in the Compass repository, I found that you can use settings-based conditionals in Sass like this:
#if compass-env() == 'development' {
body {
color: red;
}
}
Confirmed this works with Sass 3.2.5 + Compass 0.12.2 + Ruby 1.9.3p194.
However you can't do this:
#if compass-env() == 'development' {
#import 'debug';
}
That throws a Syntax Error: "Import directives may not be used within control directives or mixins.
So the workaround is to #import the file and then wrap its entire contents in the environment conditional.

This can done (when Compass is not available) by storing environment information in an dynamically generated SCSS file, which can then be imported wherever this information is required. This should be done before the SASS compiler is started.
Here's what the build script can look like (i.e. build.sh):
#!/bin/bash
echo "\$ENV: 'production';" > ./_sass/env.scss
../node_modules/.bin/node-sass ./_sass/style.scss ../_site/css/style.css --source-map=true
This SCSS file can the be used like so:
#import 'env';
#if ($ENV == 'production') {
...
// some SCSS styling to be used in production only
...
}

Related

In sass, replacing #import with #use not expanding variables

I want to start using #use instead of #import, since #import is going to be depreciated. I have a partial:
// _test.scss
$color1: #ee00ee;
referenced by:
// demo.scss
#use test;
html {
color: $color1;
}
If I comment out the html: $color1 line, I get no error. If I delete _test.scss I get a file not found error, so it seems the file is included.
But when everything is in place, I'm getting an undefined variable error. If I replace #use with #import, I get no error and the generated css has the expected color.
I'm on a Mac (big sur), using dart sass:
% dart --version
Dart SDK version: 2.9.3 (stable) (Tue Sep 8 11:21:00 2020 +0200) on "macos_x64"
The docs I've seen say that #use will replace #import, so I thought just changing to #use would be easy. Is there a step I'm missing?
It's a bit late, but as I understand it, #use treats the partial as modules, which more closely resembles the way JavaScript treats ES modules: In particular, the #use statement makes available a namespaced bit of external code, which changes the way you reference imported variables (and mixins and functions as well):
// demo.scss
#use test;
html {
color: test.$color1;
}
Some documentation is found in the article about #use on the SASS docs, and this article explains dart-sass' namespacing in some detail.

How to include SCSS Glob in a Gatsby project?

I am currently working on setting up a boilerplate that uses Gatsby. Everything so far has been very simple and easy to use, but I can't seem to fix one problem, which is getting SCSS glob hooked up with my global SCSS styling.
I currently have localized SCSS styling for each component. However, I also have a styles directory for my global styles(variables, typography...ect). This is also using SCSS and is working great. Now the last thing I want to do is get SCSS glob working so I can do imports like /**/*.scss within my global styles.
Currently, I am using the gatsby-plugin-sass and have included globImporter as an option within my gatsby-config.js file. However, it does not seem to do it for me.
From what I read node-sass-glob-importer should be what I need but no luck so far.
My configuration looks like the following
{
resolve: `gatsby-plugin-sass`,
options: {
importer: globImporter(),
cssLoaderOptions: {
camelCase: false,
},
},
},
I then try to do a global import in my scss like so #import "./**/*.scss"; but I get the following error:
An #import loop has been found:
has anyone set up scss glob on gatsby or see anything wrong with my configurations.
Thanks
If you're still having this issue (or in case anyone else is), here's what worked for me:
options: {
importer: function(url, prev, done) {
// url is the path in import as is, which LibSass encountered.
// prev is the previously resolved path.
// done is an optional callback, either consume it or return value synchronously.
// this.options contains this options hash, this.callback contains the node-style callback
var result = globImporter();
return {file: result.path, contents: result.data};
}
},
It was inspired by the example code on in the node-sass repo.
Make sure to also include var globImporter = require('node-sass-glob-importer') at the top of your file.

Overriding Foundation settings not working in Middleman

Im doing my web in Middleman (static Ruby web generator)
I have Foundation installed and working. Then in my styleshhets folder I have a _settings file where I should override foundation's settings.
But somehow it is not working...
My structure is this:
web
source
bower_components
foundation
components
_settings.scss
...
images
javascript
layouts
stylesheets
...
_settings.scss
...
Then In the _settings.scss on the stlesheets folder I want to override for example the body font color to red like this:
// We use these to control various global styles
// $body-bg: $white;
$body-font-color: red;
But is taking no effect.
Config.rb:
# Change Compass configuration
compass_config do |config|
config.output_style = :compact
config.add_import_path "bower_components/foundation/scss"
config.http_path = "/"
config.css_dir = "stylesheets"
config.sass_dir = "stylesheets"
config.images_dir = "images"
config.javascripts_dir = "javascripts"
end
after_configuration do
#bower_config = JSON.parse(IO.read("#{root}/.bowerrc"))
sprockets.append_path File.join "#{root}", #bower_config["directory"]
end
Any ideas what i need to do to be able to override foundations styles via my _settings.scss on my stylesheets folder ?
Here is my web on github if you need to see the whole structure :
https://github.com/GiorgioMartini/Giorgio-Web
As long as _settings.scss is imported BEFORE foundation.scss it should work.
Your config.rb is correct. However, one thing to pay attention to is the line
#import 'foundation';
which will be treated differently than the line
#import 'foundation.scss';
Compass will first look to see if there is a foundation.scss file in the bower_components/foundation path. Since there is, it will use that one. Further, since all SASS variables have already resolved in foundation.css, importing _settings.scss beforehand will not change the default styles. Instead, make sure the top of your app.scss file looks like this:
#import 'foundation.scss';
#import 'settings';
Because bower_components/foundation/_settings.scss is fully commented out by default, it won't disrupt the new styles.

compiling SASS on Prepros with compass - failed to compile when attempting to import compass

Having completely given up attempting to touch the command line for SASS compiling, I have installed Prepos which seems to have a decent interface, and at least seems to be able to do the basics like compile SASS.
This compiles nicely when I save the file:
$my_color: #BADA55;
nav {
a{
color: $my_color;
&:hover {
color: darken($my_color, 10%);
}
}
}
The problem arises when I try to import compass, eg. if I add
#import 'compass';
Then I get a 'compilation failed' error- with a big red sad face, but naturally, no elaboration as to what the problem is.
To be honest, I barely know where to start to fix this. I just about grasp the basics of what I'm dealing with, but that's about it. I have found the available online documentation surrounding getting started with SASS nothing short of atrocious, which is why I have (reluctantly) turned to using a GUI. From my understanding Prepros comes with everything out of the box- including ruby and compass. So I really have no idea why this isn't working, and would greatly appreciate any pointers. Thanks.
EDIT: I am using the free version of Prepros (will happily pay for the full version if it actually works) and have tried adding a custom config.rb file which sets some basic configuration options:
relative_assets = true
css_dir = "css"
sass_dir = "scss"
images_dir = "images"
output_style = :expanded
line_comments = true
preferred_syntax = :scss
Doesn't work.
Sorry, adding the config.rb file (with the above content) DOES solve the issue (I'd just made an error in the path).

How to remove SASS debug from Brunch.io compiled CSS?

When I compile my SCSS code in to CSS using Brunch.io it adds the following to the CSS.
#media -sass-debug-info{filename{font-family:file\:[*FILE PATH REMOVED*]/reset\.scss}line{font-family:\000031}}
I have tried adding a "quiet" flag to the Brunch config file (as suggested here: https://github.com/brunch/sass-brunch) but this doesn't seem to make a difference.
How can I stop the compiler from adding these lines?
Try to disable :debug_info to false. Like this in config.rb:
debug_info = false
Or in brunch config file you should edit these lines:
config =
plugins:
sass:
debug: 'comments'
You need to specify it this way in config.rb (just specifying debug_info on it's own wasn't enough).
sass_options = {:debug_info => false}
Tested with Compass 1.0.3 and Sass 3.4.25
Useful blog

Resources