Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass @import using leading underscore

Tags:

sass

partials

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'; 
like image 245
Timidfriendly Avatar asked Jan 16 '15 08:01

Timidfriendly


People also ask

Why do SCSS files start with _?

The _ (underscore) is a partial for scss. That means the stylesheet its going to be imported (@import) to a main stylesheet i.e. styles. scss. The advantage on using partials is that you can use many files to organize your code and everything will be compiled on a single file.

Is @import deprecated in Sass?

As of October 2021 @import will be deprecated, along with global built-in functions, and one year after that (no later than October 2022) @import support will be dropped. Make sure you go check out the newly refreshed Sass documentation for the latest updates.

What is the use of the @import function in Sass?

The SASS @import function helps us to import multiple SASS or CSS stylesheets together such that they can be used together. Importing a SASS file using the @import rule allows access to mixins, variables, and functions to the other file in which the other file is imported.

How do I name a Sass file?

Naming Conventions. General rules for naming conventions apply to variables, functions, and mixins. When naming anything in Sass it's recommended to use all lowercase letters with dashes for separation. Sass code syntax is actually based on the CSS guidelines ruleset.


2 Answers

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 
like image 173
cimmanon Avatar answered Sep 30 '22 08:09

cimmanon


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

like image 29
Nesha Zoric Avatar answered Sep 30 '22 07:09

Nesha Zoric