Sass #import using leading underscore - sass

I understand that it is best practise to import SASS/SCSS partials without using the leading underscore; e.g.
#import 'normalize-scss/normalize';
// this imports ./normalize-scss/_normalize.scss
My question for nerdy completeness is, are there any consequences if the file is imported using the underscore?
#import 'normalize-scss/_normalize';

No. If your file is _foo.scss, all of these imports have identical results as long as you don't have any ambiguous filenames (barring any side effects that might exist):
#import "foo";
#import "foo.scss";
#import "_foo";
#import "_foo.scss";
Files with the same name but different extension
The only time an extension is necessary is if you have both _foo.scss and _foo.sass in the same search path. You'll get the following error if you don't specify which one:
error sass/test.scss (Line 7: It's not clear which file to import for '#import "test/bar"'.
Candidates:
test/_bar.sass
test/_bar.scss
Please delete or rename all but one of these files.
)
Files with the same name, but one is prefixed with an underscore
If you have both foo.scss and _foo.scss, then foo.scss will take precedence. If you want _foo.scss instead, you'll need to add the underscore to your import statement.
#import "test/_foo";
Sass will nag you with a warning every time you save no matter what your import statement looks like:
WARNING: In /path/to/test:
There are multiple files that match the name "bar.scss":
_bar.scss
bar.scss

If you add an underscore to the start of the file name, Sass won’t compile it. So, if you don’t want colors.scss to compile to colors.css, name the file _colors.scss instead. Files named this way are called partials in Sass terminology.
More about import feature in Sass you can find here

#import 'normalize-scss/_normalize';
This will simply treat the _normalize.scss file as not partial.
So, if you want that file to be a partial file. then you add another underscore in the front of the actual file name. so the file name will be __normalize.scss.

Related

Separate main files in SCSS result in undefined variables

I'm basically doing a "theme" over existing scss by changing some variable definitions. I need to have separate css bundles rendered with different variables. I thought of having separate main files that import the common files to keep it DRY.
File structure
- variables_1.scss
- variables_2.scss
- main_1.scss
- main_2.scss
- _main-shared.scss
_main-shared.scss contains #import statements for all other scss files, but does not import variables.
Main file contents:
//main_1.scss
#use 'variables_1' as *;
#import "main-shared";
//main_2.scss
#use 'variables_2' as *;
#import "main-shared";
But multi-level imports do not seem to work resulting in SassError:
Undefined variable in _main-shared.scss.
Is there a better way to do this? I cannot import variables in _main-shared.scss since it would defeat the purpose of having separate variable files.
Is there a fix for this that I am not aware of or a better solution?

Sass doesn't compile variables properly

I start to write my own SCSS along with boostrap. I follow the hierarchy of the framework like:
/* The main file and all partial files are in the same directory */
#import "variables";
#import "mixins";
#import "other-components";
Everything compiles perfectly except that every variable from variables.scss is not compiled. From my understanding, CSS doesn't have variables except for :root, so SCSS variables will be compiled directly to its value in CSS. But when it's done compiling, the variables in CSS remain the same as they're in SCSS, just name like --primary but not value like #000.
Before my post gets slammed as duplicate, here's what I've done:
I've followed Bootstrap's practice. You can check them on Github.
I use VSCode and my colors have boxes right next to them, so I've written valid hex values;
I've tried to switch to #use and use as namespace instead of #import.
I've tried to rename my variables file with and without underscore _ back and forth. It doesn't help solve the problems.
The compiler shows no error whatsoever.
I only use sass package to compile .scss to .css. No extra libraries.
main.scss
_variables.scss
main.css
Any help is appreciated!
When using standard CSS variables, you should make SASS write out the contents like this:
--variable: #{$sass-var};
Otherwise SASS will print the variable as if it's a valid value inside a CSS value (since you could technically define something like --var: $text and then use it later with content: var(--var) to print out that string). Anyways, it's because SASS otherwise doesn't know if you want to print the output of the variable, or just a string named similar to a sass variable.

How to import a SASS file?

I'm trying to use multiple variables accross my differents files, but I got an error saying Undefined variable.
The example:
base.scss
$dark-blue: #031f60;
$blue: #36b2e6;
$dark-grey: #747577;
center.scss
#use 'base';
p {
color: $dark-blue;
}
I specifically used #use because SASS website mentioned that it will be deprecated in the future.
#use only works with dart-sass so far, nonetheless,
The naming scheme for files that you want to include should have an underscore _ at the start of the file name.
for example if anyone wants to include a variables file they should name it as _variables.scss rather than variables.scss.
In your case, change the name of base.scss to _base.scss and it should work as long as they are in the same folder/path given is correct.

Using Gumby, when I import a new custom scss file into gumby.scss, do I include an underscore?

I noticed that in the gumby.scss file, there is the line:
#import "custom";
... but the filename itself is _custom.scss.
For every file that I create after that, do I include the underscore?
As an example, if I create a file _custom2.scss, would I then use the import
#import "custom2";
or
#import "_custom2":
?
If it assumes that there will be an underscore in the file name, what are the rules for its assumptions? Does it do that with all filenames?
You can use either.
A file that begins with an _ is a partial in Sass.
Basically, if you want it to generate its own css file, don't add the underscore. If you want to import it into another sass file, use the _.

Webstorm 6 - How to make the scss file watcher ignore files

I would like the file watcher for SCSS files to ignore files with file names starting with an underscore, for example _buttons.scss.
How would I do that?
Start by adding a _ to a file that you want to be ignored... Done! From the documentation:
Partials
If you have a SCSS or Sass file that you want to import but don’t want
to compile to a CSS file, you can add an underscore to the beginning
of the filename. This will tell Sass not to compile it to a normal CSS
file. You can then import these files without using the underscore.
For example, you might have _colors.scss. Then no _colors.css file
would be created, and you can do
#import "colors";
So adding an underscore will do the job. Just don't import.
Be careful naming your files because if you have a style.scss and _style.scss Sass will see these as the same filename and trow an error:
>>> Change detected to: /Users/allcaps/test/style.scss
WARNING: In /Users/allcaps/test:
There are multiple files that match the name "style.scss":
_style.scss
style.scss
A simple workaround will be to add two underscores: __style.scss.
#LazyOne has the right idea. A Scope can be created that excludes files that being with an underscore (_). The page at http://www.jetbrains.com/phpstorm/webhelp/scopes.html#d437174e402 has more information about this, but basically you select the folder you want after creating a custom scope and then in the scope field append it again with && ! between the two and exclude files starting with an underscore.
For example:
file:website/css//* && !file:website/css//_*

Resources